How to Install MariaDB on CentOS and Rocky Linux

November 21, 2024

Introduction

MariaDB is a backward-compatible drop-in replacement for MySQL. This open-source database management system uses relational databases and structured query language (SQL).

In this tutorial, you will learn how to install MariaDB on CentOS and Rocky Linux from the local or official repository.

How to Install MariaDB on CentOS and Rocky Linux.

Prerequisites

Options for Installing MariaDB on CentOS and Rocky Linux

There are two options to install MariaDB on a CentOS or Rocky Linux system:

  • Via the local repository. This option does not provide the newest version of MariaDB.
  • Via the official MariaDB repository. The official repository includes the latest stable version but requires additional configuration steps.

The sections below contain steps to install MariaDB on CentOS and Rocky Linux using both methods.

Option 1: Install MariaDB on CentOS and Rocky Linux from Local Repository

The simplest way to obtain MariaDB is by downloading it from the local YUM repository. Follow the steps below to install MariaDB on CentOS and Rocky Linux:

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

sudo yum install mariadb-server
Installing MariaDB on CentOS or Rocky Linux from the YUM repository.

Press Y to confirm installation and hit Enter.

2. Start the MariaDB service:

systemctl start mariadb

3. Enable the service to run on boot:

systemctl enable mariadb
Enabling MariaDB on CentOS and Rocky Linux using systemctl.

4. Verify the installation by checking the service status with:

systemctl status mariadb

The output shows the service as active (running).

Checking MariaDB service status on CentOS and Rocky Linux Using systemctl.

Option 2: Install MariaDB on CentOS and Rocky Linux from MariaDB Repository

Install the latest stable version of MySQL by downloading it from the official MariaDB repository. Follow the steps below to add the repository and perform the installation:

1. Create a repository file for MariaDB (MariaDB.repo) using a text editor such as Vi:

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

The command creates and opens a new file in the /etc/yum.repos.d/ directory.

2. Copy the following lines in the text editor:

[mariadb]
name = MariaDB
baseurl = https://rpm.mariadb.org/10.6/rhel/$releasever/$basearch
gpgkey= https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
The repo file for MariaDB.

3. Save and close the text editor.

4. Install the latest MariaDB version with the command:

sudo yum install MariaDB-server MariaDB-client -y

5. Start the service and verify the MariaDB version:

service mysql start

6. Check whether the installation was successful by viewing the installed MariaDB version:

mysqld -V
The mysqld command showing the installed MariaDB version.

Securing MariaDB

MariaDB does not have secure settings after the default installation is performed. To ensure the data is safe, run the dedicated security script to configure the security options:

1. Initiate the security script:

sudo mariadb-secure-installation
MariaDB security script.

2. The script allows the user to answer the following configuration questions:

  • Set root password?
  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

Type Y and press Enter for each question. The output shows that the MariaDB installation is now secure.

The security script exits after finishing.

Note: For more information on user management in MariaDB, read our article on how to create a MariaDB user and grant privileges.

Connecting to MariaDB from Command Line

After using one of the methods mentioned above to install MariaDB, use the following steps to connect to the database from the command line:

1. Enter the following command:

mysql -h localhost -u root -p

2. When prompted, type in the root password you set while securing the MariaDB installation. A welcome message appears.

MariaDB interface.

3. When finished, exit MariaDB with the following command:

quit

Conclusion

This article taught you how to install MariaDB on CentOS and Rocky Linux. It also covered securing MariaDB and connecting to the database management system from the command line.

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

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Create MariaDB User and Grant Privileges
March 18, 2020

Once you install MariaDB, one of the first things you need to do is to create a new MariaDB user. This...
Read more
How to Install MariaDB on Ubuntu
February 28, 2024

MariaDB is an open source, fully compatible, relational database management system. It is commonly used as a...
Read more
How to Check the MySQL Version In Linux
July 11, 2019

It is essential to know which version of MySQL you have installed. The version number helps to determine if a...
Read more
How To Install MySQL on CentOS 7
February 6, 2019

MySQL is a popular free and open source database management application. It forms part of the LAMP stack...
Read more