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.
Then run:
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:
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:
- Stop the MySQL server.
- Start it with grant tables disabled.
- Connect locally.
- Re-enable privileges and set a new password.
- Restart MySQL normally.
On many Linux systems, stopping the service looks like this:
Then start a temporary recovery instance:
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:
Inside the MySQL prompt, run:
After that, stop the recovery server and start MySQL normally:
Finally, verify that the new password works:
Windows Notes
The same idea applies on Windows, but the commands differ. Stop the MySQL service from an elevated Command Prompt:
Then start the server manually with grant tables disabled. The exact path depends on your installation:
Open another elevated Command Prompt and connect:
Then run the same SQL:
Shut down the temporary server and restart the Windows service:
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
rootuse 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:
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 USERwhen you already know the currentrootpassword. - If you are locked out, start MySQL with
--skip-grant-tables --skip-networkingfor local recovery. - Run
FLUSH PRIVILEGESand thenALTER USERto set the new password. - Restart MySQL normally after recovery and verify the new login.
- Check the actual
roothost entry and authentication method if the reset seems ineffective.

