Reset Root Password

Updated on 03 Oct 2020

First Stop MySQL

First things first, you will need to stop the MySQL server.

sudo systemctl stop mysql

Reset root password - MySql 5.7

You can reset the root password with the following steps.

sudo mysqld_safe --skip-grant-tables &
sudo mysql --user=root mysql

mysql> update user 
       set authentication_string=PASSWORD("new-password"),
	       plugin="mysql_native_password"    
       where user='root';

       flush privileges;
       quit

sudo systemctl restart mysql

Socket Error

There is a possibility that you’ll encounter a UNIX socket file don't exists error when you perform the following step.

sudo mysqld_safe --skip-grant-tables &

MySQL creates the /var/run/mysqld directory when it runs, and removes it when MySQL is shutdown. The error is most likely because the directory doesn’t exist; so we can create the directory and assign the appropriate www-data owner & group to it.

sudo mkdir /var/run/mysqld
sudo chown www-data:www-data /var/run/mysqld

Now when you attempt to run sudo mysqld_safe --skip-grant-tables & we’ll hopefully not encounter any issues, and we’ll get something similar to what is shown.

Reset root password - MySql 8

Similar steps as before, but we are not updating the tables directly - but rather using the alter user command.

sudo mysqld_safe --skip-grant-tables &
sudo mysql --user=root mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';

       flush privileges;
       quit

sudo systemctl restart mysql