MySQL error: #1045 - Access denied for user 'root'@'localhost' (using password: YES)

MySQL error: #1045 - Access denied for user 'root'@'localhost' (using password: YES)

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

MySQL error: #1045 - Access denied for user 'root'@'localhost' (using password: YES)

You are locked out of your MySQL instance because the server has rejected your credentials, yet you cannot authenticate to reset them. This specific scenario—where a forced password change leaves you with no access—is a classic administrative nightmare. The error message Access denied for user 'root'@'localhost' (using password: YES) indicates that the server is receiving a password prompt and rejecting it, whereas mysql -u root without arguments fails because no password was provided at all.

The core issue here is privilege escalation. To reset your own password, you must authenticate as a user with administrative privileges. If you are logged in as the OS-level root user (superuser) on Linux or Windows, you can bypass MySQL authentication entirely to modify the mysql system database. This approach works because MySQL stores its internal users and permissions in the mysql.user table, which is just another table in the filesystem that an OS superuser can edit directly.

Bypassing Authentication via OS Superuser Access

The most reliable method to recover access involves using a shell command to connect to the server without sending any credentials. You must ensure you are running these commands as the root user of your operating system, not necessarily the MySQL root user.

On Linux, execute the following sequence:

sudo mysql -u root

If the prompt asks for a password and you refuse to type one, or if it accepts an empty string, you have successfully bypassed authentication. Once inside the SQL shell, you can inspect the current state of your root account:

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

If you see a hash (often starting with *) or a long string for authentication_string, that is why the password prompt fails. You need to update this field directly. However, simply running an ALTER USER statement might fail if the server configuration (skip-grant-tables or similar) isn't set up correctly to allow privilege changes without authentication.

A safer and more universal approach involves updating the table directly using a text editor or SQL commands that do not require privilege verification in this specific context, though direct table manipulation is risky. A better alternative within the shell session is to force an update of the authentication_string field for the root user at localhost:

UPDATE mysql.user SET authentication_string = '' WHERE user='root' AND host='localhost';
FLUSH PRIVILEGES;
EXIT;

Note that on MySQL 8.0 and later, you might need to hash an empty string or set the plugin to mysql_native_password if the server expects a specific format. However, setting it to an empty string often works as a "reset" because it effectively removes the password requirement for that specific host/user combination.

On macOS, similar logic applies using sudo mysql -u root, provided your system allows MySQL to run under the standard user account or via a specific launchd agent.

The skip-grant-tables Workaround

If you cannot access the shell as an OS superuser, or if direct table updates fail due to server version constraints, you can modify the MySQL configuration to skip privilege checks entirely. This is a high-risk operation that should only be done during maintenance windows.

  1. Locate your MySQL configuration file (usually /etc/my.cnf on Linux or /my.cnf on macOS).
  2. Add skip-grant-tables under the [mysqld] section.
  3. Restart the MySQL service: sudo systemctl restart mysql.

Once restarted, you can connect with no password:

mysql -u root

Inside the shell, reset the password using a generated hash to ensure compatibility with modern plugins:

UPDATE mysql.user SET authentication_string = PASSWORD('NewSecurePassword123!') WHERE user='root';
FLUSH PRIVILEGES;

After resetting the password, immediately remove skip-grant-tables from your configuration and restart MySQL again. Leaving this option enabled is a critical security vulnerability that allows any user to connect without authentication, effectively handing over control of your database.

Why PHPMyAdmin Fails While the Shell Succeeds

The discrepancy between your shell access and PHPMyAdmin errors highlights how different clients handle authentication. When you run mysql -u root without arguments, the client library sends an empty password string. The server then checks if that empty string matches the stored credential. If the account was forced to have a new password but you haven't logged in to set it, the stored hash doesn't match an empty string, resulting in rejection.

PHPMyAdmin explicitly prompts for a password and sends whatever you type (or attempts to send a default) to the server. Since the root account now expects a specific hash that you don't know, the connection fails immediately with error #1045.

Final Considerations

Once you regain access, you should audit your user accounts. Ensure that no other users have been created with excessive privileges during the forced password change process. If you are using Laravel or another framework, remember to update your .env file credentials accordingly, as these often default to root with an empty password in development environments but require a strong hash in production.

If you find yourself unable to access the OS shell or modify configuration files, your only remaining option is to stop the MySQL service and start it with skip-grant-tables, reset the password, and then restart normally. Always verify that skip-grant-tables is removed before restarting the service permanently.

Our network