How to Fix “Access Denied for User ‘root’@’localhost’ (using password: YES)” in MySQL/MariaDB

Access Denied for User ‘root’@’localhost

Facing the error “Access denied for user ‘root’@’localhost’ (using password: YES)” can be frustrating, especially when trying to log in to MySQL or MariaDB. This error typically indicates an authentication issue for the root user. I will walk through this guide and we’ll cover the most common causes and solutions to fix it.

Common Causes of the problem.

  1. Incorrect password for the root user.
  2. The root user is not authorized to connect from the current host.
  3. Mis-configuration of MySQL/MariaDB authentication plugins
  4. The root user password is not set properly.
  5. Corrupted MySQL/MariaDB installation or configuration issues.

Solution 1: Reset the Root Password (Safe Mode)

If you’ve forgotten the root password or it’s not working, follow these steps to reset it:

Stop the MySQL/MariaDB service:

sudo systemctl stop mysqld

Start MySQL/MariaDB in safe mode (without authentication):

sudo mysqld_safe –skip-grant-tables &

Log in as root without a password:

mysql -u root

Reset the password:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Restart the MySQL/MariaDB service:

sudo systemctl restart mysqld

Try logging in again:

mysql -u root -p

 

Solution 2: Check Authentication Plugin Configuration

Check the root user’s authentication plugin:

SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

If the plugin is auth_socket, change it to mysql_native_password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;

exit and try login

Solution 3: Ensure Proper Host Matching

By default, the root user is only allowed to connect from localhost. If you’re accessing MySQL/MariaDB remotely, ensure the user has permissions.

Grant access for remote login:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

Update the MySQL/MariaDB bind address (if necessary): Open the MySQL configuration file:

sudo nano /etc/my.cnf

bind-address = 0.0.0.0


Restart the service

sudo systemctl restart mysqld

 

Solution 4: Check and Fix MySQL Permissions Issues

If the root user permissions are corrupted, recreate them:

Access MySQL as a different sudo-enabled user:

sudo mysql -u root

Recreate the root user:

DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY 'new_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Test login

mysql -u root -p