Installing MariaDB featured image

Installing MariaDB on CentOS 7

In the world of database engines, MySQL and MariaDB are two of the major players. MariaDB is practically MySQL with some key changes. It’s a community-developed fork of MySQL backed by commercial support, for example, Wikipedia, Google, WordPress.com, etc. MariaDB is free and open-source and guaranteed to stay so.

In this guide, we will guide you through the steps of installing MariaDB on CentOS 7.

MariaDB on CentOS 7

MariaDB is an RDBMS (relational database management system). It comes with all major open-source storage engines. The MariaDB source code is publicly available on GitHub.

Oftentimes, MariaDB is installed as a part of the LEMP (Linux, NGINX, MySQL/MariaDB, and PHP/Python/Perl) or LAMP (Linux, Apache, MySQL/MariaDB, and PHP/Python/Perl) stacks.

Prerequisites

The first requirement is having a properly configured CentOS server. This guide assumes that you already have one configured.

Performing any system-level changes requires root privilege. CentOS has strict control over root access. Installing and configuring MariaDB requires you to have root access. Alternatively, a user with sudo privilege will also work.

If MariaDB is going to be a part of the LAMP stack, then check out this guide on how to install and configure the LAMP stack on CentOS 7. This guide will elaborate on the MariaDB installation section.

Installing MariaDB

Depending on the version of MariaDB, there are two ways of installing MariaDB on CentOS 7.

  • Installing MariaDB 5.5

This is the easiest way of installing MariaDB on CentOS. The CentOS package servers host MariaDB 5.5 packages. Thus, we can use YUM to grab and install it right away.

First, check out the MariaDB package info:

Mariadb yum info

Then, install MariaDB 5.5:

Installing Mariadb 5.5

  • Installing MariaDB 10.4

Both MariaDB 5.5 and MariaDB 10.4 are mainstream releases. As the release version suggests, MariaDB 10.4 comes with substantial improvements over MariaDB 5.5.

Installing MariaDB 10.4 is a bit tricky. It isn’t directly available from the CentOS package repositories. Thankfully, MariaDB has a dedicated YUM repository to help the package management process.

First, add the MariaDB YUM repository:

Download Latest Mariadb Repo

Mariadb repo permission setup

Mariadb repo setup

Once the repo is installed, install MariaDB:

Mariadb 10 Installation

MariaDB Service

CentOS uses systemd as the init system. The MariaDB service is managed by systemd. This service determines the MariaDB execution status. Run the following command to start the MariaDB daemon:

Start Mariadb Service

If the service was started successfully, it won’t show any output. To verify, we can check the service status:

Next, we need to ensure that MariaDB starts at boot. Otherwise, the service has to be manually started every time the system reboots:

Enable Mariadb

Securing MariaDB

Once MariaDB is up and running, we need to perform the initial configuration. MariaDB comes with a security script. It changes some of the default settings for better security. First, launch the security script:

Mariadb Secure Installation

Every single step of the script comes with an explanation. The first step will ask for the MariaDB root password. Assuming it’s a fresh installation, there’s no root password configured. Press Enter to go to the next step.

If there’s no root password, the script will prompt you to set a new root password. The anonymous user feature is for testing purposes. It allows anyone to log in to MariaDB without having access to any user account. If you’re installing MariaDB for testing purposes only, then it’s fine to keep it. Otherwise, enter y to remove anonymous users:

Remove Anonymous Users

Similar to Linux, the root user holds the ultimate power over all MariaDB databases and system configurations. It’s a common practice to disable root for remote login. If it’s a local server, this is the protocol you should follow. However, if MariaDB is running on a remote server, then consider keeping the feature:

Disable Root Login

The test database is a built-in database that’s designed for testing only. It should be removed when implementing MariaDB into a production environment:

Drop Test DB

Finally, the script will ask to reload privilege tables. It will ensure that all the changes made so far will take into effect immediately:

Reload Privilege Tables

The script should finish without any issue.

Testing MariaDB

The MariaDB installation and configuration are complete now. It is time to test it out. The testing should work fine if everything up to this step was performed properly.

For the test, we’ll be using the mysqladmin tool that’s dedicated to running administrative commands. Run the following command:

MySQL Admin Info

Here, we’ve used two flags:

  • -u: Signifies the user to connect to MariaDB
  • -p: Signifies password for user authentication

This command will print out the MariaDB version along with a short report of the system.

Basic Usage

  • Accessing MariaDB shell

To directly interact with the MariaDB server, it comes with a shell of its own. Launch the MariaDB shell as the root user:

Mariadb Shell

  • Listing databases

The following SQL query will print all the databases on the current server:

Listing Databases

  • Creating a new database

Next, the following SQL query will create a new database demo_database:

Create DB

  • Accessing database contents

In order to check the content of a database, change the active database to the desired one and run necessary SQL queries. Then, change the current database:

Use Demo DB

We can add, remove, or print the tables in the database. For example, here’s how to check all the tables under the database:

Show DB Tables

  • Deleting a database

If a database is no longer needed, we can safely delete it. In MySQL/MariaDB terminology, deleting a database is called dropping. To drop the database “demo_database”, run the following command:

Drop DB

Final Thoughts

Voila! MariaDB installation is successful! MariaDB is now ready to be deployed in production.

As MariaDB is very similar to MySQL, all the MySQL knowledge you learned before is still valid. For those who are new to MySQL and MariaDB, here’s a detailed guide on some of the most basic functions of MySQL. It elaborates on various MySQL functions and usage. You can also check out how to create a MySQL user, grant various permissions and privileges, and delete it.

Happy computing!

 

About Pranay Kapgate

Software Engineer | Open Source Software aficionado