How to Change Root Password of MySQL or MariaDB in Linux

If you’re installing MySQL or MariaDB in Linux for the first time, chances are you will be executing mysql_secure_installation script to secure your MySQL installation with basic settings.

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

Suggested Read: Recover MySQL or MariaDB Root Password in Linux

This article will come in handy. We will explain how to change a root password of MySQL or MariaDB database server in Linux.

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

Change MySQL or MariaDB Root Password

You know the root password and want to reset it, in this case, let’s make sure MariaDB is running:

------------- CentOS/RHEL 7 and Fedora 22+ ------------- 
# systemctl is-active mariadb

------------- CentOS/RHEL 6 and Fedora -------------
# /etc/init.d/mysqld status
Check MySQL Status
Check MySQL Status

If the above command does not return the word active as output or its stopped, you will need to start the database service before proceeding:

------------- CentOS/RHEL 7 and Fedora 22+ ------------- 
# systemctl start mariadb

------------- CentOS/RHEL 6 and Fedora -------------
# /etc/init.d/mysqld start

Next, we will login to the database server as root:

# mysql -u root -p

For compatibility across versions, we will use the following statement to update the user table in the mysql database. Note that you need to replace YourPasswordHere with the new password you have chosen for root.

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

To validate, exit your current MariaDB session by typing.

MariaDB [(none)]> exit;

and then press Enter. You should now be able to connect to the server using the new password.

Change MySQL/MariaDB Root Password
Change MySQL/MariaDB Root Password

In this article we have explained how to change the MariaDB / MySQL root password – whether you know the current one or not.

As always, feel free to drop us a note if you have any questions or feedback using our comment form below. We look forward to hearing from you!

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

4 thoughts on “How to Change Root Password of MySQL or MariaDB in Linux”

  1. If I want to change the root password, it most likely means that I actually forgot it. This article is kind of useless in that regard.

  2. Keep in mind that if the database is configured to be accessed outside the localhost, the Host = ‘localhost’ condition part is not going to match. Keep it in mind and remove it if need.


  3. Hi, a great snippet of “best practice” for a sysadmin.

    Just a suggestion about “To validate, exit your current MariaDB session by typing ‘exit””.. let me tell you I’ll suggest checking if the new root PWD is valid ON ANOTHER SESSION, when the previous one is still running, so you could recover easily a typo or mistake if the new PWD is not what you think it should be. sometimes it happens! :-)


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.