Recover MySQL 5.7 root Password in Redhat/CentOS 7

You can recover MySQL 5.7 root Password with following 9  easy steps. These commands will work in Redhat/CentOS 7

Step-1: Stop MySQL server

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('YourNewPassword')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

 

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

 

You can download the script from MySQL password recovery.  Follow the steps to recover password automatically

Step-1: Give execution permission to the scripts

chmod +x mysql_password_recovery.sh

Step-2 : Run the script

./mysql_password_recovery.sh YourNewPassword

One thought on “Recover MySQL 5.7 root Password in Redhat/CentOS 7

  1. Instead of the UPDATE command I used the ALTER command from the MySQL Developers website. The UPDATE command did not work for me.
    Thanks for this article!

Comments are closed.