MySQL对于相同名称用户但host不同的登陆选择

MySQL多个同名用户且不同host会怎样

有时候我们会发现有创建多个相同名称的用户,但是host给的不是相同的,最普遍的是有的小伙伴习惯性的创建 root 用户,系统有默认的 root@‘127.0.0.1’ ,而自己再去创建个 root@‘%’ 或 root@'192.168.20.%'类似这种的,那么存在多个相同名称的了,mysql在登录时候会选择哪个用户来进行登陆呢?
记得有块官方文档介绍这一块的,一时没找到,感兴趣小伙伴自己去找下。大致意思应该如下:

服务器使用与客户端主机名和用户名相匹配的第一行记录进行授权。
在服务器使用的排序规则中,先排序主机名字段值(越精确的值越靠前,字符串主机
名和IP地址是最具体的。
另外,IP地址的精确度不会受到掩码的影响,例如:
192.168.100.13和192.168.100.0/255.255.255.0被视为具有相同的精确度。
通配符“%”表示“任意主机”,被视为精确度较差的主机名。空字符串也意味着“任意主机”,但精确度比“%”更差,所以排在“%”之后),
然后按照用户名字段值进行排序(排序规则与主机名字段值的排序规则类似)。
主机名和用户名两个字段值的排序规则有点类似于多列索引中的排序规则。

直接上环境测试验证。
测试环境:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.25-log |
+------------+
1 row in set (0.00 sec)

演示

1.创建不同相同用户名且不同host的用户

SESSION 1

mysql> create user 'yangq'@'192.168.20.100' identified by '123456a';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'yangq'@'192.168.20.%' identified by '123456b';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'yangq'@'192.168.%' identified by '123456c';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'yangq'@'%' identified by '123456d';
Query OK, 0 rows affected (0.01 sec)

2.登录测试

SESSION 2

当前存在 % ,192.168.% , 192.168.20.% , 192.168.20.100,使用ip 连接一下看看,使用的是哪个用户连接的。
结果发现,连接是已最小权限的那个用户去判断连接的,当前最小host是 192.168.20.100

[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456a
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 199
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select current_user();
+----------------------+
| current_user()       |
+----------------------+
| yangq@192.168.20.100 |
+----------------------+
1 row in set (0.00 sec)

mysql> exit
Bye
[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456b
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'yangq'@'192.168.20.100' (using password: YES)
[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456c
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'yangq'@'192.168.20.100' (using password: YES)
[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456d
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'yangq'@'192.168.20.100' (using password: YES)

删除 192.168.20.100 的 host 用户

mysql> drop user 'yangq'@'192.168.20.100';
Query OK, 0 rows affected (0.01 sec)

再次登录,按照上面的推测,应该是使用 yangq@‘192.168.20.%’ 这个用户了。
验证结果证实确实是这么回事

[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456b

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

mysql> exit
Bye
[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456c
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'yangq'@'192.168.20.100' (using password: YES)
[root@tx19 ~]# mysql -h192.168.20.100 -uyangq -p123456d
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'yangq'@'192.168.20.100' (using password: YES)

结论

若存在多个相同用户名称,但host限制不同的用户,MySQL优先会选择host限制大的,也就是开放最少ip段的用户,其余的不能登录。
例如:

yangq@'192.168.20.100'
yangq@'192.168.20.%'
yangq@'192.168.%'
yangq@'%'

用户登陆时候,会优先选择使用yangq@'192.168.20.100’用户进行登陆,即使你使用其他的用户与正确的密码也不能登录上去,因为去校验的是 yangq@‘192.168.20.100’ 这个用户的密码了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值