Maria DB – How to reset root password ?

If you come across a situation where you do not remember to master password (root password) of Maria DB then following simple steps will be helpful to reset the root password.

  1. First stop MariaDB Server.
    # systemctl stop mariadb
  2. Edit /etc/my.cnf file and add following line (skip-grant-tables) in [mysqld] section as given below.
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    skip-grant-tables
  3. Now start MariaDB. It will open without any password.
    # systemctl start mariadb
  4. Login to MariaDB as below.
    # mysql
    Welcome to the MariaDB monitor. Commands end with ; or \g.
    Your MariaDB connection id is 96823
    Server version: 5.5.60-MariaDB MariaDB Server
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]>
  5. Now set mysql root password using following commands.
    MariaDB [(none)]> use mysql ;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    Database changed
    MariaDB [mysql]> update user set password=password('123') where user='root' ;
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 4 Changed: 0 Warnings: 0
    
    MariaDB [mysql]> flush privileges ;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [mysql]> quit ;
    #
  6. Now edit /etc/my.cnf file again and remove line (skip-grant-tables) in [mysqld] section.
  7. Now it’s time to restart MariaDB server again.
    # systemctl restart mariadb

You have done it !