Solution:
1. Log in to the MySQL server as the root user using the command line.
2. Run the following command to reset the root user’s password:
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘new_password’;
3. Replace ‘new_password’ with the desired new password.
4. Exit the MySQL command line and log in again using the new password.
If you are trying to log in to your MySQL for the first time or you forgoted to set password, while installing mysql. Then you get this ERROR 1698 (28000): Access denied for user ‘root’@’localhost’. There can be two reasons for the error to appear. First of all, you have to not install MySQL correctly. Or forget to set the root user password in MySQL.
In this tutorial, you will learn how to resolve 1698 (28000) Access denied for user ‘root’@’localhost’.
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
There are two ways to resolve this error, as follows:
- Method 1: Fix error 1698 (28000): Access denied for user using mysql_secure_installation
- Method 2: Fix error 1698 (28000): Access denied for user using Alter
Method 1: Fix error 1698 (28000): Access denied for user using mysql_secure_installation
Now, open your terminal or command line and execute the following command into it to set a password for your MySQL root user:
sudo mysql_secure_installation
Once you have executed the above command. After that, a prompt will open on the terminal or command line in which you will have to type the password and confirm the password. Then press enter to set a password for your MySQL root user.
If your error is not resolved even after using the first method, then close the terminal and follow the second method.
Method 2: Fix error 1698 (28000): Access denied for user using Alter
When you install MySQL for the first time into your server or machine, you may not get the option to set a password. If so, you will need to login to MySQL as the root user.
Most of the time, users may try to log in to MySQL as “mysql -u user” or “mysql -u root”, but it won’t work because “[email protected]” has not yet been created on your mysql server, and root cannot be accessed without sudo privileges.
If you try to login to your mysql server using the command “mysql -u [email protected]” then you will get this error:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
So for this, you should use sudo command to login to your mysql server:
sudo mysql -u root
Now you have login to your MySQL server using above command. But there is a small problem that you cannot log in to MySQL without sudo privileges, and MySQL can be accessed by any privileged or sudo user.
To prevent that, you can set a password prompt to appear whenever anyone tries to log in.
Now, execute the following command on terminal or command line to check password mechanism or plugin is attached to your root account:
mysql> SELECT User, plugin from mysql.user ;
Once you have executed the above command. After that, on your terminal, the support of your MySQL Mechanism will be shown like “mysql_native_password” or the latest “caching_sha2_password“ on later version 8.0.0.
If your mysql server supports machines of “caching_sha2_password” then you have to use the below command. So that you can change or update the password of the root user in your MySQL server.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '[ENTER-NEW-PASSWORD]';
Now execute the following command on terminal or command line to restart the server to apply the changes:
sudo systemctl restart mysql.service
Conclusion
That’s all. In this tutorial, you have learned simple two ways on how to resolve 1698 (28000) Access denied for user ‘root’@’localhost’.