如果您在 MySql 5.7.+ 中遇到同样的问题:
Access denied for user 'root'@'localhost'
这是因为 MySql 5.7 默认允许使用套接字连接,这意味着您只需使用 连接即可 sudo mysql
。如果您运行 sql :
SELECT user,authentication_string,plugin,host FROM mysql.user;
然后你就会看到它:
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *497C3D7B50479A812B89CD12EC3EDA6C0CB686F0 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
要允许使用 root 和密码连接,请使用命令更新表中的值:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
FLUSH PRIVILEGES;
然后再次运行选择命令,你会看到它已经发生了变化:
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *2F2377C1BC54BE827DC8A4EE051CBD57490FB8C6 | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *497C3D7B50479A812B89CD12EC3EDA6C0CB686F0 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
就这样。运行并完成 sudo mysql_secure_installation
命令后,您就可以运行此过程。
对于 mariadb,使用
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('manager');
设置密码。更多信息请访问 https://mariadb.com/kb/en/set-password/