CentOS 7 Reset Your MySQL or MariaDB Root Password

Today I am going to share how to reset root password of MySQL/MariaDB.

Table of Contents

  1. Stop Database Server
  2. Change the Root Password
  3. Restart the Database Server
  4. Test the Password

Step 1 : Stop Database Server

If you don’t know the database server (MySQL or MariaDB) of your server then you can see by typing this command:

mysql --version

To reset the root password, we need to stop the database server. Let’s do this by hitting this command:

# MySQL
sudo systemctl stop mysql
#MariaDB
sudo systemctl stop mariadb

Step 2 : Change the Root Password

Start the database server without loading the grant tables:

sudo mysqld_safe --skip-grant-tables --skip-networking &

Now login to MySQL server as root. It should not ask for password.

mysql -u root

After successfully login, type this command:

FLUSH PRIVILEGES;

Now we are going to change the root password.

For MySQL 5.7.6, MariaDB 10.1.20 and newer version:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW_PASSWORD';

For MySQL 5.7.5, MariaDB 10.1.20 and older version:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NEW_PASSWORD');

NEW_PASSWORD should be your new password.

Step 3 : Restart the Database Server

After performing clean-up operations, we should run this command to exit smoothly:

# MySQL
sudo kill `cat /var/run/mysqld/mysqld.pid`
#MariaDB
sudo kill `/var/run/mariadb/mariadb.pid`

Now restart the database server:

# MySQL
sudo systemctl start mysql
#MariaDB
sudo systemctl start mariadb

Step 4 : Test the Password

We have successfully changed the root password. Now we are going to check the password is changed or not. Run this command:

mysql -u root -p

After hitting this, you will be asked for password. Now enter the newly changed password and you should gain access to the database server.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.