mysql> select user, host, password from user;
+------------------+--------------------------+-------------------------------------------+
| user | host | password |
+------------------+--------------------------+-------------------------------------------+
| root | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | vagrant-ubuntu-trusty-64 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| | localhost | |
| | vagrant-ubuntu-trusty-64 | |
| debian-sys-maint | localhost | *9B8E84CA988E1CA66CEB919A0D9D3BF1532942B6 |
+------------------+--------------------------+-------------------------------------------+
7 rows in set (0.00 sec)
可以看到数据库中有如上这些用户。那么,如果有'root'@'localhost'登录时,怎么匹配呢?
根据MySQL官方文档MySQL :: MySQL 5.6 Reference Manual :: 6.2.4 Access Control, Stage 1: Connection Verification里是如下面这样说的:
复制代码 代码如下:
When multiple matches are possible, the server must determine which of them to use. It resolves this issue as follows:
Whenever the server reads the user table into memory, it sorts the rows.
When a client attempts to connect, the server looks through the rows in sorted order.
The server uses the first row that matches the client host name and user name.