MySQL: recover lost root password

Log in as root and stop the mysql daemon.

service mysqld stop

Then start up the mysql daemon and skip the grant tables which stores the passwords and other priviledge information

mysqld_safe --skip-grant-tables &

Now you should be able to connect to mysql without a password.

mysql --user=root mysql
mysql> update user set Password=PASSWORD('new password') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit

and then restart the mysql daemon (it will stop the mysql-safe) as you would normally do

service mysqld start

Leave a Reply

Your email address will not be published. Required fields are marked *