MySQL使用普通用户访问返回ERROR 1698 (28000): Access denied for user 'root'@'localhost'

时间:2023-03-09 08:57:11
MySQL使用普通用户访问返回ERROR 1698 (28000): Access denied for user 'root'@'localhost'

这个问题最开始查资料都说要改密码,密码不对。其实不是这个样子都。

解决方法

  1. 修改/etc/mysql/my.cnf,添加以下内容
    [mysqld]
    skip-grant-tables
  2. 重启mysql服务/etc/init.d/mysql restart
  3. 使用mysql -u root连接
  4. 执行select user, plugin from mysql.user;

    若显示以下结果,请继续;若没有,这篇文章解决不了。
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | auth_socket |
+------------------+-----------------------+
  1. 执行update mysql.user set authentication_string=PASSWORD('pass'),plugin='mysql_native_password' where user='root';将root都密码修改为pass
  2. 执行flush privileges;刷新一下
  3. 移除第一步添加的内容;重复第二步

修改过后user表就变这样了

+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
+------------------+-----------------------+