Reset Your MariaDB or MySQL Root Password

Today we’re going to learn how to reset the root password of MySQL or MariDB. Let’s get started:

Table of Contents

  1. Stop Database Server
  2. Restart Database Server Without Permission
  3. Change Root Password
  4. Normally Restart Database Server

Stop Database Server

First, we need to shutdown our database server using this command:

# MariaDB
sudo systemctl stop mariadb

# MySQL
sudo systemctl stop mysql

Restart Database Server Without Permission

It will allow you to access the database command line with root privileges without providing a password:

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

Now you’re able to login to database server without providing password:

mysql -u root

Change Root Password

Now let’s change root password:

MariaDB 10.1.20 and newer as well as MySQL 5.7.6 and newer:

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

# if you face any issue, use this command:
UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost'

MariaDB 10.1.19 and newer as well as MySQL 5.7.5 and newer:

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

Don’t forget to replace new_password with your password.

Reload the grant tables using this command:

FLUSH PRIVILEGES;

Normally Restart Database Server

Run this command to restart the database server normally:

# MariaDB
sudo kill `/var/run/mariadb/mariadb.pid`

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

After that restart database service:

# MariaDB
sudo systemctl start mariadb

# MySQL
sudo systemctl start mysql

Now you can login to databaser server using the new password:

mysql -u root -p
That’s all. Thank you. ?

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.