Reset MySQL (or MariaDB) Root Password

Last Updated: Feb. 17th 2022 at 7:28pm Tags: blog linux mysql

This article explains how to reset the database root password.

Have you forgotten your MySQL root password? Well if you are managing a large number of customers, someone will do it eventually.
Also helpful if you are working on a project where the previous developer or server admin has vanished. This article tells you how to reset your password.

You will need root access to the Linux box running the database.

Here are the steps to recover your MySQL password.

Step 1

Log in as root on the Linux server.

sudo su

Step 2

Create a file named mysql-init in your /root directory.

touch /root/mysql-init

Step 3

Place the following in your mysql-init file, be sure to change “myNEWpassword” to your new password:

UPDATE mysql.user SET Password=PASSWORD('myNEWpassword') WHERE User='root';
FLUSH PRIVILEGES;

Note: ensure UPDATE and FLUSH are on their own lines.

Step 4

Turn off your mysql service.

sudo service mysql stop

Step 5

Run your mysql-init

mysqld_safe --user=mysql --init-file=/root/mysql-init &

Note: Make sure the user is set to your mysql user.

Step 6

sudo service mysql restart

After the server has started successfully, delete your mysql-init for security.

rm /root/mysql-init

Reference

Comments

You need to login to comment.