HOW TO CHANGE MYSQL ROOT PASSWORD IN UBUNTU
Passwords are hard to remember and if you have forgotten the mysql root password or you just want to change /reset it, there is a way. This guide will help you do that :
STEP 1: CHECK THE VERSION OF MYSQL ON UBUNTU 20.04
First of all, check the version of your MYSQL because this post contains a solution for version 8 or higher. If your version is different then the solution will be different..
Check the mysql version using the command:
$ mysql — version
STEP 2: STOP THE MYSQL SERVER
To change the mysql root password , you first need to shut down the MYSQL server and you can do so using the following command.
$ sudo systemctl stop mysql.service
Check the status of te MYSQL server using this command
$ sudo systemctl status mysql.service
STEP 3: SKIP GRANT TABLES AND NETWORKING
To start the MYSQL server without granting the tables and networking check, set the environment variable MYSQLD_OPTS which MYSQL used on start up
$ sudo systemctl set-environment MYSQLD_OPTS=” — skip-networking — skip-grant-tables”
We can now log in to MYSQL without providing any passwords
STEP 4: START THE MYSQL SERVICE
After setting the environment variable MYSQLD_OPTS, start the MYSQL service now using the command:
$ sudo systemctl start mysql.service
STEP 5: CONFIRMING ITS STATUS
$ sudo systemctl status mysql.service
STEP 6: SIGN IN TO MYSQL SHELL
Now we can sign in to MYSQL as a root user using the command without using any password, you will log in to the MYSQL shell
$ sudo mysql -u root
Alter the root password by flushing the privileges
mysql> flush privileges;
Select the mysql database using the command :
mysql> USE mysql
Set the new password using the alter command by typing :
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘the-new-password’;
Then log out from the MYSQL shell using exit command
mysql> exit;
STEP 7: KILL ALL MYSQL PROCESSES AND RESTART THE MYSQL SERVICE
$ sudo killall -u mysql
Then restart MYSQL server using the command below:
$ sudo systemctl restart mysql.service
STEP 8: LOGIN WITH THE NEWLY CREATED PASSWORD
$ sudo mysql -u root -p
The password has been successfully reset and you will be logged successfully into the MYSQL shell.