How to reset MySQL root password if lost

Stop the MySQL daemon:

$ /etc/init.d/mysqld stop 

Create a file in your home dir called init, with your prefered shell editor (mine is vim):

$ vi /home/aa/init

Insert the following SQL:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewPassword');

Start MySQL with the mysqld_safe command with the --init-file parameter, pointed at your new init-file.

$ mysqld_safe --init-file=/home/user/mysql-init &

MySQL will now start up in the background, with the init file, changing your root password.

Try to login with your new password:

$ mysql -u root -p NewPassword

Kill the mysqld_safe process, and start MySQL as a service again:

$ /etc/init.d/mysqld start

Your password has been changed.

comments powered by Disqus