Troubleshooting MySQL Error: Access Denied for 'root'@'localhost' (using password YES)
Resolve the 'Access denied for user root@localhost using password YES' MySQL error. Learn common causes and step-by-step fixes for this critical database authentication issue on Linux systems.
Introduction
Encountering an âAccess denied for user ârootâ@âlocalhostâ using password YESâ error while trying to connect to your MySQL or MariaDB server is a common and often frustrating hurdle for system administrators and developers alike. This error indicates that the database server is rejecting your login attempt for the root user from the localhost address, despite a password being provided. This guide will walk you through the typical scenarios leading to this issue and provide a comprehensive, step-by-step resolution.
Symptom & Error Signature
When attempting to connect to your MySQL/MariaDB server using the mysql client or when an application tries to establish a database connection, you will typically see an error message similar to one of the following:
From the mysql client:
mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
From application logs (example for a PHP application):
PHP Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /var/www/html/index.php on line X
From Docker container logs:
[Entrypoint] MySQL init process in progress...
[ERROR] Access denied for user 'root'@'localhost' (using password: YES)
Root Cause Analysis
The âAccess denied for user ârootâ@âlocalhostâ using password YESâ error specifically means that the MySQL/MariaDB server received a password, but it was either incorrect, or the root user is not configured to authenticate with a password from localhost. Hereâs a breakdown of the most common underlying causes:
- Incorrect
rootpassword: This is the most straightforward cause. The password you are providing simply does not match the one stored in the MySQL user table forroot@localhost. auth_socketplugin in use: On modern Linux distributions (like Ubuntu and Debian), MySQL/MariaDB often configures therootuser to use theauth_socketauthentication plugin by default forroot@localhost. This plugin allows the Unixrootuser to connect to MySQL asrootwithout a password, provided they are connecting fromlocalhostand their Unix user ID matches. When you supply a password, the server still attempts to useauth_socketand fails if the Unix user doesnât match or if you explicitly try to authenticate with a password against anauth_socketuser.- User
root@localhostnot configured for password authentication: Similar to theauth_socketissue, therootuser might exist but is specifically configured to not usemysql_native_passwordorcaching_sha2_password(the standard password authentication plugins). - Corrupted or improperly configured
mysql.usertable: Less common, but possible if there were manual interventions or issues during database upgrades, leading to an invalid entry forroot@localhost. - Environment inconsistencies (e.g., Docker): In Docker environments, misconfigured entry points or volume mounts might lead to a fresh database initialization that doesnât respect expected
rootpasswords, or an application might be trying to connect before the database is fully initialized and secured.
The key distinction in âusing password YESâ is that the server received credentials but rejected them, rather than not receiving any password at all.
Step-by-Step Resolution
The most robust way to resolve this issue is to reset the root password for localhost and ensure itâs configured to use a standard password authentication plugin.
#### 1. Stop the MySQL/MariaDB Service
First, you need to stop the running MySQL/MariaDB service to start it in safe mode.
sudo systemctl stop mysql
# For MariaDB, it might be:
# sudo systemctl stop mariadb
[!IMPORTANT] Verify the service status after stopping. If itâs still running, subsequent steps will fail or operate on the wrong instance.
sudo systemctl status mysql
# Look for "Active: inactive (dead)"
#### 2. Start MySQL/MariaDB in Safe Mode (Skip Grant Tables)
Starting the database with --skip-grant-tables allows you to connect to the MySQL server as root without a password, bypassing all permission checks. This is crucial for resetting the password. Weâll also use --skip-networking to prevent remote connections during this vulnerable state.
sudo mysqld_safe --skip-grant-tables --skip-networking &
[!WARNING] Running
mysqld_safedirectly might not work as expected on all Systemd-based systems, or it might leave processes orphaned. A more controlled approach is to usesystemctloverrides. However, for a quick fix like this,mysqld_safe &is commonly used. Be sure to kill it later.To ensure it starts correctly and cleanly, you might prefer this:
Alternative (more robust) method for Systemd systems:
Create a temporary Systemd override:
sudo systemctl edit mysql
Add the following lines to the editor that opens:
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking --socket=/var/run/mysqld/mysqld.sock --pid-file=/run/mysqld/mysqld.pid
Save and exit (usually Ctrl+X, Y, Enter). Then restart MySQL:
sudo systemctl daemon-reload
sudo systemctl start mysql
[!NOTE] Ensure you remember to remove this override after fixing the issue, as shown in step 6.
#### 3. Connect to MySQL as root
Now, connect to the MySQL server. Since you started it with --skip-grant-tables, you wonât be prompted for a password.
mysql -u root
You should now see the MySQL prompt: mysql>.
#### 4. Reset root Password and Update Authentication Plugin
Inside the MySQL prompt, you need to perform two key actions:
- Flush privileges: Reloads the grant tables.
- Alter the
rootuser: Set a new password and ensure themysql_native_passwordorcaching_sha2_passwordplugin is used. MySQL 8.0+ defaults tocaching_sha2_password, while older versions and MariaDB often usemysql_native_password. Use the plugin appropriate for your MySQL/MariaDB version and client compatibility.
mysql> FLUSH PRIVILEGES;
-- For MySQL 8.0+ (recommended for modern clients):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YourStrongNewPasswordHere';
-- For MariaDB or older MySQL versions (or if caching_sha2_password causes client issues):
-- mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongNewPasswordHere';
mysql> FLUSH PRIVILEGES;
mysql> quit;
[!WARNING] Replace
YourStrongNewPasswordHerewith a robust, unique password. Do not use this placeholder password in production!
#### 5. Restart MySQL/MariaDB Service Normally
After resetting the password, you must stop the MySQL/MariaDB service and restart it in its normal operating mode to re-enable grant tables and networking.
If you used mysqld_safe &:
# Find the mysqld_safe process
ps aux | grep mysqld_safe
# Kill it (replace PID with the actual process ID)
sudo kill PID
Then restart the service:
sudo systemctl restart mysql
# For MariaDB:
# sudo systemctl restart mariadb
If you used the Systemd override method:
sudo systemctl stop mysql
sudo systemctl daemon-reload # Reload Systemd to pick up the original service file
sudo systemctl start mysql
#### 6. Remove Systemd Override (if used)
If you created a temporary Systemd override in step 2, remove it now to ensure your service runs with its default configuration.
sudo systemctl revert mysql
sudo systemctl daemon-reload
#### 7. Verify New Password
Finally, try connecting to your MySQL server with the new password.
mysql -u root -p
Enter password: YourStrongNewPasswordHere
You should now successfully connect to the MySQL prompt.
[!IMPORTANT] If you are using
auth_socket(e.g., you can connect assudo mysqlwithout a password), and you want to continue using password authentication forroot, ensure that theALTER USERcommand specifically sets a password. If your goal is to only useauth_socketforroot, then you should avoid setting a password and simply connect viasudo mysqlor a client designed to use the socket. However, for most external applications, password-based authentication is preferred.Consider creating dedicated, less privileged users for your applications instead of using
root.
-- Example: Create a new user for an application
CREATE USER 'your_app_user'@'localhost' IDENTIFIED BY 'YourAppPassword';
GRANT SELECT, INSERT, UPDATE, DELETE ON your_database.* TO 'your_app_user'@'localhost';
FLUSH PRIVILEGES;
