Last Updated: December 22, 2018
· ieldanr

Recover root password in MySql

Forgot the root password for a mysql database server?

One easy way to recover it without restarting the server, is to read the mysql command history. This is because most people write the database passwords in plain text and won't delete the command history.

cat /root/.mysql_history|more

If you are lucky, you will find something like this.

set password = password('r00tPass');

Where r00tPass is the root user password.

Now, if you are unlucky and the command history was deleted, you will need to stop the server. Follow these steps:

Stop the mysql server

/etc/init.d/mysql stop

Start the mysql server without passwords

mysqld_safe --skip-grant-tables &

Connect to the mysql server

mysql -u root

Set up a new root password

use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;

Stop the mysql server

/etc/init.d/mysql stop

Start the mysql server and login with your new root password

/etc/init.d/mysql start
mysql -u root -p

Hope this helps.