Introduction

For CentOS 7 users, MariaDB is the default database system as it replaced MySQL as the default database system.

The original MySQL developers designed MariaDB as a backward compatible drop-in replacement for MySQL. This open-source management system uses a relational database and structured query language (SQL).

In this tutorial learn how to install MariaDB on CentOS 7 from the local repository or official repository.

The article also shows you how to secure MariaDB and connect to the database management system from the command line.

header for a tutorial on installing mariadb on centOS 7

Prerequisites

  1. An RHEL-based system, such as CentOS 7
  2. Access to a terminal/command line
  3. The yum package manager, included by default
  4. Access to the root user or a user with sudo privileges

2 Options for Installing MariaDB on CentOS

There are two (2) options to install MariaDB on your CentOS system.

The first is with the CentOS local repository. However, this repository does not provide the newest version of MariaDB available.

The second option includes the latest stable version available but requires additional configuration steps.

Option 1: Install MariaDB 5.5 on CentOS 7 from Local Repository

The simplest way to install MariaDB is from the CentOS repository. Although the repository does not offer the latest stable version, MariaDB 5.5 is a stable release.

1. Open the terminal and type in the following command to download and install the MariaDB package:

yum install mariadb-server

Press y to confirm installation and hit Enter.

2. To start using the MariaDB service and enable it to run on boot use the commands:

systemctl start mariadb
systemstl enable mariadb

3. Lastly, verify the installation by checking the service status with:

systemctl status mariadb

The output should tell you the service is active (running), as in the example below:

verification that MariaDB is running after install

Option 2: Install MariaDB 10.3 on CentOS 7 from MariaDB Repository

You can install the latest stable version of MySQL by downloading it from the official MariaDB repository.

1. First, you need to create a repository file for MariaDB (MariaDB.repo) using a text editor. Type in the following command:

vi /etc/yum.repos.d/MariaDB.repo

This creates and opens a new file under the name MariaDB.repo in the /etc/yum.repos.d/ directory. At the moment, the file is still empty and it appears as in the following image:

maria.db new file while Oracle VM VirtualBox is running

2. Copy the following lines in the text editor:

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.3/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

new file in MariaDB repository created


Note: Change the baseurl line to correspond to your Linux distribution. For Example, if you are using RHEL instead of CentOS, type: baseurl = http://yum.mariadb.org/10.1/rhel7-amd64


3. Once you have added the content to the repository file, save and close the text editor.

4. Next, install the latest MariaDB version with the command:

yum install mariadb-server mariadb-client –y

5. Finally, check whether the installation was successful by running the service and verifying the MariaDB version:

service mysql start
mysqld –V

verifying the MariaDB version running 10.3.15


Note: Although MariaDB replaced MySQL, it still uses some commands to reference to it. For example: mysql –V.


Securing MariaDB

This open-source database management system does not have secure settings by default. To ensure your data is safe, run its installation script to configure the security options.

1. Start by typing the following command to initiate running the security script:

mysql_secure_installation

2. Next, you will be presented with following options:

  • Set root password? [Y/n] – type y (for yes) and hit Enter create a root password for your database
  • Remove anonymous users? [Y/n] – type y and hit Enter
  • Disallow root login remotely? [Y/n] – type y and hit Enter
  • Remove test database and access to it? [Y/n] – type y and hit Enter
  • Reload privilege tables now? [Y/n] – type y and hit Enter

The output informs you that the MariaDB installation is now secure:

screenshot after securing your install of MariaDB

Connecting to MariaDB From Command Line

1. Connect to MariaDB from the command line as the root user with the command:

mysql –h localhost –u root –p

2. When prompted, type in the root password you have set in the previous step, while securing the MariaDB installation.

3. A welcome message will appear as follows:

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is #your id number
Server version: 10.3.15-MariaDB MariaDB Server
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4. Once you have finished working with the database, exit MariaDB with the following command:

mysql> exit

Conclusion

Now you know how to install MariaDB on CentOS. We also covered securing MariaDB and connecting to the database management system from the command line.

Now you have everything set up, you can start managing and storing data.