0
0
MysqlHow-ToBeginner · 3 min read

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 PASSWORD instead of ALTER USER in 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

StepCommand/ActionDescription
1sudo systemctl stop mysqlStop MySQL server
2sudo mysqld_safe --skip-grant-tables &Start MySQL without password checks
3mysql -u rootConnect to MySQL as root without password
4ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';Change root password
5FLUSH PRIVILEGES;Reload privilege tables
6exitExit MySQL client
7sudo systemctl stop mysqlStop MySQL server again
8sudo systemctl start mysqlStart 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.