Problem Description
After installing MySQL on Ubuntu, an access denied error occurred when trying to log in using the root account.
mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
solution
To solve this problem, we can use the following steps to reset the password of the root account.
- Find the password for debian-sys-maint
First, we need to find the password for the debian-sys-maint user. You can find it with the following command:
sudo cat /etc/mysql/debian.cnf
In the output, find the value of the password
field. Make a note of this password, we will use it in the next steps.
- Log in to MySQL using the password of debian-sys-maint
You can log in to MySQL using the password of the debian-sys-maint user. Use the following command:
mysql -u debian-sys-maint -p
Enter the password you recorded earlier at the prompt and press Enter.
- Switch to mysql database
After successfully logging in to MySQL, you need to switch to the mysql
database. Use the following command:
USE mysql;
- Change root user password
Change the root user's password. Use the following SQL statement:
UPDATE user SET authentication_string='' WHERE User='root';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
Please replace new_password
with the new password you want to set.
- Refresh permissions
After completing the password modification, you need to refresh the permissions for the changes to take effect. Use the following SQL statement:
FLUSH PRIVILEGES;
- Log in with new password
Use the newly set password to log in to the root account. Use the following command:
mysql -u root -p
Enter the new password you just set at the prompt and press Enter.
If all goes well, you should successfully log in to MySQL.
References
- https://blog.csdn.net/qq_26164609/article/details/106881079