日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mysql当前用户user()与current_user()

發布時間:2025/7/14 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql当前用户user()与current_user() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Mysql在進行登陸時,會去匹配mysql庫中的user表,并賦予相應的權限,但是怎么知道我們當時的登陸的用戶名及相應的權限呢?

在Mysql中,有兩個函數,一個是user(),一個是current_user();?

?

我們來運行一下看一下他們有什么區別:

mysql> select user(); +----------------------+ | user() | +----------------------+ | test@192.168.203.132 | +----------------------+ 1 row in set (0.00 sec) mysql> select current_user(); +------------------+ | current_user() | +------------------+ | test@192.168.%.% | +------------------+ 1 row in set (0.00 sec)

?

user()是用來顯示當前登陸的用戶名與它對應的host,幫助文檔是這樣描述的:

Returns the current MySQL user name and host name as a string in the
utf8 character set.

currrent_user()是用來顯示當前登陸用戶對應在user表中的哪一個,幫助文檔是這樣描述的:

Returns the user name and host name combination for the MySQL account
that the server used to authenticate the current client. This account
determines your access privileges. The return value is a string in the
utf8 character set.

?

所以假如我們想知道當前登陸的用戶具有什么權限的話,

第一步是找出當前登陸用戶是用user表中的哪一個,用current_user()

mysql> select current_user(); +------------------+ | current_user() | +------------------+ | test@192.168.%.% | +------------------+ 1 row in set (0.00 sec)

?

第二步用show grants命令,如下:

?

mysql> show grants for 'test'@'192.168.%.%'; +--------------------------------------------------------------------------------------------------------------------------------+ | Grants for test@192.168.%.% | +--------------------------------------------------------------------------------------------------------------------------------+ | GRANT SELECT, INSERT, UPDATE ON *.* TO 'test'@'192.168.%.%' IDENTIFIED BY PASSWORD '*032197AE5731D4664921A6CCAC7CFCE6A0698693' | +--------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

?

好了,那另一個問題是,如果有如下的用戶名,host及權限,我在登陸時到底會是匹配到哪一個呢?

mysql> grant select on *.* to test@192.168.203.132 identified by '000000'; Query OK, 0 rows affected (0.00 sec) mysql> grant select,update on *.* to 'test'@'192.168.203.%' identified by '000000'; Query OK, 0 rows affected (0.01 sec) mysql> grant select,update,insert on *.* to 'test'@'192.168.%.%' identified by '000000'; Query OK, 0 rows affected (0.01 sec) mysql> grant select,update,insert,delete on *.* to 'test'@'192.%.%.%' identified by '000000'; Query OK, 0 rows affected (0.00 sec) mysql> grant update,insert,delete on *.* to 'test'@'%' identified by '000000'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from user order by user,host; +-------------+-----------------+ | user | host | +-------------+-----------------+ | root | localhost | | test | % | | test | 192.%.%.% | | test | 192.168.%.% | | test | 192.168.203.% | | test | 192.168.203.132 | +-------------+-----------------+

?

如果我用如下命令進行登陸,會匹配到user表中的哪一個?

1 [root@host2 ~]# mysql -h192.168.203.132 -utest -p

  

我們可以用上面提到的select current_user()可以清楚地查找出來

mysql> select current_user(); +----------------------+ | user() | +----------------------+ | test@192.168.203.132 | +----------------------+ 1 row in set (0.00 sec)

?

?

我們刪除對應的帳戶:

delete from user where user='test' and host='192.168.203.132';

再次登陸:

[root@host2 ~]# mysql -h192.168.203.132 -utest -p

此時:

mysql> select current_user(); +------------------+ | current_user() | +------------------+ | test@192.168.203.% | +------------------+ 1 row in set (0.00 sec)

?

繼續刪除

mysql> delete from user where user='test' and host='192.168.203.%';

再登陸:

mysql> select current_user(); +------------------+ | current_user() | +------------------+ | test@192.168.%.% | +------------------+ 1 row in set (0.00 sec)

?

以上每一次執行后用user()都可以得到相同的結果:

mysql> select user(); +----------------------+ | user() | +----------------------+ | test@192.168.203.132 | +----------------------+ 1 row in set (0.00 sec)

?

所以結論是:mysql在登陸時會用最精確匹配user表中的帳戶,host來作為當前的用戶。

轉載于:https://www.cnblogs.com/crxis/p/10444398.html

總結

以上是生活随笔為你收集整理的mysql当前用户user()与current_user()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。