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;