MySQL is one of the world's most popular open-source databases. It is mainly used on many database-enabled websites, primarily for being free and powerful.

If you happen to lose your MySQL root password, you can follow these steps to reset it.

Run MySQL in safe mode

To run MySQL in safe mode, you will first need to stop all the running instances and start it back in safe mode. It can be achieved by running the following commands;

sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables

Login to MySQL as root

You can now login as root user without a password. You can use the following command to do that.

mysql -u root mysql

Set new password

Once logged in as root, you can now set a new password for your root account by running these commands at your MySQL console.

mysql> update user set Password=PASSWORD('desiredpassword') where User = 'root';
mysql> flush privileges;

Change desiredpassword to the password you want to set your root account to.

Kill the current MySQL daemon via the following command and (re)start MySQL service.

sudo killall mysqld
Discuss the article:

Comment anonymously. Login not required.