Introduction
The "Access denied for user root@localhost" error commonly occurs when attempting to connect to a new MySQL deployment with the root user. The reasons for the error include the authentication configuration, misconfigured user privileges, and problems with the local network.
This guide will show you how to quickly resolve the "Access denied for user root@localhost" error in MySQL or MariaDB.
Prerequisites
- MySQL installed/MariaDB installed.
- Command-line access.
- User with administrative privileges.
What Causes "Access denied for user root@localhost"?
The "Access denied for user root@localhost" error appears when the mysql
login command is executed with the -u
flag and the root as the accessing user:
mysql -u root
The system outputs the error message and exits MySQL.
The most common cause of this error is auth_socket, the default server-side plugin for managing connections from localhost through the Linux socket file. The auth_socket plugin checks the system username and the specified MySQL username and allows passwordless access if the usernames match.
While the auth_socket procedure provides better security than a password, it can cause problems in accessing the root user or connecting to an external application, such as phpMyAdmin.
How to Fix "Access denied for user root@localhost"
Aside from the authentication preferences, other common causes for the "Access denied for user root@localhost" error are insufficient permissions for the root user and localhost problems. The following sections will help you troubleshoot the issue.
Create Root Password
Attempt to fix the "Access denied for user root@localhost" by changing the authentication method from auth_socket to mysql_native_password and setting the password for the root user. This method is less secure than auth_plugin, so it is necessary to set a strong password.
Follow the steps below to set up password access for the root user.
1. Log in to MySQL with the sudo command.
sudo mysql
A MySQL shell loads.
2. Use the ALTER USER
command and change the authentication method to log into MySQL as root. Replace [password] with a new password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '[password]';
On MariaDB, the command syntax is slightly different:
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('[password]');
3. Reload the grant tables to apply the configuration changes:
FLUSH PRIVILEGES;
4. Exit the MySQL shell by pressing CTRL + D on your keyboard. Alternatively, type exit;
and hit enter.
5. Type the login command. Add the -p
flag to tell MySQL to prompt for the user password.
mysql -u root -p
6. Enter the password you used with the ALTER USER
command.
The MySQL welcome message appears, followed by the mysql
prompt.
NOTE: The ALTER USER
command may not work for MySQL and MariaDB versions older than 5.7.6 and 10.1.20, respectively.
Grant Privileges to Root User
If setting up a password for the root account does not fix the error, the issue may be related to misconfigured root permissions.
If you have access to a MySQL user with administrative access, follow the steps below to grant the necessary permissions to root.
1. Log in to MySQL with an admin user.
mysql -u [username]
2. Execute the following command:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
Adding the WITH GRANT OPTION clause enables root to pass privileges to other users.
3. Reload the grant tables:
FLUSH PRIVILEGES;
Exit the MySQL shell and try logging in as root.
Use Localhost IP Address
If the error persists after setting up a root password and granting sufficient privileges to the root user, the reason may be incorrect hostname resolution on the network. To test if this is the case, type the following command:
mysql -u root -p -h 127.0.0.1
The -h flag allows the user to pass the localhost IP address to the login command instead of relying on the system to resolve the localhost hostname. If the network configuration is the issue, the command above enables you to log in.
Conclusion
After reading this article, you should know how to fix the "Access denied for user root@localhost" error. The article showed you how to change the MySQL authentication method, grant user privileges in MySQL, and perform some basic network troubleshooting.
To learn more about user management in MySQL, read How to Create a New MySQL User and Grant Privileges.