How to reset MySQL root password using terminal in ubuntu 20.04

If for some reason you forget the MySQL root password, there is still a chance to recover the root password of your database. This post will help you change your MySQL password just follow these easy steps.

Step 1

Check your MySQL version. Thi solution might be different if you use a different version.

For this tutorial, I use version 8.0.28

Step 2

Stop the MySQL server. We need to stop the MySQL server first to proceed with changing the password. To do this type this command:

  • sudo systemctl stop mysql.service

To check if the MySQL server is stop, enter this command:

  • sudo systemctl status mysql.service

Step 3

Set the environment variable MYSQLD_OPTS to start the MySQL server without granting the tables

  • sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"

Step 4

After we set the MYSQLD_OPTS, we need to start the MySQL service. Type this command:

  • sudo systemctl start mysql.service

To check if the service is running successfully, run this command:

  • sudo systemctl status mysql.service

Step 5

Open your MySQL shell by typing this command:

  • mysql -u root

You should successfully enter the MySQL shell without entering a password

Step 6

Flush all the privileges and alter the root password

  • flush privileges;
  • USE mysql;
  • ALTER USER 'root'@'localhost' IDENTIFIED BY 'the-new-password-here';
  • quit;

Step 7

Kill all the processes of MySQL before even restarting the MySQL server.

  • sudo killall -u mysql

Then restart again your MySQL server

  • sudo systemctl restart mysql.service

Step 8

Try to open the mysql shell and enter your new password

  • sudo mysql -u root -p

You have now successfully changed the root password of your MySQL database.


Leave a Comment

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