Reset MySQL root password in Ubuntu Linux

1. Stop MySQL

sudo /etc/init.d/mysql stop

2. Start MySQL with the --skip-grant-tables option

Ensure the directory /var/run/mysqld exists and correct owner set.

sudo mkdir /var/run/mysqld 
sudo chown mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables &

3. Login to MySQL as root

This will login without a password now

sudo mysql --user=root mysql

4a. For MySQL 8 – Reset Root Password

UPDATE mysql.user SET authentication_string=null WHERE User='root';
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here';
flush privileges;
exit

4b. For MySQL 5.7 – Reset Root Password

UPDATE user SET authentication_string=PASSWORD('your_password_here') WHERE user='root';
Change the auth plugin to mysql_native_password.
UPDATE user SET plugin="mysql_native_password" WHERE User='root';
flush privileges;
exit

4c. MySQL 5.6 – Reset Root Password

UPDATE user SET Password=PASSWORD('your_password_here') WHERE user='root';
Change the auth plugin to mysql_native_password.
UPDATE user SET plugin="mysql_native_password" WHERE User='root';
flush privileges;
exit

5. Stop all MySQL processes before starting the service again.

sudo killall -u mysql

6. Restart MySQL again.

sudo /etc/init.d/mysql start

Login to MySQL and test the password.

sudo mysql -u root -p

Taken from: https://devanswers.co/how-to-reset-mysql-root-password-ubuntu/?unapproved=70874&moderation-hash=0bc48506f2da3157a026fd37d2ae4a43#comment-70874

Leave a Reply