Do you forget your MySql root password? If yes! Then don't worry, this article will help you to reset a MySQL Root password.
The command line of this article is for the MySQL 8 or higher version. I have tested these commands on ubuntu 20.04 LTS. Commands may work on other Ubuntu versions too.
So, First, we need to check the version of your MySQL.
Check the MySql version
mysql --version
Execute the following command to Stop the MySQL server (We need to stop the MySQL in order to change the password)
sudo systemctl stop mysql.service
You can apply the following command to check whether the MySQL server stopped or not
sudo systemctl status mysql.service
Enter ctrl+C to return to the command prompt
Now start MySQL server manually without permission networking checks and set “MYSQLD_OPTS” environment variable
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"
Start the MySQL service
sudo systemctl start mysql.service
You can apply the following command to check whether the MySQL server started or not
sudo systemctl status mysql.service
Enter ctrl+C to return to the command prompt
Now you are able to login Into the MySQL shell without a password
sudo mysql -u root
Apply the below command to Flush the privileges
flush privileges;
Now Select the MySQL database
USE mysql
Apply the following command to set the new password for root user
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW_PASSWORD';
Enter your new password in the place of ''NEW_PASSWORD''
Exit from the MySQL database
quit;
Now we need to unset the "MYAQLD_OPTS" environment variable which we had set earlier.
sudo systemctl unset-environment MYSQLD_OPTS
Revert the updated configuration of the MySql system
sudo systemctl revert mysql
Now Kill the all MySql process
sudo killall -u mysql
(Hit Enter to get back to the command prompt)
Restart the MySql Server normally
sudo systemctl restart mysql.service
Now log in with the recently updated password
sudo mysql -u root -p
0 Comments