1. Check MySQL authentication method
- In MySQL 5.7 and above,
root
Users may use it by defaultauth_socket
Plugins 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 viewroot
User authentication method:
SELECT user, host, plugin FROM WHERE user='root';
If you seeauth_socket
Plugins androot
User association, descriptionroot
The 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.
root
User'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
- renew
root
User'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
- if
root
The 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!