Last Updated: February 25, 2016
· ieldanr
Daniel rios small

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.

Say Thanks

1 Response
Add your response


Pretty rad. I'm actually going to write this down because I lost a local root account a little while ago.

over 1 year ago ·