How To Reset/Recover/Crack Your MySql Root Password? On CentOs/Redhat – 7

Whenever you forget your MySql password, it’s a disaster and really important because data is everything. Apart from this, you have got many more reasons to reset your CentOs/RHEL password like, forgetting after installation or missed with handling lots of passwords.

Well, this tutorial will help you reset/break MySQL root password and by the end of this article, you will be able to reset the MySql root password on your RedHat/CentOS 6/7.

Scenario:

Server IP: 172.31.2.215
OS:
CentOS/RHEL 6/7

Note: If you are a SUDO user then prefix every command with sudo, like #sudo ifconfig

Note: systemd is now used to look after mySQL instead of mysqld_safe

STEP 1: Stop MySql Service

use below command to stop MySQL service

[root@ip-172-31-2-215 ~]# systemctl stop mysqld



STEP 2: Set Temp MySql Environment

we need to set a temporary MySQL environment for skipping up table grants in MySQL servers.

[root@ip-172-31-2-215 ~]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

STEP 3: Start MySql Service

now start the MySQL server with provided MySQL environment

[root@ip-172-31-2-215 ~]# systemctl start mysqld

STEP 4: Login As root

Now you need to login into MySQL server and it should be done as soon as possible or MySQL server should not be online as it is not secure.

[root@ip-172-31-2-215 ~]#  mysql -u root

STEP 5: Reset Password

Now Update  user password with these MySQL commands

for MySQL version below 5.7

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('YourPassword') WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

for 5.7.6 and later

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourPassword';
mysql> FLUSH PRIVILEGES;
mysql> quit



STEP 6: Now Stop MySql

Now after logging out, stop MySql and unset the environment

[root@ip-172-31-2-215 ~]# systemctl stop mysqld

STEP 7: Unset Environment

Unset the MySql environment option so it starts normally next time

[root@ip-172-31-2-215 ~]# systemctl unset-environment MYSQLD_OPTS

STEP 8: Now Start Mysql

after all these steps performed, Start MySQL normally using the below command.

[root@ip-172-31-2-215 ~]# systemctl start mysqld

Now you may log in to MySql using your new password:

[root@ip-172-31-2-215 ~]# mysql -u root -p
Enter password: 
MariaDB [(none)]>

Thanks for reading and keep learning.