Easy Steps to Reset MariaDB or Mysql Password in Linux
by Prashant ·

In an unlucky situation of forgetting or losing Mysql root password and you will have to recover it somehow. Mysql stores the password in the user’s table.
That means we need to check out a way to skip the Mysql authentication so that we can change or update the Mysql Password.
In this tutorial, we are going to see some easy steps that help to Reset the Mysql or MariaDB Password.
If you are facing this issue in the production or development environment then make sure to take a full backup of existing databases.
Also, you require the downtime to update the Mysql password because we are going to stop/start the Mysql service.
Although we will use a Mysql server in this article, the instructions should work for MariaDB as well.
Reset Mysql or MariaDB Password
Step 1: Stop the MySQL/MariaDB service
Changing Or Resetting the password in Mysql requires stopping the Mysql service first.
------------- SystemD ------------- # systemctl stop mysql ------------- SysVinit ------------- # /etc/init.d/mysql stop
2. Start Mysql with –skip-grant-tables
Now start the database server without loading the grant tables, Run the following command,
------------- SystemD ------------- # systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" # systemctl start mysql # systemctl status mysql ------------- SysVinit ------------- # mysqld_safe --skip-grant-tables &

3. Log in to the MySQL shell
Now you can connect to the database server as the root user without password,
# mysql -u root
4. Set a new root password
When you are successfully able to access MySQL with root user.
Run the following command to reset the new password for the root user,
MySQL > SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MY_NEW_PASSWORD');
MySQL > FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
5. Stop and Start the database server normally
Finally, stop the service, unset the environment variable, and start the service normally once again:
# mysqladmin -u root -p shutdown
# systemctl start mysql
Check the password is working or not [It will work for sure :)],
# mysql -u root -p
In this article, we have discussed how to reset the MariaDB / MySQL root password. As always, feel free to use the comment form below to drop us a note if you have any questions or feedback. We look forward to hearing from you! Linuxgrow.com
Thank You 🙂
Awesome explanation
Thank You 🙂