How to Fix "Access denied for user root@localhost" MySQL Error

February 13, 2024

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.

Introduction to the article on resolving the "Access denied for root user" MySQL error.

Prerequisites

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.

Access denied for user root localhost error message.

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.

Logging in with MySQL sudo command and its output.

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]';
Alter user MySQL command output.

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.

Logging in with MySQL root user and password.

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.

Output from the privilege granting operation.

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.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Check the MySQL Version In Linux
July 11, 2019

It is essential to know which version of MySQL you have installed. The version number helps to determine if a specific feature is available...
Read more
How To Create New MySQL User and Grant Privileges
July 18, 2024

MySQL is a database application for Linux. It's part of the LAMP (Linux, Apache, MySQL, PHP) stack that powers a part of the Internet.
Read more
How to List All Users in a MySQL Database
September 18, 2024

This simple tutorial analyses the commands used to list all user accounts in MySQL. A short set of queries in MySQL...
Read more
How To Remove or Delete a MySQL User Account
April 16, 2024

This article covers the basics of using the DROP USER statement used to delete MySQL user account.
Read more