Install and Configure MariaDB on CentOS Stream 9: An Efficient MySQL Alternative

CentOS tutorial - IT technology blog
CentOS tutorial - IT technology blog

Context and Why MariaDB on CentOS Stream 9 is Needed?

If you’ve worked with Linux for a long time, CentOS is surely no stranger. It’s a free Linux operating system based on the source code of Red Hat Enterprise Linux (RHEL). However, since CentOS shifted from a stable version (CentOS Linux) to a rolling-release version (CentOS Stream), many changes have occurred. This requires us to adapt how we deploy services.

I still have a few servers running CentOS 7, and migrating to AlmaLinux was a challenge I had to tackle. Through that experience, I realized the importance of adapting to new operating systems, as well as efficiently deploying core services like databases.

In this context, MariaDB emerges as a powerful open-source relational database choice, often considered a direct and efficient replacement for MySQL. MariaDB is favored because the original creators of MySQL developed it.

While maintaining high compatibility with MySQL, it still offers many improvements in performance, features, and security. Especially with CentOS Stream 9, using open-source technologies like MariaDB becomes even more suitable, ensuring you have a solid data platform for your applications.

Installing MariaDB Server on CentOS Stream 9

The process of installing MariaDB on CentOS Stream 9 is quite simple thanks to the DNF package manager. First, we’ll update the system, then install the necessary packages.

1. Update the System

This is the first and most crucial step, ensuring your system is always up-to-date with the latest security patches and software packages.


sudo dnf update -y

2. Install MariaDB Server

CentOS Stream 9 provides the MariaDB Server package directly from its default repositories. We just need to run the following command:


sudo dnf install mariadb-server -y

This command will install MariaDB Server along with all necessary dependencies.

3. Start and Enable MariaDB

After installation, we need to start the MariaDB service and configure it to run automatically at system boot.


sudo systemctl start mariadb
sudo systemctl enable mariadb

To check the service status, you can use the command:


sudo systemctl status mariadb

You should see the status as active (running) if everything goes smoothly.

Detailed MariaDB Configuration and Security

After installation, initial configuration and security are extremely important to ensure your database is safe and operates efficiently. We will use the mysql_secure_installation tool and then adjust some basic settings.

1. Run mysql_secure_installation

This tool guides you through basic MariaDB security steps: setting the root password, removing anonymous users, disallowing remote root login, and removing the test database.


sudo mysql_secure_installation

When you run this command, you will be asked a series of questions. Answer as follows:

  • Enter current password for root (enter for none): Press Enter if this is your first time running it.
  • Set root password? [Y/n] Y (Set a password for the root user).
  • Enter the new root password twice.
  • Remove anonymous users? [Y/n] Y (Remove anonymous users).
  • Disallow root login remotely? [Y/n] Y (Disallow remote root login to enhance security).
  • Remove test database and access to it? [Y/n] Y (Remove the test database).
  • Reload privilege tables now? [Y/n] Y (Reload privilege tables).

2. Configure Firewalld

If you have configured firewalld (as I instructed in a previous article), you need to open port 3306 so that other applications or clients can connect to MariaDB. New users often forget this step and wonder why they can’t connect from another machine.


sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --reload

3. Create a New Database and User

Using the root user for applications is not a good security practice. You should create a separate database and user with specific privileges.

Log in to the MariaDB shell with the root user:


sudo mysql -u root -p

Enter the root password you just set. Then, run the following SQL commands to create a database, user, and grant privileges:


CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace myapp_db, myapp_user, and your_strong_password with your desired database name, username, and password. If you want the user to be able to connect from anywhere, you can replace 'localhost' with '%' (not recommended for production environments without additional security).

4. Advanced MariaDB Configuration (Optional)

MariaDB reads configuration files from /etc/my.cnf and files in the /etc/my.cnf.d/ directory. You can create a custom configuration file to adjust server performance or behavior.

To adjust buffer sizes or other settings, you can create or edit the file /etc/my.cnf.d/server.cnf. A good practice is to create a separate configuration file like /etc/my.cnf.d/custom.cnf.


sudo nano /etc/my.cnf.d/custom.cnf

Add the following content to the file:


[mariadb]
innodb_buffer_pool_size = 512M
max_connections = 200
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip-name-resolve

Save the file and restart the MariaDB service for the changes to take effect:


sudo systemctl restart mariadb

Testing and Monitoring MariaDB

After installation and configuration, testing and monitoring are the final steps to ensure MariaDB operates stably and efficiently.

1. Test Database Connection

You can try connecting to the newly created database with the new user to confirm everything is working.


mysql -u myapp_user -p myapp_db

Enter the password for myapp_user. If successful, you will see the MariaDB prompt. You can type SHOW DATABASES; to check or exit; to quit.

2. Check Service Status

Regularly check the service status to ensure MariaDB is running. You can also view error logs to find potential issues.


sudo systemctl status mariadb
sudo journalctl -u mariadb

3. Monitor Performance

MariaDB provides many status variables and tools for performance monitoring. You can view these variables from within the MariaDB shell:


SHOW STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Qcache%';
SHOW VARIABLES LIKE 'max_connections';

For more in-depth monitoring, you might consider third-party tools like Prometheus with an exporter or Zabbix. However, for the purpose of basic initial installation and configuration, this manual checking is sufficient for you to grasp the database’s operational status.

At this point, you have completed the installation and configuration of MariaDB on CentOS Stream 9. MariaDB is now ready to serve your applications. Understanding how system components interact and configuring them appropriately will help you build robust, scalable systems in the future.

Share: