Recover MySQL Root Password

We need to first stop MySQL:

/etc/init.d/mysql stop

Now start MySQL without password like so:

mysqld_safe --skip-grant-tables;

Now connect to MySQL:

mysql -u root

At the mysql prompt, change to mysql database and update the root password like so:

mysql> use mysql;
mysql> update user set password=PASSWORD('new_password') where user='root';
mysql> flush privileges;
mysql> quit

Where ‘new_password‘ is the new root password for MySQL server. Flush Privileges is needed to making the password change effect immediately.

Restart MySQL:

/etc/init.d/mysql stop
/etc/init.d/mysql start

Now test the new password:

mysql -u root -p

That’s it.