A database is an organized collection of related data that can be accessed electronically. They store a variety of data such as emails, passwords, usernames, etc. Database Management System (DBMS) is software that helps users create and manage databases. This software helps in backing up a database and maintaining a huge amount of data and also handles the security of the database. There are two main types of databases which are;

  • Relational Databases store data in a structured way, that is tables with a unique key identifier known as the Primary Key. Examples of this are MySQL Database, PostgreSQL, and MariaDB database.
  • Non-Relational databases store data in an unstructured way, unlike tables. Data can be Organized in XML format or graphs. Examples of this are the MongoDB database, DynamoDB database, and Firebase.

Databases are important as they;

  • Help Database Administrators and users of a system access data at any time.
  • Data stored in Databases is secured with different protocols.
  • Make it easy to backup data in different locations.
  • Assist in keeping track of data.

MariaDB Database

MariaDB is a relational database developed with the intention of remaining free and open-source software under the GNU General public License. It is a commercially supported fork of the MySQL database. Some of the companies that use MariaDB include; Alibaba Group, Accenture, Upstage, BlaBlaCar, Walmart, etc.

Advantages of MariaDB include;

  • Tighter security for your website.
  • Fast performance and high efficiency.
  • Better User support.
  • It is easy to use.

This guide will show you how to install the MariaDB database on CentOS 9|AlmaLinux 9|RHEL 9

Install MariaDB Database on CentOS 9|AlmaLinux 9|RHEL 9

Update system packages.

sudo dnf update

Install MariaDB using the following command.

sudo dnf install mariadb-server

Confirm installation

$ dnf info MariaDB-server
Installed Packages
Name         : mariadb-server
Epoch        : 3
Version      : 10.5.13
Release      : 2.el9
Architecture : x86_64
Size         : 62 M
Source       : mariadb-10.5.13-2.el9.src.rpm
Repository   : @System
From repo    : appstream
Summary      : The MariaDB server and related files
URL          : http://mariadb.org
License      : GPLv2 and LGPLv2
Description  : MariaDB is a multi-user, multi-threaded SQL database server. It
             : is a client/server implementation consisting of a server daemon
             : (mariadbd) and many different client programs and libraries. This
             : package contains the MariaDB server and some accompanying files
             : and directories. MariaDB is a community developed fork from
             : MySQL.

Start and enable the MariaDB service

sudo systemctl start mariadb
sudo systemctl enable mariadb

Check the status of the service:

$ systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor p>
     Active: active (running) since Fri 2022-06-10 21:30:04 EAT; 34s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 5107 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 23440)
     Memory: 71.7M
        CPU: 455ms
     CGroup: /system.slice/mariadb.service
             └─5107 /usr/libexec/mariadbd --basedir=/usr

Secure MariaDB

Use the following command to run the installation security script that comes with the MariaDB installation.

sudo mysql-secure-installation

Test MariaDB

mysql -u root -p

You can check the MariaDB version using the following SELECT command.

> SELECT VERSION();

+-----------------+
| VERSION()       |
+-----------------+
| 10.5.13-MariaDB |
+-----------------+

To Logout of the shell, use the exit command.

exit;

Change MariaDB default Data Directory

The default data directory for MariaDB is /var/lib/mysql/. The following section illustrates how t change the directory of the database and configure SELinux to be aware of the change.

Stop MariaDB service.

sudo systemctl stop mariadb

Create a new directory for the MariaDB data.

sudo mkdir -p /mariadb

Copy the contents from the old directory to the new directory.

sudo cp -R /var/lib/mysql/* /mariadb/

Change the ownership of the location to allow MySQL user to access it.

sudo chown -R mysql:mysql /mariadb

Edit the configuration file to change the

sudo vi /etc/my.cnf

Then edit the data directory option as shown below.

[mariadb]
datadir=/mariadb

Run the semanage command to add the content mapping of the new directory.

sudo semanage fcontext -a -t mysqld_db_t "/mariadb(/.*)?"

Then apply the context mapping to the system using the following command.

sudo restorecon -Rv /mariadb

Then start the MariaDB service.

sudo systemctl start mariadb

To check the current file context use the following command.

$ ls -lZ /mariadb

Create Database

To create a database, Log in to the MariaDB shell and add the following command.

CREATE DATABASE technixleo;

Verify database creation with the following command.

> SHOW DATABASES;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| technixleo         |
+--------------------+

Create User

Create a user and assign privileges to the new user on the newly created database using the following statements.

CREATE USER 'technix'@'localhost' IDENTIFIED BY 'Password';
GRANT ALL PRIVILEGES ON *.* TO 'technix'@'localhost' IDENTIFIED BY 'Password';
GRANT ALL PRIVILEGES ON 'technixleo'.* TO 'technix'@'localhost';
FLUSH privileges;
exit;

Confirm the user has the permissions with the following statement.

> SHOW GRANTS FOR 'technix'@localhost;

+-------------------------------------------------------------------------------------------------------------------------+
| Grants for technix@localhost                                                                                            |
+-------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `technix`@`localhost` IDENTIFIED BY PASSWORD '*FBA7C2D27C9D05F3FD4C469A1BBAF557114E5594' |
+-------------------------------------------------------------------------------------------------------------------------+

Create Table

Select the database to use when creating a table.

USE technixleo;
Database changed

Create a table using the following command.

CREATE TABLE clients (id INT, name VARCHAR(20), email VARCHAR(20));

Use the following code to show the table created.

> show tables;

+----------------------+
| Tables_in_technixleo |
+----------------------+
| clients              |
+----------------------+

Use the following command to verify the columns added.

> SHOW COLUMNS FROM clients;

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| email | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

Insert data into the table with the following syntax.

INSERT INTO clients (id,name,email) VALUES(01,"Mark","[email protected]");

To view users, use the following statement.

> SELECT * FROM clients;

+------+------+---------------------+
| id   | name | email               |
+------+------+---------------------+
|    1 | Mark | [email protected] |
+------+------+---------------------+

To remove a user account. use the follwoing ststement.

DROP USER 'technix'@localhost;

Removing MariaDB (Only for reference)

You can uninstall MariaDB database server with all the data using the following commands. If you changed the data directory

sudo rm -rf /var/lib/mysql/
sudo dnf autoremove mariadb-server
sudo rm /etc/my.cnf

Conclusion

From this guide, we have gone through the installation of the MariaDB database on AlmaLinux 9|CentOS 9|RHEL 9 systems. MariaDB database is easy to use and the best part is that it is free to use. MariaDB is also compatible with MySQL and can replace MySQL without any issues.

See more guides using the following links:

LEAVE A REPLY

Please enter your comment!
Please enter your name here