Set root password MariaDB

Published: December 11, 2018 (Updated: Dec 11, 2018)

Enjoying this content? Subscribe to the Channel!

Secure Your Database Now: How to Set the MariaDB Root Password (And Why You Must!)

Hello and welcome back to Darren’s Tech Tutorials!

If you’ve just finished installing MariaDB on your Linux system—whether it’s CentOS 7, RedHat 7, or even another platform—you might have run into a very common, yet alarming, default behavior: you can log in as the root user without ever being prompted for a password!

Running the simple command mysql -u root -p and hitting Enter should ideally throw an error, but by default, MariaDB often lets you straight into the shell. While convenient for initial setup, this is a massive security risk that needs to be addressed immediately.

In this tutorial, we are going to walk through the essential steps to correct this default setting, set a strong root password, and lock down your database installation. Let’s dive in and make your data secure!


Understanding the MariaDB Default Behavior

Why does MariaDB (or even MySQL, as this solution works there too) do this? Often, modern package installations prioritize functionality over initial security, especially in development environments.

By default, the MariaDB root user is often authenticated via the system socket mechanism (known as unix_socket plugin) rather than a traditional password. If you log in as the system’s root user, the database trusts you implicitly. However, if any other user or process gains access to the system, they could potentially log into your critical database unhindered.

Our goal is to switch this authentication method to the standard mysql_native_password plugin and enforce a secure password.

Step-by-Step Guide: Setting the MariaDB Root Password

Follow these commands carefully to secure your MariaDB installation.

Step 1: Access MariaDB (The Insecure Way)

Since we know the password is not yet set (or we are using socket authentication), we can log in directly to begin the process.

Open your terminal and execute the following command:

mysql -u root -p

When prompted for a password, just press Enter. You should now be at the MariaDB prompt: MariaDB [(none)]>.

Step 2: Set the New Root Password

We will use the powerful ALTER USER command. This is the most current and recommended way to manage user credentials in MariaDB and MySQL.

Crucial Note: Replace 'YourStrongNewPassword!' with a secure, complex password that you will remember!

Enter the following command into the MariaDB prompt:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourStrongNewPassword!';

You should receive a query successful message. This command tells the database to authenticate the root user when connecting from the local machine (localhost) using a traditional password instead of the system socket.

Step 3: Apply Changes and Exit

MariaDB caches permission tables for efficiency. For our password change to take effect immediately, we need to instruct the server to reload those tables using the FLUSH PRIVILEGES command.

FLUSH PRIVILEGES;

Once the privileges are flushed, you can exit the database console:

EXIT;

The system will return you to your Linux shell prompt.

Finalizing Your Security

Step 4: Test Your New Password

This is the moment of truth! Attempt to log back into MariaDB using the same command, but this time, you must enter the password you just set.

mysql -u root -p

When prompted:

Enter password:

Type in your new password (it will not echo to the screen).

If successful, you will be granted access to the MariaDB prompt! If you entered the wrong password, the system will correctly reject the login attempt, confirming that your database is now secured.

Congratulations! You have successfully implemented a necessary layer of security, eliminating the risk of unauthorized root access.

Conclusion and Next Steps

We’ve tackled one of the most critical security oversight steps right after installing MariaDB. By setting the root password, you are ensuring that only authorized individuals (who know the password) can access and modify your database structure.

Security is never a step you should skip, and now you have the foundation set for building secure applications and services on top of your MariaDB installation.

Did this tutorial help you lock down your server? Let us know in the comments below! If you found these steps clear and helpful, please do me a huge favor: Like this post and subscribe to Darren’s Tech Tutorials on YouTube for more accessible, powerful technology guides!