MariaDB is an open-source database management system that is a backwards-compatible replacement for MySQL. It is a multi-threaded relational database management system. MariaDB is maintained and developed by the MariaDB Foundation, which comprises some of the original developers of MySQL.

With CentOS 7, MySQL has been replaced with MariaDB as the default database system. This is commonly used as an alternative for the MySQL portion of the LEMP stack. It can be used as a direct replacement for MySQL.

This tutorial will go through how to install MariaDB on CentOS 7. Note that you can also use the following steps to install MariaDB on CentOS 8 systems.

Prerequisites

Before we get started with the installation process, we will need:

  • A CentOS 7 VPS
  • Access to a root user or a user with Sudo privileges
  • A basic understanding of Linux commands

Install MariaDB from the default repository

You will not get the latest version when you install it from the CentOS default repository. Even though this version is not the latest, it’s stable. To install MariaDB 5.5 on your VPS, follow the steps below:

  • Update your system:

sudo yum update –y

  • Install the MariaDB package from the default CentOS repository using the yum command:

sudo yum install mariadb-server

Here, you will be prompted to continue; press y.

  • The installation will take a few seconds (at least with 1Gbits VPS servers) and once it’s completed, start the MariaDB service. We will also set it to startup during a boot-up using the following commands:

sudo systemctl start mariadb

sudo systemctl enable mariadb

  • To verify the installation, enter the following command, which will show the current status of the MariaDB service:

sudo systemctl status mariadb

The output should show that the service is active and running.

  • Now run the secure installation script.

sudo mysql_secure_installation

You will be prompted to set the root password, remove anonymous users, and other general security patches during the setup. Each step will be explained in detail, and we recommend answering all questions with y. This is meant to increase security for your database server.

  • Restart the MariaDB service

service mysql restart

Once that is done, you have successfully installed MariaDB 5.5 on CentOS 7.

Installing the latest version of MariaDB

If you want to install the latest version of MariaDB on your CentOS 7 VPS, you can do it from the MariaDB repository. The latest version of MariaDB is 10.5 at the time of writing this article, but you can find the latest version by visiting the MariaDB repository page. You can generate the repository file from this page for any specific MariaDB version.

  • Firstly, we will create a repository file called repo using the nano command:

nano mariadb.repo

  • Now add the following content inside the repository file:
# MariaDB 10.5 CentOS repository list - created 2021-05-25 09:24 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-ppc64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Save the file and exit the text editor by pressing ctrl + x > y > Enter.

  • We will install the MariaDB server and client packages using the yum command.

sudo yum install MariaDB-server MariaDB-client

  • Once the installation is complete, start the MariaDB service. We will also set it to startup during a boot-up using the following commands:

sudo systemctl enable mariadb

sudo systemctl start mariadb

  • To verify the installation, enter the following command, which will show the current status of the MariaDB service:

sudo systemctl status mariadb

  • Now run the secure installation script.

sudo mysql_secure_installation

During the setup, you will be prompted to

  1. Change the root password
  2. Remove anonymous users
  3. Disallow root logins remotely
  4. Remove the test database and access to it
  5. Reload privilege tables

Each step will be explained in detail, and we recommend answering with y to all questions mentioned. This is meant to increase security for your database server.

  • Restart the MariaDB service

service mysql restart

Once that is done, you have successfully installed MariaDB 10.5 on CentOS 7.

Connecting to MariaDB from the command line

Now that we have installed MariaDB on our CentOS 7 server, we can connect to it through the terminal. We will log in with the root user we created during the installation.

mysql -u root -p

When prompted, enter the root password that you set during the mysql_secure_installation. You will be presented with the MariaDB shell prompt when you enter the password.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.5.7-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Conclusion

In this guide, we covered the installation of MariaDB on CentOS 7. It can be used as a direct replacement for the SQL server. We added some security measures during the installation process and got it ready to be connected from the command line.

If you liked this tutorial, don’t forget to share it with your colleagues; your feedback is important to us. So let us know in the comments below!

People also read: