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';

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.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

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