How to Reset MySQL or MariaDB Root Password in Linux

If you are setting up a MySQL or MariaDB database server for the first time, chances are you will be running mysql_secure_installation soon afterwards to implement basic security settings.

One of these settings is the password for the database root account – which you must keep private and use only when strictly required. If you forget the password or need to reset it (for example, when a database administrator changes roles – or is laid off!).

Suggested Read: Change MySQL or MariaDB Root Password

This article will come in handy. We will explain how to reset or recover forgottent MySQL or MariaDB root password in Linux.

Although we will use a MariaDB server in this article, the instructions should work for MySQL as well.

Recover MySQL or MariaDB root Password

To begin, stop the database service and check the service status, we should see the environment variable we set previously:

------------- SystemD ------------- 
# systemctl stop mariadb

------------- SysVinit -------------
# /etc/init.d/mysqld stop

Next, start the service with --skip-grant-tables:

------------- SystemD ------------- 
# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mariadb
# systemctl status mariadb

------------- SysVinit -------------
# mysqld_safe --skip-grant-tables &
Start MySQL/MariaDB with Skip Tables
Start MySQL/MariaDB with Skip Tables

This will allow you to connect to the database server as root without a password (you may need to switch to a different terminal to do so):

# mysql -u root

From then on, follow the steps outlined below.

MariaDB [(none)]> USE mysql;
MariaDB [(none)]> UPDATE user SET password=PASSWORD('YourNewPasswordHere') WHERE User='root' AND Host = 'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;

Finally, stop the service, unset the environment variable and start the service once again:

------------- SystemD ------------- 
# systemctl stop mariadb
# systemctl unset-environment MYSQLD_OPTS
# systemctl start mariadb

------------- SysVinit -------------
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

This will cause the previous changes to take effect, allowing you to connect to the database server using the new password.

Summary

In this article we have discussed how to reset the MariaDB / MySQL root password. As always, feel free to use the comment form below to drop us a note if you have any questions or feedback. We look forward to hearing from you!

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Gabriel Cánepa
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

8 Comments

Leave a Reply
  1. CGroup: /system.slice/mysqld.service
    ├─14887 /bin/sh /usr/bin/mysqld_safe –basedir=/usr
    └─15058 /usr/sbin/mysqld –basedir=/usr –datadir=/var/lib/mysql –plugin-dir=/usr/lib64/mysql/plugin –log-error=/var/log/mysqld.log –pid-file=/var/run/…

    I am getting this, and from a different terminal, it still says access denied for root. Please advice

    Reply
        • @Arun,

          To reset root password in MySQL 8 version, use the followinig instructions.

          1. First, stop the MySQL server and then restart it with the –skip-grant-tables option as shown.

          # systemctl stop mariadb
          # systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
          # systemctl start mariadb
          

          2. In the mysql client shell, tell the server to reload the grant tables so that account-management statements work.

          mysql> FLUSH PRIVILEGES;
          

          3. Then change the ‘root’@’localhost’ account password. Make sure to replace the password with the password that you want to use.

          mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
          

          4. Now stop the server, unset (the –skip-grant-tables) and restart the service.

          # systemctl stop mariadb
          # systemctl unset-environment MYSQLD_OPTS
          # systemctl start mariadb
          
          Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.