Heim > Fragen und Antworten > Hauptteil
P粉8645949652023-08-08 13:46:16
如果在 MySQL 5.7.+ 中有这个问题:
Access denied for user 'root'@'localhost'
因为 MySQL 5.7 默认允许通过 socket 连接,这意味着您只需使用管理员权限连接到 MySQL。
SELECT user,authentication_string,plugin,host FROM mysql.user;
t然后就看到
+------------------+-------------------------------------------+-----------------------+-----------+ | 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)
允许使用管理员权限连接并输入密码,然后通过命令更新表中的值:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password'; FLUSH PRIVILEGES;
然后再次运行select命令,你就会看到它已经更改了:
+------------------+-------------------------------------------+-----------------------+-----------+ | 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/。
P粉6475042832023-08-08 09:12:02
使用 resetting 密码的说明 - 但不是重置密码,而是将一个记录强制插入到 mysql.user 表中
INSERT INTO mysql.user (Host, User, Password) VALUES ('%', 'root', password('YOURPASSWORD')); GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;