Loading...

Knowledge Base

How to Reset the MySQL Root Password on a CentOS VPS Server

When you need to change or recover the MySQL root password on your CentOS VPS server. This article explains how to reset the MySQL root password.
 
To reset the MySQL root password, the below steps need to be performed via SSH as the root user.

Step 1. Stop mysql:
systemctl stop mysqld

Step 2. Set the mySQL environment option
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

Step 3. Start mysql usig the options you just set
systemctl start mysqld

Step 4. Login as root
mysql -u root

Step 5. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

for MySQL 5.7.6 and later, you should use the below command or you'll get a warning
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Step 6. Stop mysql
systemctl stop mysqld

Step 7. Unset the mySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS

Step 8. Start mysql normally:
systemctl start mysqld

Step 9: Try to login using your new password:
mysql -u root -p

In case of any queries, please feel free to contact our support team which is available 24x7.