How to Reset MySQL Root Password
Introduction
If you’ve forgotten your MySQL root password, you can reset it easily with a few commands. This guide shows you how to recover access to your MySQL server on Linux.
Step-by-step Guide
- Stop the MySQL service
- sudo systemctl stop mysql
- Start MySQL in safe mode (skip grant tables)
- sudo mysqld_safe --skip-grant-tables &n
- Wait a few seconds for MySQL to start.
- sudo mysqld_safe --skip-grant-tables &n
- Log in to MySQL without a password
- mysql -u root
- Change the root password
- For MySQL 5.7+ and MariaDB 10.1+:n
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
- For older versions:n
- FLUSH PRIVILEGES;
- SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
- Replace
newpasswordwith your desired password.
- For MySQL 5.7+ and MariaDB 10.1+:n
- Exit MySQL and restart the service
- exit
- sudo systemctl stop mysql
- sudo systemctl start mysql
- Test the new root password
- mysql -u root -pn
- Enter your new password when prompted.
- mysql -u root -pn
Recommendations
- Use a strong, unique password for the root user.
- Store your credentials securely.
- Regularly back up your databases.
Call To Action
eed a secure VPS for your databases? Check out our hosting plans! https://xernode.com/#pricing