Resetting lost MySQL password

From Cosmin's Wiki

Jump to: navigation, search

Home > MySQL > Resetting lost MySQL password




If you have lost the root mysql password, but do have the root *nix password, here's how you can change the root password for MySQL.

Please note that the commands below are for Debian-based distributions

First, stop mysql:

 
sudo /etc/init.d/mysql stop
 

Now enter in safe mode:

 
sudo mysqld_safe --skip-grant-tables &
 

Note: The ampersand (&) at the end of the command is required.

Login now to mysql:

 
mysql -u root
 

 
USE mysql;
 
UPDATE user SET password=PASSWORD("mynewpassword") WHERE User='root';
 
FLUSH privileges;
 
quit
 

Now all you need to do is stop and restart MySQL and you'll be able to log in with the new root password.

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