Skip to Content

How to reset mysql or Mariadb root password ?

To reset the MySQL or MariaDB password, 

You need to Stop the MySQL/MariaDB service and Start the service with the "--skip-grant-tables" option. Later Connect to the MySQL/MariaDB server and update the password for the user.


How to reset MySQL or MariaDB root password?

1.  First you need to stop MySQL or MariaDB server.

root@localhost:# service mariadb stop   

root@localhost:~#   service mysql stop   

 2. Start the MySQL/MariaDB server without loading the grant tables.

root@localhost:~# mysqld_safe --skip-grant-tables &

  When the --skip-grant-tables option is used, anyone can connect to the database server without a password and with all privileges granted. 

 3. Log in to the Mysql or MariaDB shell

root@localhost:~# mysql -u root

 4. Set a new password.

Type the following command, replacing "new-password"  with the new root password

MariaDB [(none)]> UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';

MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> exit;

In old version of mariadb run the following command. Replace 'new password' with the new root password

MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

 5. Stop the MySQL server using the following command. You will be prompted to enter the new MySQL root password before the MySQL server shuts down:

root@localhost:~# mysqladmin -u root -p shutdown

Make sure to kill all the running mariadb process (pkill).

 6. Start the MySQL/MariaDB server normally

root@localhost:~# service mariadb start

7. Once started you can check the status of MariaDB service using service mariadb status command as shown below:

root@localhost:~#service mariadb status

8. Log in to MariaDB with New Password

Test the new reset MariaDB root password by logging in to the Server using mysql -u root -p command as shown below. Once you provide the root password it should allow you to log in just the way it is described below.

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>