mysql密码正确无法登陆问题

2019-06-26  本文已影响0人  WAHAHA402

参考链接1
参考链接2
有的时候连接数据库,在用户名密码和端口号(包括远程访问的情况)都正确的情况下,会出现报错:

MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

很大可能是因为匿名用户存在的情况。
匿名用户(即没有用户名的用户'')可以伪装成任何其他用户, 也就是说''@'localhost''any_other_username'@'localhost'是等价的。
刚刚安装mysql的时候是允许不用用户名密码登陆的。参考链接2中mysql的官方说明中,对账号密码匹配的规则是这么解释的:

When multiple matches are possible, the server must determine which of them to use. It resolves this issue as follows: (...)
1.When a client attempts to connect, the server looks through the rows [of table mysql.user] in sorted order.
2.The server uses the first row that matches the client host name and user name.
(...) The server uses sorting rules that order rows with the most-specific Host values first. Literal host names [such as 'localhost'] and IP addresses are the most specific.

当可能存在多个匹配项时,服务器必须确定要使用哪些匹配项。 它按照如下方式解决这个:(...)
1.当客户端尝试连接时,服务器按排序顺序查看[表mysql.user]的行。
2.服务器使用与客户端主机名和用户名匹配的第一行。
(...)服务器使用排序规则,首先对具有最特定主机值的行进行排序。 文字主机名[例如“localhost”]和IP地址是最具体的。

如下:

mysql> SELECT user, host FROM mysql.user;
    +------+-----------+
    | user | host      |
    +------+-----------+
    |      | localhost |  1
    | root | 127.0.0.1 |  2
    | root | ::1       |  3
    | root | localhost |  4
    | bill | %         |  5
    | bill | localhost |  6
    +------+-----------+

当用户bill通过本地连接mysql, 根据匹配规则,首先对具体主机值排序,再选取符合条件的第一个行来验证密码正确性;所以这里排好序的符合条件的行排序情况是这样:

    |      | localhost |  1
    | bill | localhost |  6
    | bill | %         |  5

这里选取第一行,如果匿名用户密码和bill的账户密码不一致,则当然连接会失败。
推荐方式是删除匿名用户,不管怎么说这都是好事。

上一篇 下一篇

猜你喜欢

热点阅读