MySQL server 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, 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 the 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 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

“Access denied for user ‘root’@’localhost'” Error Solution
As this error is mainly related 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 local MySQL pipe. We will use the following command which will provide us 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 same system with the following ALTER command.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypass123';
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 use this password for the root login.
$ mysql -u root -p
