Immediate Fix
The most effective way to fix the “Access denied for user ‘root’@’localhost'” error is to restart MySQL in safe mode and reset the authentication method. This bypasses the standard permission checks temporarily.
Step 1: Stop the MySQL Service
You must shut down the active database service before you can restart it in manual mode.
sudo systemctl stop mysql
Step 2: Start MySQL without Grant Tables
Running MySQL with the --skip-grant-tables flag allows anyone to connect with full privileges without a password.
sudo mysqld_safe --skip-grant-tables &
Step 3: Update the Root Password and Plugin
Connect to the database and force a password update while ensuring the authentication plugin is set to a password-based method.
mysql -u root
-- Inside the MySQL prompt:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_secure_password';
FLUSH PRIVILEGES;
EXIT;
Step 4: Restart the Service
Kill the safe-mode process and restart the standard MySQL service to apply the changes securely.
sudo killall mysqld
sudo systemctl start mysql
Technical Explanation
In modern MySQL versions (specifically 5.7 and 8.0+), the default authentication plugin for the root user is often set to auth_socket or caching_sha2_password.
The auth_socket plugin ignores passwords and instead checks if the operating system user matches the database user. If you are logged into Linux as a standard user and try to access MySQL root, the connection is rejected even if you provide a password.
| Component | Description |
|---|---|
| auth_socket | Authenticates via local system UID; requires sudo to login as root. |
| mysql_native_password | The traditional method that requires a defined password string. |
| skip-grant-tables | A startup mode that disables all security checks for recovery purposes. |
Switching to mysql_native_password allows third-party applications and scripts to connect using a standard username/password combination.

Alternative Methods
Method 1: Using Sudo Privileges
If your system is using the auth_socket plugin, you don’t actually need to reset the password. You can simply login by elevating your terminal privileges.
sudo mysql -u root
Once inside, if you prefer to set a password, use the ALTER USER command mentioned in the “Immediate Fix” section.
Method 2: Checking the .my.cnf File
Sometimes, the “Access Denied” error occurs because of a configuration file in your home directory that provides the wrong credentials automatically.
nano ~/.my.cnf
Ensure there aren’t conflicting user or password entries under the [client] or [mysql] headers. If they exist, comment them out with a # and try connecting again.
Method 3: Verifying Host Permissions
Ensure that the root user is actually allowed to connect from ‘localhost’. You can verify this in the user table.
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';
If the host is set to a specific IP or ‘%’ instead of ‘localhost’, you will need to adjust your connection string or the user record accordingly.