Last tutorial discuss about Resetting VPS Root Password. Now in this tutorial showing how to resetting MySQL Password or MariaDB. While updating the Adminer MySQl Password, Forgetting password reaalllyyy so much irritating part while working on it. And It happens to the most of us. If you forget or lose the root password to your MySQL or MariaDB database, you can still access and reset the password if you have access to the server and a sudo-enabled user account.
In this article I am going to cover how to reset the root password for older and newer versions of MySQL and MariaDB
At First run the PuTTY and follow the bellow command carefully
Step 1 — First Identify the Running Database Version
Depending on the database used and its version, you’ll need to use different commands to recover the root password. Here is the command, You can check your version with the following command:
Output for MySQL:
mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Output for MariaDB:
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
Step 2 — Stopping the Database Server
To change the root password, first you have to shut down the database server beforehand. You can do that for MySQL with:
sudo systemctl stop mysql
And for MariaDB wtih:
sudo systemctl stop mariadb
Step 3 — Restarting the Database Server
If you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password. This will allow you to gain access to the database without knowing it.
To do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.
Start the database without loading the grant tables or enabling networking:
sudo mysqld_safe –skip-grant-tables –skip-networking &
here The ampersand(&) at the end of this command will make this process run in the background so you can continue to use your terminal.
For me when I run this command it shows “mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists”. In that case you just need to run those command and the “mysqld” directory will automatically create.
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
Now you can connect to the database as the root user, which will not asked for a password. Now use this Command:
mysql -u root
Now this MySQL Prompt Will Promt
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
Now that you have root access, you can change the root password.
Step 4 — Changing the Root Password
For modern versions of MySQL One simple way to change the root password is using the ALTER USER command. However, this command won’t work right now because the grant tables aren’t loaded.
Let’s tell the database server to reload the grant tables by issuing the FLUSH PRIVILEGES command.
Now we can actually change the root password.
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘new_password’;
For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:
SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘new_password’);
Make sure to replace new_password with your new password of choice.
But sometimes the ALTER USER command does not work. Then it’s usually indicative of a bigger problem. However, you can try UPDATE … SET to reset the root password instead.
UPDATE mysql.user SET authentication_string = PASSWORD(‘new_password’) WHERE User = ‘root’ AND Host = ‘localhost’;
The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.
Step 5 — Restart the Database Server Normally
First, stop the instance of the database server. For stopping the instance Database Server run the command that run in Step 2. This command searches for the PID, or process ID, of MySQL or MariaDB process and sends SIGTERM to tell it to exit smoothly after performing clean-up operations. You can learn more in this Linux process management tutorial.
sudo systemctl stop mysql
Then run below command, For MySQL use:
For MariaDB, use:
Then, restart the service using systemctl.
For MySQL, use:
sudo systemctl start mysql
For MariaDB, use:
sudo systemctl start mariadb
Now you can confirm that the new password has been applied correctly by running:
mysql -u root –p
After running this command the password will reset automatically. You just need to check. Hope so it will work Perfectly..
Here is the overall Screenshot to clear the idea