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来作为当前的用户。

mysql当前用户user()与current_user()的更多相关文章

  1. MySQL Study之--MySQL普通用户无法本地登陆

    MySQL Study之--MySQL普通用户无法本地登陆       在安装完毕MySQL后,我们通常加入拥有对应权限的普通用户用来訪问数据库.在使用用户本地登录数据库的时候,常常会出现怎么登录也无 ...

  2. Mysql创建用户并授权

    运行命令行 mysql -uroot -p 登录mysql use mysql; 创建用户:create user 'test123'@'localhost' identified by '12345 ...

  3. mysql 操作用户权限

    使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','password');如 ...

  4. MySQL的用户和权限介绍

    一.关于MySQL权限的几点常识: 1.MySQL的权限系统主要用来验证用户的操作权限. 2.在MySQL内部,权限信息存放在MySQL数据库的granttable里.当mysql启动后,grantt ...

  5. Mysql新增用户,权限管理

    MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant selec ...

  6. 详解MySQL的用户密码过期功能

    这篇文章主要为大家详细介绍了MySQL的用户密码过期功能的相关资料,需要的朋友可以参考下   Payment Card Industry,即支付卡行业,PCI行业表示借记卡.信用卡.预付卡.电子钱包. ...

  7. mysql创建用户

    mysql创建用户 创建用于localhost连接的用户并指定密码 mysql> create user 'pcom'@'localhost' identified by 'aaa7B2249' ...

  8. ubuntu下mysql添加用户的问题

    在ubuntu下使用命令: $:sudo apt-get install mysql-server 命令安装的Mysql 版本为:Server version: 5.7.13-0ubuntu0.16. ...

  9. 转: MySQL 赋予用户权限(grant %-远程和localhost-本地区别)

    相关参考资料: MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. gr ...

随机推荐

  1. Mongodb的入门(7)window安装mongodb4

    Mongodb4: MongoDB CTO Eliot Horowitz 刚刚于2月16日凌晨在MongoDB西雅图大会上宣布,MongoDB将在4.0版本中正式推出多文档ACID事务支持 . “Mo ...

  2. IntelliJ Idea编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8

    解决办法: 1.Project Settings-Modules,选择项目,选择language level 8 2.选中项目,右击选择Maven-->Reimport, 再次编译. 3.Fil ...

  3. 快速设置UITableView不同section对应于不同种类的cell

    快速设置UITableView不同section对应于不同种类的cell 本文主要是为了写明如何在UITableView中,一个section对应于一种类型的cell,写起来不凌乱. 在不封装任何类的 ...

  4. Java学习---下载文件并且对文件编码

    import java.io.IOException; import java.net.URLEncoder; import sun.misc.BASE64Encoder; public class ...

  5. ISA 连接非443端口的https站点提示错误

    ISA 连接非443端口的https站点提示错误:12204 The specified Secure Sockets Layer (SSL) port is not allowed. ISA Ser ...

  6. 沉淀再出发:web服务器和应用服务器之间的区别和联系

    沉淀再出发:web服务器和应用服务器之间的区别和联系 一.前言 关于后端,我们一般有三种服务器(当然还有文件服务器等),Web服务器,应用程序服务器和数据库服务器,其中前面两个的概念已经非常模糊了,但 ...

  7. .net framework profiles /.net framework 配置

    几年前一篇关于 .net framework client profile http://www.cnblogs.com/zzj8704/archive/2010/05/19/1739130.html ...

  8. [COGS 2066]七十和十七

    2066. 七十和十七 ★★★   输入文件:xvii.in   输出文件:xvii.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 七十君最近爱上了排序算法,于是Ta ...

  9. [COGS 2064]爬山

    2064. 爬山 ★☆   输入文件:mountain.in   输出文件:mountain.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 球有一天走在街上. 一个健 ...

  10. Linux提权后获取敏感信息的方法与途径

    在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享(我的出发点).下面是一个混合的命令做同样的事情,在不同的地方,或只是一个不同的眼光来看待事物.我知道 ...