继续昨天的问题。

  确认不是密码的问题,再到QQ群里请教。这次,很快得到回复。

  事实上,上午就有同学说是“拒绝root在本地登录”。但我始终不明白这话到底是什么意思,也不好意思刨根问底(现在,也是不明白具体的意思)。当我下午确认不是密码设置问题时,那位同学再次提醒我:“没有设置root在本地登录”,然后又有同学让我加上参数“-h 127.0.0.1”。

  加上参数“-h 127.0.0.1”,确实可以正常登录了。但我还是希望在数据库里调整一下,最好在登录时不要加“-h 127.0.0.1”。这次,在百度上搜索“增加root远程登录权限”,总算找到正确的命令:http://hi.baidu.com/cst05001/item/3b3be5ef18410d3a86d9dec6

  由于这个视频是oldboy在2012年4月13日录制,我现在怀疑可能是MySQL的版本有问题。否则,为什么他delete那么多用户没事,我delete同样的用户就直接把有用的账号也给删掉了呢?

  没有查到他录制视频的MySQL版本,只是猜测。


  现在,把操作步骤整理如下,供日后查阅。我整理的文档:“MySQL多实例安装配置指南.doc”的“1.8 root失去在本地登录MySQL数据库的权限”也有这部分内容。

[root@Web ~]# mysql -uroot -p -S /data/3306/mysql.sock
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@Web ~]# mysql -uroot -p'oldboy3306' -S /data/3306/mysql.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

  增加参数:“-h 127.0.0.1”,输入密码成功登录数据库。

[root@Web ~]# mysql -uroot -p -S /data/3306/mysql.sock -h 127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.62-log Source distribution

Copyright (c) 2000, 2011, 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 user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | Web       | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB |
| root | 127.0.0.1 | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB |
+------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

  为MySQL添加root远程登录权限(3306实例的登录密码是“111”):

mysql> use mysql;
Database changed
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "111";
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@Web ~]# mysql -uroot -p -S /data/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.62-log Source distribution

Copyright (c) 2000, 2011, 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 user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | %         | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB |
| root | localhost | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB |
| root | 127.0.0.1 | *832EB84CB764129D05D498ED9CA7E5CE9B8F83EB |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql> quit
Bye


  解决问题后,我就怀疑是清理多余账号时,出现误delete,导致登录数据库报错,原因可能是root失去在本地登录数据库的权限。昨天oldboy又回复我,他也认为是我把账号删多了。


  补充2篇相关文章,没有解决这个问题,但有帮助。

http://leeon.me/a/resolve-mysql-login-fail-notes——解决MySQL密码正确却无法本地登录

http://www.dabaoku.com/jiaocheng/biancheng/mysql/2013081118389.shtml——MySQL密码正确却无法本地登录怎么办