MySQL servers may encounter different errors during usage. The “ERROR 1698 (28000): Access denied for user ‘root’@’localhost’” error is one of the most common errors which occurs during command-line connection to the MySQL server shell. This error can be a bit different according to the command like “Access denied for user ‘root’@’localhost’ (using password: YES)“. In this tutorial, we will examine this error, its causes, and solutions to prevent it again.
“Access denied for user ‘root’@’localhost'” Error Causes
This error is related to MySQL access restrictions. MySQL uses access rules in order to make things more secure and generally prevents root access to the MySQL shell. This error means the root user access to the localhost MySQL shell has been denied. The localhost means we try to access the MySQL shell over the network.
$ mysql -u root -p
Or we can get this error without providing the -p option like below.
$ mysql -u root

Alternatively, this error may be produced when we try to use the mysqladmin
with the -u root
and -p
option. We can provide the password via the command line with the -p option or enter it interactively.
$ mysqladmin -u root -p
“Access denied for user ‘root’@’localhost'” Error Solution
As this error is mainly related to that we can not access the MySQL shell via a network by using the root user. This root user is the MySQL installation root user, not the Linux system. So if we can not access via network we will access using a local MySQL pipe. We will use the following command which will provide us with the MySQL shell with root user privileges.
$ sudo mysql

Now we will change the access rules where enable the root user access via the network from the localhost or the same system with the following ALTER command.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypass123*.!';
If The Root User Password Is Unknown
Sometimes we do not have the root user password. When we enter the wrong password we may get this error. So first we should disable the MySQL service password and login into the MySQL server with the mysql
command and then set the password.
In order to disable the password, open the /etc/my.cnf
or /etc/mysql/my.cnf
configuration file and add the skip-grant-tables
lines under the [mysqld]
section. Then restart the MySQL server to activate the passwordless login configuration.
$ sudo systemctl mysql restart
Then run the following SQL query to set the new root password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypass123*.!';
The last steps are removing the skip-grant-table
from the my.conf configuration file and restarting the MySQL service like below.
$ sudo systemctl mysql restart
Login MySQL Shell
We have configured the MySQL root access from localhost properly. We will use the following command which will log in into the MySQL shell properly. Keep in mind that we have set the root user password as mypass123 and used this password for the root login.
$ mysql -u root -p
