MySQL
root password
security
database management
password reset

How to reset or change the MySQL root password?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing the MySQL root password is straightforward when you already know the current password, and still manageable when you do not. The safe approach depends on whether you are rotating credentials during normal administration or recovering access to a locked-out server.

Changing the Password When You Can Still Log In

If you already know the current root password, use a normal client session and run ALTER USER. This is the cleanest method because MySQL keeps its privilege tables active and logs the change as an account-management operation.

bash
mysql -u root -p

Then run:

sql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'StrongerPassword123!';
FLUSH PRIVILEGES;

In modern MySQL versions, ALTER USER is preferred over directly updating internal system tables. FLUSH PRIVILEGES is often not strictly required after ALTER USER, but including it is harmless and makes the intent explicit.

To verify the new password, exit and reconnect:

bash
mysql -u root -p

If the login succeeds with the new secret, the change is complete.

Resetting the Password When You Forgot It

If you cannot log in, you need a recovery procedure. The usual approach is:

  1. Stop the MySQL server.
  2. Start it with grant tables disabled.
  3. Connect locally.
  4. Re-enable privileges and set a new password.
  5. Restart MySQL normally.

On many Linux systems, stopping the service looks like this:

bash
sudo systemctl stop mysql

Then start a temporary recovery instance:

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

The --skip-networking flag matters because --skip-grant-tables disables authentication checks. You do not want that recovery instance exposed to remote clients.

Now connect without a password:

bash
mysql -u root

Inside the MySQL prompt, run:

sql
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'RecoveredPassword123!';

After that, stop the recovery server and start MySQL normally:

bash
sudo systemctl stop mysql
sudo systemctl start mysql

Finally, verify that the new password works:

bash
mysql -u root -p

Windows Notes

The same idea applies on Windows, but the commands differ. Stop the MySQL service from an elevated Command Prompt:

bat
net stop MySQL80

Then start the server manually with grant tables disabled. The exact path depends on your installation:

bat
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe" --skip-grant-tables --skip-networking

Open another elevated Command Prompt and connect:

bat
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" -u root

Then run the same SQL:

sql
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'RecoveredPassword123!';

Shut down the temporary server and restart the Windows service:

bat
net start MySQL80

Service names vary, so check the actual name in services.msc if MySQL80 is not correct.

Security Considerations

The root account is the highest-privilege account in many MySQL deployments. Treat a password reset as a security-sensitive event:

  • choose a strong password
  • avoid reusing old secrets
  • store it in a password manager
  • limit direct root use in applications

In many production systems, a separate administrative account is safer for daily work, leaving root for emergency maintenance only.

If your server uses a different authentication plugin, such as socket-based local authentication on some Linux packages, the issue may not be the password at all. In that case, inspect the configured authentication method before assuming the password is wrong.

Common Pitfalls

One common mistake is editing the mysql.user table manually. That used to work in older setups, but ALTER USER is the supported and clearer approach in current MySQL versions.

Another mistake is forgetting to restart MySQL normally after using --skip-grant-tables. Leaving the server in that state is a major security problem because authentication checks are bypassed.

A third issue is changing 'root'@'localhost' when the actual login account is a different host entry, such as 'root'@'127.0.0.1'. If the password change appears not to work, inspect the defined users:

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

Finally, some administrators try to reset the password while applications are actively reconnecting in the background. During recovery, stop dependent services first so they do not interfere with the maintenance window.

Summary

  • Use ALTER USER when you already know the current root password.
  • If you are locked out, start MySQL with --skip-grant-tables --skip-networking for local recovery.
  • Run FLUSH PRIVILEGES and then ALTER USER to set the new password.
  • Restart MySQL normally after recovery and verify the new login.
  • Check the actual root host entry and authentication method if the reset seems ineffective.

Course illustration
Course illustration

All Rights Reserved.