P粉8645949652023-08-08 13:46:16
If you have this problem in MySQL 5.7.:
Access denied for user 'root'@'localhost'
Because MySQL 5.7 allows connections via sockets by default, this means you only need to connect to MySQL with administrator privileges.
SELECT user,authentication_string,plugin,host FROM mysql.user;
tThen I saw
+------------------+-------------------------------------------+-----------------------+-----------+ | 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)
Allow to connect with administrator privileges and enter a password, then update the values in the table with the command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password'; FLUSH PRIVILEGES;
Then run the select command again and you will see that it has changed:
+------------------+-------------------------------------------+-----------------------+-----------+ | 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)
Then this is it. After completing the sudo mysql_secure_installation command, you can run the following procedure.
For mariadb, use:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('manager');
Then set the password. For more information, see https://mariadb.com/kb/en/set-password/.
P粉6475042832023-08-08 09:12:02
Instructions for using resetting password - but instead of resetting the password, force insert a record into the mysql.user table
INSERT INTO mysql.user (Host, User, Password) VALUES ('%', 'root', password('YOURPASSWORD')); GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;