How to Resolve MySQL Error: Access Denied for User ‘root’@localhost

root@localhost

When working with MySQL on the local machine, you may encounter errors that may prevent you from working with the database and use it with other applications. 

Access denied for user root@localhost is a MySQL error that can be frustrating, as it prevents you from accessing your MySQL database. This error typically occurs when the root user password is incorrect or the root user doesn’t have the proper privileges to access the MySQL server.

The good news is that resolving this issue is relatively straightforward. 

In this tutorial, we’ll look at common reasons behind the issue. Next, we will explore several solutions you can apply to fix this issue. Whether you’re a beginner or an experienced user, these troubleshooting steps will assist you in resolving the Access denied for user root@localhost MySQL error efficiently and effectively. 

Table Of Contents

  1. Causes of the Access denied for user ‘root’@’localhost’ Error in MySQL
    1. Incorrect Password
    2. Incorrect Username
    3. Insufficient Privileges
    4. Host Incompatibility
    5. Problems in MySQL Server Configuration
    6. Conflicts in Password Encryption Method
    7. Network Issues
  2. How to Resolve the Access denied for user ‘root’@’localhost’ Error
    1. The Prerequisites
    2. Fix #1: Check Username and Password
    3. Fix #2: Reset Password for the root User
    4. Fix #3: Grant Appropriate Privileges
    5. Fix #4: Check Host Access
    6. Fix #5: Check MySQL Server Configuration
    7. Fix #6: Check Network and Firewall Settings
  3. Conclusion
  4. FAQs

Causes of the Access denied for user root@localhost Error in MySQL

In most cases, this error occurs due to one or more of the following reasons:

Incorrect Password

The password you provided for the root user is incorrect. This could happen if the password was changed, forgotten, or mistyped during login attempts.

Incorrect Username

In some cases, the provided username can be incorrect. In MySQL, usernames are case-sensitive, so ensure you use the correct case for the username (for instance, root instead of Root).

Insufficient Privileges

The root user may not have sufficient privileges to access the MySQL server or perform the requested operation. This might happen if privileges are not properly granted or revoked by accident.

Host Incompatibility

The host shown in the MySQL user account is not the one from which the root user attempts to connect. For instance, if the user account is limited to localhost connections but attempts to connect from a different host address, the DBMS will generate an access denied error.

Problems in MySQL Server Configuration

The user may not be able to access the MySQL server or run specific commands due to server limitations or misconfiguration in the server config files.

Conflicts in Password Encryption Method

The password encryption method used by the MySQL server might not match the one expected by the client. As a result, the user may experience this error because the client expects a different authentication plugin or method.

Network Issues

Connectivity issues, firewall rules, or network configurations could prevent the MySQL client from establishing a connection to the server, resulting in the access denied error.

How to Resolve the Access denied for user ‘root’@’localhost’ Error 

Now that you know the causes behind this error, let’s see how you can fix it and restore access to the MySQL server. We will start with the prerequisites.

The Prerequisites

  • A system running a mainstream Linux distribution
  • A user account with sudo or root privileges
  • You have MariaDB and MySQL installed on a server

Fix #1: Check Username and Password

Double-check the username and password you’re using to connect to MySQL. Ensure that the username is correct (case-sensitive) and that you use the correct password.

Fix #2: Reset Password for the root User

If you suspect the root password is incorrect or you have forgotten it, we recommend resetting it using the MySQL password reset procedure. This typically involves stopping the MySQL server, starting it again with the –skip-grant-tables option to bypass authentication, resetting the root password, and finally restarting the server normally. 

Here’s a general outline of the steps:

Step #1: Determine the Current MySQL Version

Run the following command in the terminal to get the current version information:

 

# mysql --version

mysql --version

With MySQL, you might see something like mySQL Ver 14.14 Distrib 5.7.16 for Linux (x86_64)

Likewise, the output for the MariaDB server version looks like this:
mysql Ver 15.1 Distrib 5.5.52-MariaDB.

Step #2: Stop the Database Server

Now, you can run the following command to stop the database server:

 

# sudo systemctl stop MySQL

Alternatively, run the following command to stop the MariaDB service: 

# sudo systemctl stop mariadb

Step #3: Restart the Database Server

If you start MySQL and MariaDB servers without loading user privileges information, you can access the database command line with root permissions and no password protection. You will need this capability to access the database.

Start by stopping the database from loading the grant tables, which include user privilege data. You should also stop networking to prevent other clients from connecting to the server, as it poses a security risk.

Run the following command to launch the database without enabling networking or loading the grant tables:

# sudo mysqld_safe --skip-grant-tables --skip-networking &

Adding an ampersand to the end of the command allows you to run this procedure in the background.

Instead, a database shell prompt will appear immediately. Type help or \h on the MySQL prompt to get assistance. Similarly, run \c to remove the current input statement.

remove the current input statement

Step 4: Change the root Password

Start by running the following statement to remove all current privileges:

mysql> FLUSH PRIVILEGES;

You can now modify the current password of the root user.

Use the following statement for MariaDB 10.1.20 and newer and MySQL 5.7.6 and newer:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

FLUSH PRIVILEGES

Alternatively, run the following statement to set the password for ‘root’@’localhost’ in MySQL 5.7.5 and older:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

Next, use the following statement to update the changes:

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';

Also, make sure to reload the grant tables.

You should notice an indication that the instruction was carried out successfully.

OK query, affecting 0 rows in 0.00 seconds

Step# 5: Restart the Database Service

You can now terminate the manual database server instance and restart it using the original configuration because the password was updated.

Run the following command to restart the database:
For MySQL# sudo systemctl start mysql

For MariaDB

#sudo systemctl start mariadb

Next, log into the MySQL shell as the root user with the following command:

# mysql -u root -p

The command should prompt you for the password.

Alternatively, run the following sequence of commands: # sudo systemctl stop mysql

# sudo mysqld_safe --skip-grant-tables & mysql -u root UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root'; FLUSH PRIVILEGES; quit; 

# sudo systemctl restart mysql

Fix #3: Grant Appropriate Privileges

In rare cases, the root user may lack sufficient privileges. 

If that’s the case behind the error, you can grant them manually. 

For this, log into the MySQL shell as a user with administrative privileges and execute the following GRANT statements to grant the necessary privileges to the root user.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

mysql> FLUSH PRIVILEGES;

grant all previliges

Fix #4: Check Host Access

Verify that the root user can connect from the localhost host. You can check the user’s host access in the MySQL user table (mysql.user) and adjust it (if necessary) by running the following statement:

mysql> SELECT User, Host FROM mysql.user WHERE User='root';

Fix #5: Check MySQL Server Configuration

Review the MySQL server config file (typically my.cnf or mysqld.cnf) for settings restricting access or authentication methods. Ensure authentication methods such as mysql_native_password are enabled and properly configured in the configuration file.

To connect to MySQL as root with a password, you must change its authentication method from auth_socket to mysql_native_password

Launch the MySQL shell from your terminal by running this command:

# sudo mysql

Next, execute the following statement to see which authentication method is being used for the MySQL users:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

mysql SELECT user authentication

Next, run the following statement that alters the root password you set in Step #2 and updates it to a strong password of your own.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

mysql ALTER USER

Next, you should verify the authentication methods used by every user to ensure the root is no longer authenticating through the auth_socket plugin:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

select user authentication
Finally, run the following statement to exit the MySQL shell: 

mysql> exit

Fix #6: Check Network and Firewall Settings

Finally, you should check that there are no network connectivity issues or firewall rules blocking connections to the MySQL server. In addition, you should verify that the MySQL server is listening to the correct ports and network interfaces.

Also Read: 3 Simple Methods to Delete Duplicate Rows in MySQL Database

Conclusion

The Access denied for user ‘root’@’localhost’ error in MySQL can often be attributed to incorrect login credentials. 

This error, while common, can cause frustration for users attempting to access their relational database management system. Users can resolve this issue swiftly by double-checking the provided username and password. However, if the problem persists, it’s essential to review the MySQL service configuration and ensure that the appropriate permissions and authentication methods are in place. With careful attention to detail and troubleshooting, users can overcome this hurdle and regain access to MySQL databases.

At RedSwitches, we’re dedicated to helping you discover the perfect server solutions to drive your business to new heights. So, if you’re looking for a robust server, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What should I do if I get an “Access denied” error despite entering the correct password?

If you know the correct password, double-check for common errors such as typos or case sensitivity. Ensure that you’re entering the password exactly as it was set.

Q. Why am I encountering the “Access denied” error when accessing MySQL with the root user?

The “Access denied” error for the root user might occur due to insufficient permissions or misconfiguration. Check if the root user has the necessary privileges to access MySQL, and review the MySQL server configuration for any restrictions.

Q. Can I use the ‘sudo mysql -u root’ command to access MySQL with root privileges?

While using ‘sudo mysql -u root’ can grant root access to MySQL, it’s essential to exercise caution. This method bypasses MySQL’s native authentication and may not be recommended for regular usage due to security risks.

Q. I’m receiving the “Access denied” error despite using the correct command ‘mysql -u root -p’. What could be the issue?

Ensure the root user has the appropriate permissions to connect to MySQL from the localhost. Look up the MySQL user rights to ensure the root user can connect from the localhost.

After troubleshooting, what should I do if I still get the “Access denied” error?

If you’ve exhausted troubleshooting options and are still facing the error, consider resetting the root password or creating a new MySQL user with appropriate permissions. Review MySQL’s error logs for more specific error messages that might explain the issue.

Q. How to grant user privileges in MySQL to fix access-denied issues?

To grant user privileges in MySQL, you can use the ALTER USER command to assign the necessary permissions for the user.

Q. What is the significance of the ‘skip-grant-tables’ option when trying to fix MySQL errors?

The ‘skip-grant-tables’ option bypasses the privilege system, allowing users with access issues to connect and reset their passwords.