Introduction
Most MySQL users encountered the ERROR 1698 (28000): Access denied for user ‘root’@’localhost’. This error message usually appears for new installations of MySQL when you try to connect to MySQL with the root user.
This guide will show you how to quickly resolve the access denied for user root on localhost. The outlined instructions apply to both MySQL and MariaDB. There will be no need to modify any tables or to perform complex configuration.
Prerequisites
- Access to a command line or terminal window
- MySQL or MariaDB installed
- User with sudo or root privileges
Using Root to Access MySQL
When you install MySQL and try to access it on the local machine with the root user, the command you use is:
mysql -u root -p
In most cases, you will receive the error message Access denied for user ‘root’@’localhost’.
You can enable access for root using one MySQL command.
Solve Access Denied for User Root Error
To be able to log into MySQL as root, first use sudo
to modify the root user:
sudo mysql
Enter your password at the prompt. A MySQL shell loads.
Use the ALTER USER
command and change the authentication method to log into MySQL as root:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password';
This command changes the password for the user root and sets the authentication method to mysql_native_password. This is a traditional method for authentication, and it is not as secure as auth_plugin. In the example above, we set “root” as the password, but we encourage you to set a stronger password.
Test Root User MySQL Access
After you run the commands listed above, exit the MySQL shell by pressing CTRL + D on your keyboard or type exit;
and hit enter. There is no need to restart the mysqld service to log in.
Now try again to access MySQL with root. In the terminal, type in:
mysql -u root -p
Enter the password you used with the ALTER USER
command. Do not type in the system password to access MySQL as it will not work. If everything worked fine, you should see the MySQL welcome message.
NOTE: The ALTER USER
command may not work for MySQL and MariaDB versions older than 5.7.6 and 10.1.20 respectively.
Conclusion
Now you know how to bypass the MySQL ERROR 1698 (28000): Access denied for user ‘root’@’localhost’.
There are different ways to approach this issue, but we selected the easiest and fastest method. Make sure to enter the commands as listed in the article to avoid errors in SQL syntax.