MySql ERROR 1698 (28000) 错误:Access denied for user 'root'@'localhost'
本文参考:https://blog.youkuaiyun.com/qq_35709559/article/details/82691883
网上的方案查了一天半,都没有有效的,好在最后解决了。
废话不讲,直接说解决方案
原因和解决方案:mysql里其实是有个叫mysql的database,里面有个user表。这里面有root用户,但是密码根本不是你设的那个,你需要把它改成正确的。
解决步骤:
(1) 停止mysql服务:
~$ sudo service mysql stop
(2)以安全模式启动MySQL(这可以帮你跳过验证密码过程,&表示后台运行,切记以sudo来执行否则会失败!):
~$ sudo mysqld_safe --skip-grant-tables &
(3)MySQL启动之后就可以不用密码登陆了:
-
~$ mysql -u root
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.10 MySQL Community Server (GPL)
(4)查看一下user表,错误的起因就是在这里, root的plugin被修改成了auth_socket,用密码登陆的plugin应该是mysql_native_password。(也就是应该把这个字段改成用密码登陆,而不是用socket!)
-
mysql> select user, plugin from mysql.user;
-
+-----------+-----------------------+
-
| user | plugin |
-
+-----------+-----------------------+
-
| root | auth_socket |
-
| mysql.sys | mysql_native_password |
-
| dev | mysql_native_password |
-
+-----------+-----------------------+
-
3 rows in set (0.01 sec)
(5)关于auth_socket,在官方有说明: https://dev.mysql.com/doc/mysql-security-excerpt/5.5/en/socket-authentication-plugin.html ,反正现在暂时不用它, 那就把这里改了。
【补充一下 authentication_string这个字段就是密码,以后会逐渐废弃password字段
】
-
mysql> update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';
-
Query OK, 1 row affected, 1 warning (0.00 sec)
-
Rows matched: 1 Changed: 1 Warnings: 1
-
mysql> flush privileges;
-
Query OK, 0 rows affected (0.00 sec)
(6)重启服务,问题就解决了:
-
~$ sudo service mysql stop
-
...
-
* MySQL Community Server 5.7.10 is stopped
-
~$ sudo service mysql start
-
..
-
* MySQL Community Server 5.7.10 is started
-
~$ mysql -u root -p
-
Enter password:
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.10 MySQL Community Server (GPL)
修改后的user表结果如下: