Mysql在進行登錄時,會去匹配mysql庫中的user表,並賦予相應的權限,可是怎麼知道咱們當時的登錄的用戶名及相應的權限呢?mysql
在Mysql中,有兩個函數,一個是user(),一個是current_user(); sql
咱們來運行一下看一下他們有什麼區別:bash
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,幫助文檔是這樣描述的:ide
Returns the current MySQL user name and host name as a string in the
utf8 character set.函數
currrent_user()是用來顯示當前登錄用戶對應在user表中的哪個,幫助文檔是這樣描述的:spa
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.code
因此假如咱們想知道當前登錄的用戶具備什麼權限的話,server
第一步是找出當前登錄用戶是用user表中的哪個,用current_user()blog
mysql> select current_user(); +------------------+ | current_user() | +------------------+ | test@192.168.%.% | +------------------+ 1 row in set (0.00 sec)
第二步用show grants命令,以下:ci
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來做爲當前的用戶。