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';
MariaDB [(none)]> FLUSH PRIVILEGES;

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
Summary

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!

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
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.

6 Comments

Leave a Reply
    • @Mushtaq,

      I’m sorry to hear you’re experiencing issues with MariaDB. Can you please provide more details about the problem you’re facing? Specific error messages, the version of MariaDB you’re using, and any recent changes you made can be helpful in diagnosing the issue.

      I’ll do my best to assist you.

      Reply
  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.

    Reply
  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.

    Cheers

    Reply
  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! :-)

    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.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.