user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

时间:2020-11-28 19:44:48

问题:
 当用户名相同,但主机名不同的多条记录。用户由不同主机登录时,选择使用那条记录来验证,数据库版本为:5.6.25

如:IP为192.168.141.241 hostname为vhost02主机上的MySQL上有下面几个用户:
 test01@localhost、test01@127.0.0.1、test01@192.168.196.128、test01@192.168.196.%、test01@vhost02  、test01@vhost03

如果要用root用户登录数据,数据库会选择哪个用户?

实验结论:

1.仅当用户的host='localhost'存在时,不指定-h可以登录;

2.其它情况,本机登录,-h 指定为什么(localhost,127.0.0.1,IP)就用host等于什么的用户登录;

3.其它情况,异地登录,登录机器IP为多少就用能够与它匹配的host登录(host越具体,此用户优先级越高)。

4.实验证明,-h指定为hostname,即使有hostname记录用户也不能通过hostname登录;

5.实验证明,-h指定为hostname,且配置/etc/hosts中对应hostname的记录时,将-h hostname 转化为-h IP登录;

测试过程:

mysql> create user test01@'localhost' identified by 'localhost'; Query OK, 0 rows affected (0.25 sec)

mysql> create user test01@'127.0.0.1' identified by '127001'; Query OK, 0 rows affected (0.00 sec)

mysql> create user test01@'192.168.196.128'  identified by '192168196128'; Query OK, 0 rows affected (0.01 sec)

mysql> create user test01@'192.168.196.%' identified by '192168196%';Query OK, 0 rows affected (0.17 sec)
mysql> create user test01@'vhost02' identified by  'vhost02'; Query OK, 0 rows affected (0.00 sec)

mysql> create user test01@'vhost03' identified by  'vhost03';Query OK, 0 rows affected (0.12 sec)

 

 

test01@localhost、test01@127.0.0.1、test01@192.168.196.128、test01@192.168.196.%、test01@vhost02都存在时

 

NO.1 不指定-h

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

NO.2 指定-h localhost

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

测试127.0.0.1时需要打开skip-name-resolve

NO.3 指定-h 127.0.0.1

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

NO.4 指定-h 192.168.196.128

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

 NO.5 指定-h vhost02

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

 test01@localhost不存在、test01@127.0.0.1、test01@192.168.196.128、test01@192.168.196.%、test01@vhost02都存在时

 user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

 

test01@192.168.196.128\test01@192.168.196.%

mysql> drop user test01@'192.168.196.128';Query OK, 0 rows affected (0.26 sec)

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

配置/etc/hosts,再使用-h vhost02登录

vhost02上测试

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?

vhost03上测试

user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?