How to reset MYSQL root password on linux

You may have found this article because you have simply forgot the MYSQL root password.  These things happen, therefore do not worry… we have a solution for you.

The MYSQL root account gives you unrestricted access to all of the databases on the server and therefore you should have a secure password.

Using the below steps, you should have the mysql root password reset in just a few minutes!

Don’t forget to leave your comments below if you found this guide helpful and feel free to leave your thoughts/suggestions.

Important notes: This was tested on CentOS 6.5 and MySQL 5.1.73 and should work on UNIX / LINUX / OS X severs, etc.

linux-which-distro

Step 1: Stop MYSQL.

# /etc/init.d/mysqld stop

You should see something like this:

Note: If you get an error message saying “-bash: /etc/init.d/mysqld: No such file or directory”, you can try “mysql” instead of “mysqld”.

Stop-MYSQL-Linux

Step 2: Start MYSQL without a password.

# mysqld_safe –skip-grant-tables &

Once you enter the above command, you should see something like the below screenshot:

mysql-start-without-password

Note: You may need to break out of the above screen to proceed.

Step 3: You can now connect to MySQL without a root password.

# mysql -u root

mysql-login-without-password-root

Step 4: Setup your new MySQL root password.

Remember, it is best to use a SECURE password which is randomly generated and at least 15-20 characters long.

# use mysql;

# update user set password=PASSWORD(“NEW-ROOT-PASSWORD-HERE“) where user=’root’;

# flush privileges;

# quit

linux-commands-to-reset-root-password

Step 5: Stop and restart the MySQL service.

The below command will stop the MYSQL service.  Make sure to wait 5 seconds and then start the service again.

# /etc/init.d/mysqld stop; sleep 5; /etc/init.d/mysqld start;

linux-restart-mysql

Step 6: Test your new password.

Congratulations, you have successfully reset your mysql root password! You can now test it by using the following command:

# mysql -u root -p

When prompted, simply enter your password you created in Step 4.

About the Author

Leave a Reply