SoFunction
Updated on 2025-04-14

Solution to MySQL's "Access denied for user" problem

1. Check MySQL authentication method

  • In MySQL 5.7 and above,rootUsers may use it by defaultauth_socketPlugins instead of passwords for authentication.
  • To view the authentication method, you can log in to MySQL via the following command:
sudo mysql -uroot -p 

Then run the following query to viewrootUser authentication method:

SELECT user, host, plugin FROM  WHERE user='root';

If you seeauth_socketPlugins androotUser association, descriptionrootThe user uses the socket authentication method.

You can modify the authentication method and password to solve the problem. Note that the password must also be modified after modifying the authentication method.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

2. Reset password (if using password authentication)

  • If the authentication method is based on password, you can reset it through the following steps.rootUser's password:
  • Stop MySQL service:
sudo systemctl stop mysql
  • Start MySQL using skip authorization table mode (no permission checking):
sudo mysqld_safe --skip-grant-tables &
  • Log in to MySQL:
mysql -u root
  • renewrootUser's password:
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE user='root';
FLUSH PRIVILEGES;
  • Restart the MySQL service:
sudo systemctl start mysql
  • Try to log in with a new password:
mysql -u root -p

3. Check user permissions

  • ifrootThe user's permissions are restricted. You can use the following command to check the user's permissions and make appropriate adjustments:
SHOW GRANTS FOR 'root'@'localhost';

If necessary, you can adjust the permissions, for example:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

After following the above steps to check and fix it, the Access denied error should be resolved.

The above is the detailed content of the solution to the problem of "Access denied for user" in MySQL. For more information about MySQL Access denied for user, please follow my other related articles!