How to reset root password in MySQL

If you forgot the root password in MySQL, and would like to reset it, do the following as root.  In this example, replace "NewPassword" with your own password.

First create a MySQL script in /tmp/run as follows.

$ sudo /etc/init.d/mysql stop
$ vi /tmp/run
UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;

Then run mysqld_safe pointing to the script you created.

$ sudo mysqld_safe --init-file=/tmp/run &
$ sudo /etc/init.d/mysql restart
$ rm /tmp/run

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *