Some time we are facing issue like we forgot the MySQL root password. So here we are providing just some simple steps to reset the MySQL root password.
Stop the MySQL service
1 2 3 4 5 | # service mysqld stop or # /etc/init.d/mysqld stop |
Note :- In Redhat-7 or Centos-7 use below command to stop the service.
1 | # systemctl stop mysqld\ |
Start the mysql service with –skip-grant-tables option
1 | # mysqld_safe --skip-grant-tables & |
Note:- If above command does not work in your environment then put skip-grant-tables into the my.cnf, because in some Linux platform mysqld_safe will not work.
Login into the MySQL with blank password
1 | mysql> mysql -u root -p |
Update the root password
1 | mysql> use mysql; |
1 | mysql> update user set password=PASSWORD('new_root_password') where user='root' and host='localhost'; [Before: MySQL 5.7] |
1 | mysql> update user set authentication_string = PASSWORD('new_root_password'), password_expired = 'N' where user = 'root' and host = 'localhost'; [ From: MySQL 5.7.+] |
1 | mysql> flush privileges; |
1 | mysql> \q |
Stop the MySQL service
1 2 3 4 5 | # service mysqld stop or # /etc/init.d/mysqld stop |
Note :- In redhat-7 or Centos-7 use below command to stop the service.
1 | # systemctl stop mysqld |
Start the service again.
Note:- If you have put entry of skip-grant-tables into the my.cnf file then first remove this variable, before starting the MySQL service.
1 2 3 4 5 | # service mysqld start or # /etc/init.d/mysqld start |
Note :- In redhat-7 or Centos-7 use below command to start the service.
1 | # systemctl start mysqld |
Login with the updated password.
1 | # mysql -u root -p 'new_root_password' |