解决Mysql最常见的无法连接问题,MySql ERROR 1698 (28000) 错误:Access denied for user 'root'@'localhost'

MySql ERROR 1698 (28000) 错误:Access denied for user 'root'@'localhost'

本文参考:https://blog.csdn.net/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启动之后就可以不用密码登陆了:

  1. ~$ mysql -u root

  2. Welcome to the MySQL monitor. Commands end with ; or \g.

  3. Your MySQL connection id is 2

  4. Server version: 5.7.10 MySQL Community Server (GPL)

(4)查看一下user表,错误的起因就是在这里, root的plugin被修改成了auth_socket,用密码登陆的plugin应该是mysql_native_password。(也就是应该把这个字段改成用密码登陆,而不是用socket!)

  1. mysql> select user, plugin from mysql.user;

  2. +-----------+-----------------------+

  3. | user | plugin |

  4. +-----------+-----------------------+

  5. | root | auth_socket |

  6. | mysql.sys | mysql_native_password |

  7. | dev | mysql_native_password |

  8. +-----------+-----------------------+

  9. 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字段

  1. mysql> update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';

  2. Query OK, 1 row affected, 1 warning (0.00 sec)

  3. Rows matched: 1 Changed: 1 Warnings: 1

  4.  
  5. mysql> flush privileges;

  6. Query OK, 0 rows affected (0.00 sec)

(6)重启服务,问题就解决了:

  1. ~$ sudo service mysql stop

  2. ...

  3. * MySQL Community Server 5.7.10 is stopped

  4. ~$ sudo service mysql start

  5. ..

  6. * MySQL Community Server 5.7.10 is started

  7. ~$ mysql -u root -p

  8. Enter password:

  9. Welcome to the MySQL monitor. Commands end with ; or \g.

  10. Your MySQL connection id is 2

  11. Server version: 5.7.10 MySQL Community Server (GPL)

修改后的user表结果如下:

猜你喜欢

转载自blog.csdn.net/u011495642/article/details/84206790