MariaDB/MySQL – Recover root password


Here are commands you need to type for each step (login as the root user):

Step 1.  Stop MariaDB service.

# systemctl stop mariadb

Step 2.  Start MariaDB server w/o password.

# mariadb_safe --skip-grant-tables &
  [1] 5988
  Starting mariadb daemon with databases from /var/lib/mysql
  mariadb_safe[6025]: started

Step 3.  Connect to mariadb server using mysql client:

# mysql -u root
  Welcome to the MariaDB monitor. Commands end with ; or \g.
  Your MariaDB connection id is 2
  Server version: 5.5.44-MariaDB MariaDB Server
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

Step 4.  Setup new MariaDB root user password.

mysql> use mysql;
  mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
  mysql> flush privileges;
  mysql> quit

Step 5.  Stop MariaDB server.

# systemctl stop mariadb
  Stopping MariaDB database server: mariadb
  STOPPING server from pid file /var/run/mysqld/
  mysqld_safe[6186]: ended
  [1]+ Done mysqld_safe --skip-grant-tables

Step 6.  Start MariaDB server and test it.

# systemctl start mariadb
  # mysql -u root -p