In this comprehensive guide, I will walk you through the exact steps I use to deploy MariaDB. We will move beyond simple command execution and dive into repository management, security protocols, and configuration best practices that ensure your database isn’t just “running,” but performing optimally.
How to Start MariaDB in CentOS 7
Why MariaDB is the Choice for CentOS 7
Before we dive into the terminal, it is important to understand why we are using this specific pairing. When Red Hat Enterprise Linux (RHEL) 7—and by extension, CentOS 7—was released, the decision was made to drop MySQL in favor of MariaDB as the default database system.
I have found that MariaDB offers several distinct advantages for system administrators:
- True Open Source: Unlike MySQL, which is owned by Oracle, MariaDB remains purely community-driven.
- Performance: In my benchmarks, MariaDB often provides faster replication and better query execution speeds thanks to its optimized query optimizer.
- Storage Engines: It supports a wider variety of storage engines (like XtraDB) out of the box.
Prerequisites for Installation
Before I begin any database installation, I always ensure the environment is prepped correctly. To follow along with this tutorial, you should have the following:
- A CentOS 7 Server: This can be a physical dedicated server, a VPS (like AWS EC2 or DigitalOcean), or a local virtual machine.
- Root or Sudo Access: You cannot modify system services without administrative privileges.
- SSH Access: I assume you are connecting to your server remotely via a terminal.
Step 1: Pre-Installation System Check
One of the most common mistakes I see junior administrators make is attempting to install a database without checking for existing conflicts. CentOS 7 may come with older versions of mariadb-libs or conflicting mysql-libs.
First, I update the system packages to ensure stability.
Bash
sudo yum update -y
Next, I check if any existing database packages are installed.
Bash
rpm -qa | grep -i mariadb
If you see an output, it means a version is already present. For the purpose of this guide, I will assume we are performing a fresh installation or upgrading to a specific version via a repository.
Step 2: Configuring the MariaDB YUM Repository
While you can install MariaDB directly from the base CentOS repositories, I rarely recommend this for production. The base repository often holds MariaDB version 5.5, which is significantly outdated. To get the latest stable version (such as 10.5, 10.6, or 10.11), we need to manually configure the YUM repository.
I prefer to create a custom repository file. Here is how I set it up:
- Open or create the repository file using a text editor like
nanoorvi.
Bash
sudo vi /etc/yum.repos.d/MariaDB.repo
- Paste the following configuration into the file.
Ini, TOML
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.6/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Note: You can change “10.6” to whichever version matches your specific application requirements.
Step 3: Installing MariaDB Server and Client
With the repository active, the installation process is straightforward. I use the yum package manager to handle the installation and dependency resolution.
Execute the following command:
Bash
sudo yum install MariaDB-server MariaDB-client -y
I added the -y flag to automatically answer “yes” to the prompts. This process will download the binary files, setup the default configuration paths, and create the mysql user and group on your Linux system.
Once the installation completes, check the version to verify that the repository worked as expected:
Bash
mysql --version
Step 4: How to Start MariaDB in CentOS 7
Now we arrive at the core of this tutorial. Installing the software does not mean it is running. In CentOS 7, service management is handled by systemd.
To start the MariaDB service immediately, I use the following command:
Bash
sudo systemctl start mariadb
If the command prompts no output, that is usually good news—it means the command executed without throwing an error.
Verifying the Service Status
I always verify that the service is actually active and running. Use the status command to get a detailed report:
Bash
sudo systemctl status mariadb
You are looking for a specific line in the output:
Active: active (running)
If you see Active: inactive (dead) or failed, you will need to check the logs, which I will cover in the troubleshooting section later.
Step 5: Enabling MariaDB on System Boot
Starting the service manually is fine for a one-time session, but servers reboot. Whether it is for kernel updates or an unexpected power cycle, you need your database to come back online automatically.
To ensure MariaDB starts whenever the operating system boots up, I run:
Bash
sudo systemctl enable mariadb
You should see an output indicating that a symlink has been created from /etc/systemd/system/multi-user.target.wants/ to the service unit. This confirms the persistence configuration is active.
Step 6: Securing the Database Installation
Out of the box, MariaDB usually has an empty root password and includes a test database accessible by anonymous users. This is a massive security risk. Thankfully, MariaDB includes a built-in security script that I run on every single server I provision.
Run the secure installation script:
Bash
sudo mysql_secure_installation
This interactive script will walk you through several prompts. Here is how I typically answer them for a secure setup:
- Enter current password for root: (Press Enter, as there is none yet).
- Switch to unix_socket authentication: (Type
n. I prefer standard password authentication for general setups, though sockets are secure). - Change the root password? (Type
yand create a strong, complex password). - Remove anonymous users? (Type
y. This is critical). - Disallow root login remotely? (Type
y. You should only log in as root fromlocalhost). - Remove test database and access to it? (Type
y). - Reload privilege tables now? (Type
y).
Once this is done, your database is hardened against the most common attack vectors.
Step 7: Configuring the Firewall
If your application resides on the same server as the database (localhost), you might not need to open external ports. However, if you are connecting from a remote application server or using a GUI tool like MySQL Workbench from your workstation, you must configure the firewall.
CentOS 7 uses firewalld. By default, it blocks external access to the database port (3306).
To open the port, I use these commands:
Bash
sudo firewall-cmd --permanent --zone=public --add-service=mysql
sudo firewall-cmd --reload
Warning: Opening port 3306 to the public internet is risky. I recommend scoping this rule to specific IP addresses if possible, using the --add-source flag.
Deep Dive: Managing Configuration Files
Starting the service is Step 1; tuning it is Step 2. The primary configuration file for MariaDB in CentOS 7 is located at /etc/my.cnf.
I typically make a few adjustments here depending on the server’s RAM. To edit this, use your text editor:
Bash
sudo vi /etc/my.cnf
Here are the key variables I look at:
bind-address: Defaults to 0.0.0.0 (all interfaces) or 127.0.0.1 (localhost). I ensure this is set correctly based on where the app server connects from.max_connections: If I expect high traffic, I increase this from the default (often 151) to something like 500, provided the RAM can handle it.innodb_buffer_pool_size: This is the most critical performance setting. I generally set this to about 70-80% of available RAM on a dedicated database server.
After making any changes to this file, you must restart the service for them to take effect:
Bash
sudo systemctl restart mariadb
Troubleshooting Common Start-up Issues
Even with a perfect tutorial, things can go wrong. Here are the two most common issues I encounter when trying to start MariaDB on CentOS 7.
1. The “Failed to Start” Error
If systemctl start mariadb fails, my first step is always to check the detailed logs.
Bash
journalctl -xe
Or check the MariaDB error log specifically:
Bash
sudo tail -f /var/log/mariadb/mariadb.log
Common culprits include:
- Permissions: The
/var/lib/mysqldirectory must be owned by themysqluser. - Configuration Syntax: A typo in
/etc/my.cnfwill prevent startup. - Disk Space: If the partition is full, the database cannot create lock files.
2. Socket Connection Errors
If the service is running but you cannot log in, receiving an error like Can't connect to local MySQL server through socket, it usually means the service crashed or the socket file is missing. A simple restart usually fixes this:
Bash
sudo systemctl restart mariadb
Conclusion
Managing databases is a core responsibility for any system administrator working with CentOS 7 environments. While the transition from MySQL to MariaDB might seem subtle, the performance benefits and open-source nature of MariaDB make it a superior choice for modern infrastructure.
By following this guide, you have not only installed the software but also secured it, configured the repository for updates, and set it up to handle reboots automatically. These are the foundational steps that separate a messy development environment.
You may also like the following articles:
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.