欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 21:33:57
同一个账号,不同客户端登录,可以根据user()来记录IP、判断是谁在操作。 ......

mysql在进行登陆时,会去匹配mysql库中的user表,并赋予相应的权限,但是怎么知道我们当时的登陆的用户名及相应的权限呢?

在mysql中,有两个函数,一个是user(),一个是current_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)
mysql当前用户user()与current_user()

 

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当前用户user()与current_user()
mysql> select current_user();
+------------------+
| current_user()   |
+------------------+
| test@192.168.%.% |
+------------------+
1 row in set (0.00 sec)
mysql当前用户user()与current_user()

 

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

 

mysql当前用户user()与current_user()
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)
mysql当前用户user()与current_user()

 

好了,那另一个问题是,如果有如下的用户名,host及权限,我在登陆时到底会是匹配到哪一个呢?

mysql当前用户user()与current_user()
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 |
+-------------+-----------------+
mysql当前用户user()与current_user()

 

如果我用如下命令进行登陆,会匹配到user表中的哪一个?

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

  

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

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

 

 

我们删除对应的帐户:

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

再次登陆:

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

此时:

mysql当前用户user()与current_user()
mysql> select current_user();
+------------------+
| current_user()   |
+------------------+
| test@192.168.203.% |
+------------------+
1 row in set (0.00 sec)
mysql当前用户user()与current_user()

 

继续删除

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

再登陆:

mysql当前用户user()与current_user()
mysql> select current_user();
+------------------+
| current_user()   |
+------------------+
| test@192.168.%.% |
+------------------+
1 row in set (0.00 sec)
mysql当前用户user()与current_user()

 

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

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

 

所以结论是:mysql在登陆时会用最精确匹配user表中的帐户,host来作为当前的用户。