ERROR 1045 (28000): Access denied for user 'user'@'host' (using password: YES)
连接mysql报错:
C:\Users\Lenovo>mysql -h 115.29.221.104 -u root -p Enter password: ********** ERROR 1045 (28000): Access denied for user 'root'@'218.205.187.58' (using password: YES)
还有类似的这样的错误,
C:\Users\Lenovo>mysql -h 115.29.221.104 -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'218.205.187.58' (using password: NO)
下面这个是没有使用密码,如果没有密码的话,当然不允许连接。上面那个情况就是输入了密码,但是密码出错了。
但是我明明就是这个密码为什么出错呢?(其实是你的密码真的错了)。
首先你确保你密码没错,远程连接数据库总是出现ERROR 1045 (28000): Access denied,为什么,下面来进一步分析。
往往会是这种情况,你在远程服务器上直接连接不会出错,而在本地连接远程服务器就会出错。就是这个错误,
C:\Users\Lenovo>mysql -h 115.29.221.104 -u root -p Enter password: ********** ERROR 1045 (28000): Access denied for user 'root'@'218.205.187.58' (using password: YES)
而在115.29.221.104上就用相同的用户名和密码就可以连接。为什么?
首先来看mysql库user表如下,
mysql> select user,host from user; +------------------+----------------+ | user | host | +------------------+----------------+ | root | % | | root | 127.0.0.1 | | root | 218.205.187.58 | | root | ::1 | | root | backup | | debian-sys-maint | localhost | | root | localhost | +------------------+----------------+ 7 rows in set (0.00 sec)
没什么异常的,在看如下查询,
mysql> select user,host,password -> from user; +------------------+----------------+-------------------------------------------+ | user | host | password | +------------------+----------------+-------------------------------------------+ | root | localhost | *31B38457C12D1E7B0533C3456109E38BAC97CFD5 | | root | backup | *31B38457C12D1E7B0533C3456109E38BAC97CFD5 | | root | 127.0.0.1 | *31B38457C12D1E7B0533C3456109E38BAC97CFD5 | | root | ::1 | *31B38457C12D1E7B0533C3456109E38BAC97CFD5 | | debian-sys-maint | localhost | *6124B487491BC901CF8A8978AFD5A61516D57B5A | | root | 218.205.187.58 | *09D87193377D387DCADE6841D46095002B222934 | | root | % | *31B38457C12D1E7B0533C3456109E38BAC97CFD5 | +------------------+----------------+-------------------------------------------+ 7 rows in set (0.00 sec)
有了以上两个查询的数据,我们来讨论一下,
首先我在本地机器登陆,如下,
C:\Users\Lenovo>mysql -h 115.29.221.104 -u root -p Enter password: **********
我机器IP为218.205.187.58,能匹配到的是这两条,
| root | 218.205.187.58 | *09D87193377D387DCADE6841D46095002B222934 | | root | % | *31B38457C12D1E7B0533C3456109E38BAC97CFD5
那么mysql server会选择哪一条呢?
当然是这个,
| root | 218.205.187.58 | *09D87193377D387DCADE6841D46095002B222934 |
这个规则可以看mysql手册。
http://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw
http://dev.mysql.com/doc/refman/5.6/en/connection-access.html
如果匹配到这条登陆信息,那么哈希之后的passwd是*09D87193377D387DCADE6841D46095002B222934,经过验证确实不是我
使用的登陆密码。问题就在这里。那么是怎么导致我密码变更了呢?
翻看了一下之前的命令记录,是这条sql语句出错了,如下,
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'218.205.187.58' IDENTIFIED BY '034039' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from user; +------------------+----------------+ | user | host | +------------------+----------------+ | root | * | | root | 127.0.0.1 | | root | 218.205.187.58 | | root | ::1 | | root | backup | | debian-sys-maint | localhost | | root | localhost | +------------------+----------------+ 7 rows in set (0.00 sec)
这个IDENTIFIED BY '034039'不是我想要的密码。而我一直使用原来的密码登陆。
====================END====================