How to Install MariaDB on CentOS 8

In this tutorial, we’ll show you how to install, configure and optimize MariaDB on CentOS 8. It’s a beginner-friendly step-by-step tutorial. It should work on other CentOS versions too, including CentOS 9

MariaDB is a popular alternative to MySQL that many people are migrating their databases over to.

Prerequisites

There aren’t really any prerequisites, other than owning a CentOS 8 server (which this tutorial was written for) and root access to the server itself. You can get a cheap $2.5 per month CentOS server at Vultr.

Step 1: Update your system

The first thing you should always do is update your system. Run the following command:

yum update -y

Step 2: Install MariaDB

To install the version that’s included in CentOS 8 repositories by default, run the following command:

yum install mariadb-server -y

This will install MariaDB Server 10.3 (as of writing).

If you want to use a newer version (like 10.5, the current latest stable release) run the following commands:

wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup
./mariadb_repo_setup

This will run the mariadb_repo_setup bash script that adds a repository so you can get the latest version.

After running the script, install a newer version with the same command as before:

yum install mariadb-server -y

Step 3: Start MariaDB, enable at boot, and check what version you’re using

The next step is to start the MariaDB service:

systemctl start mariadb

And enable MariaDB to start at boot:

systemctrl enable mariadb

To check what version you’re using, log into MariaDB with the following command:

mysql

And use this command to check the version you’re using:

SELECT VERSION();

This should give you a result similar to this:

MariaDB [(none)]> SELECT VERSION();
+----------------+
| VERSION() |
+----------------+
| 10.5.8-MariaDB |
+----------------+
1 row in set (0.000 sec)

Step 4: Secure MariaDB

To secure MariaDB, run the following script that should be run after the installation:

mysql_secure_installation

This script will prompt you with several options, all of them are well documented and self-explanatory.

Running this script is only a small step in securing MariaDB. You should still secure the server itself, and read more information in their official docs.

Optional steps

These are optional steps you can do to improve MariaDB:

Check and repair your MariaDB

If you want to, you can run mysqlcheck to check (and repair) your databases. To check all the databases, run:

mysqlcheck -A

You can append –auto-repair to the command to automatically repair if a checked table is broken:

mysqlcheck -A --auto-repair

Run MySQLTuner to get further recommendations

There’s a Perl MySQLTuner script that you can run and the script will give you several recommendations on what you can do to improve the performance and security of your MariaDB.

To run the script, first install Perl if it’s not already installed:

yum install perl -y

Download the script:

wget http://mysqltuner.pl/ -O mysqltuner.pl

And run it:

perl mysqltuner.pl

Just a heads up: this script will only show you recommendations on what you should do, the script won’t do any of the actual recommendations for you.

Upgrade MariaDB to a newer version

These instructions will work if you’re upgrading from a version like 10.4 to 10.5, but the instructions are similar for other cases too.

First, update the yum repositories. Either run the yum update command or run the mariadb_repo_setup script again from steps 1 or 2.

Then, stop the mariadb service:

systemctl stop mariadb

Next, remove MariaDB by running:

yum remove mariadb-server -y

And now you can install the newer version by running the commands from step 2.

Then, start MariaDB again:

systemctl start mariadb

And finally, run mysql_upgrade to check if everything is compatible with the newer version with the following command:

mysql_upgrade

Leave a comment

Your email address will not be published. Required fields are marked *