1. 程式人生 > >MySQL使用普通使用者訪問返回ERROR 1698 (28000): Access denied for user 'root'@'localhost'

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 | | mysql.session | mysql_native_password | | mysql.sys | mysql_native_password | | debian-sys-maint | mysql_native_password | +------------------+-----------------------+
  5. 執行update mysql.user set authentication_string=PASSWORD('pass'),plugin='mysql_native_password' where user='root';將root都密碼修改為pass
  6. 執行flush privileges;重新整理一下
  7. 移除第一步新增的內容;重複第二步

修改過後user表就變這樣了

+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| root             | mysql_native_password |
| mysql.session    | mysql_native_password |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+