How to Reset Root Password in MySQL Quickly and Safely
To reset the MySQL
root password, stop the MySQL server, start it with the --skip-grant-tables option to disable password checking, then connect and update the password using ALTER USER. Finally, restart the server normally to apply changes.Syntax
Resetting the MySQL root password involves these main steps:
- Stop MySQL server: Temporarily stop the running MySQL service.
- Start with skip grant tables: Run MySQL without password checks using
--skip-grant-tables. - Change password: Connect to MySQL and run
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';. - Restart server: Stop and start MySQL normally to enforce the new password.
bash
sudo systemctl stop mysql sudo mysqld_safe --skip-grant-tables & mysql -u root ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; FLUSH PRIVILEGES; exit sudo systemctl stop mysql sudo systemctl start mysql
Example
This example shows how to reset the root password to MyNewPass123! on a Linux system using systemctl and mysql client.
bash
sudo systemctl stop mysql sudo mysqld_safe --skip-grant-tables & mysql -u root ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass123!'; FLUSH PRIVILEGES; exit sudo systemctl stop mysql sudo systemctl start mysql mysql -u root -p
Output
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.x MySQL Community Server - GPL
mysql>
Common Pitfalls
Common mistakes when resetting the root password include:
- Not stopping the MySQL server before starting with
--skip-grant-tables. - Forgetting to run
FLUSH PRIVILEGES;after changing the password. - Not restarting the MySQL server normally after the password change.
- Using deprecated commands like
SET PASSWORDinstead ofALTER USERin MySQL 8.0+.
sql
/* Wrong way: Using SET PASSWORD (deprecated) */ mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); /* Right way: Use ALTER USER */ mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';
Quick Reference
| Step | Command/Action | Description |
|---|---|---|
| 1 | sudo systemctl stop mysql | Stop MySQL server |
| 2 | sudo mysqld_safe --skip-grant-tables & | Start MySQL without password checks |
| 3 | mysql -u root | Connect to MySQL as root without password |
| 4 | ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; | Change root password |
| 5 | FLUSH PRIVILEGES; | Reload privilege tables |
| 6 | exit | Exit MySQL client |
| 7 | sudo systemctl stop mysql | Stop MySQL server again |
| 8 | sudo systemctl start mysql | Start MySQL normally with new password |
Key Takeaways
Always stop the MySQL server before starting with --skip-grant-tables.
Use ALTER USER to change the root password in MySQL 8.0 and later.
Run FLUSH PRIVILEGES after changing the password to apply changes.
Restart MySQL normally after resetting the password to secure the server.
Avoid deprecated commands like SET PASSWORD for root password changes.