How to install MariaDB in CentOS/RHEL 8 and Fedora

Install MariaDB in CentOS RHEL 8 Install MariaDB in CentOS RHEL 8

Introduction – How to Install MariaDB

how to install MariaDB in CentOS 8 Linux server. A production-ready OpenSource relational database licensed under GPLv2, LGPLv2.1. MariaDB provides general release maintenance till 2022 for 10.2 version, by following for 10.3 and 10.4 till 2023 and 2024. Let us see how to install MariaDB and configure the same, By following will create a database, user and provide privileges to DB user on the created database.

How to install MariaDB
MariaDB Database

Enable Repository for MariaDB

To create the yum repository file just copy-paste the below repo entries. The copied entries will be appended to /etc/yum.repos.d/mariadb.repo.

If you wish to install the latest versions like 10.5.x use below repository.

cat << 'EOF' > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos8-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

Let’s focus on installing current stable version 10.4.x then let us see how to install an old specific version as well.

MariaDB 10.4 CentOS Repository entry

cat << 'EOF' > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

MariaDB 10.4 RedHat Repository entry

cat << 'EOF' > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/rhel8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

MariaDB 10.4 Fedora Repository entry

cat << 'EOF' > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/fedora31-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

Repository for specific Version

Let’s assume our application team requested to install MariaDB 10.3.12, And they are strictly sticking to a specific major/minor version of MariaDB. We can accomplish it by enabling the require versions repo. To get to know all available repository look into http://yum.mariadb.org/

Let’s enable the strict version of MariaDB as per our requirement.

cat << 'EOF' > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.12/centos8-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

Right after creating repo verify the same.

Verify the enabled Repo

List the enabled repository.

[root@mariadb ~]# yum repolist
 Last metadata expiration check: 0:00:31 ago on Wed 08 Apr 2020 11:25:00 PM +04.
 repo id repo name status
 AppStream CentOS-8 - AppStream 5,124
 BaseOS CentOS-8 - Base 2,126
 extras CentOS-8 - Extras 12
 mariadb MariaDB 74
[root@mariadb ~]# 

The enabled MariaDB repository contains 74 packages.

Install from AppStream

To install the MariaDB from default CentOS 8 AppStream repository we not required to enable with any above repositories. Just search for module and install with yum.

# yum module list mariadb

The default version currently available from AppStream will be 10.3.x, once we run the command it will install with the latest minor version available from the repository.

[root@mariadb ~]# yum module list mariadb
 Last metadata expiration check: 0:44:12 ago on Wed 08 Apr 2020 08:30:05 PM +04.
 CentOS-8 - AppStream
 Name Stream Profiles Summary
 mariadb 10.3 [d] client, server [d], galera MariaDB Module
 Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@mariadb ~]#

Install MariaDB using yum module.

$ sudo yum module install mariadb:10.3/default      # Installs only MariaDB server.
$ sudo yum module install --profile mariadb/client  # Installs both MariaDB server and client.

Check the installed version by running

[root@mariadb ~]# mysql --version
 mysql Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1
[root@mariadb ~]#

Installing MairaDB

Installing MariaDB from any enabled Repositories other than AppStream.

$ sudo dnf install MariaDB-server MariaDB-client -y   # RHEL8 | CentOS8 | Fedora
Installed:
 MariaDB-client-10.4.12-1.el8.x86_64 MariaDB-server-10.4.12-1.el8.x86_64 perl-IO-Socket-IP-0.39-5.el8.noarch perl-IO-Socket-SSL-2.066-3.el8.noarch
 perl-Mozilla-CA-20160104-7.el8.noarch boost-program-options-1.66.0-6.el8.x86_64 perl-DBI-1.641-3.module_el8.1.0+199+8f0a6bbd.x86_64 perl-Digest-1.17-395.el8.noarch
 perl-Digest-MD5-2.55-396.el8.x86_64 perl-Net-SSLeay-1.88-1.el8.x86_64 perl-URI-1.73-3.el8.noarch perl-libnet-3.11-3.el8.noarch
 lsof-4.91-2.el8.x86_64 perl-Data-Dumper-2.167-399.el8.x86_64 perl-Encode-4:2.97-3.el8.x86_64 perl-File-Temp-0.230.600-1.el8.noarch
 perl-Getopt-Long-1:2.50-4.el8.noarch perl-HTTP-Tiny-0.074-1.el8.noarch perl-MIME-Base64-3.15-396.el8.x86_64 perl-Math-BigInt-1:1.9998.11-7.el8.noarch
 perl-Math-Complex-1.59-416.el8.noarch perl-Pod-Escapes-1:1.07-395.el8.noarch perl-Pod-Perldoc-3.28-396.el8.noarch perl-Pod-Simple-1:3.35-395.el8.noarch
 perl-Pod-Usage-4:1.69-395.el8.noarch perl-Storable-1:3.11-3.el8.x86_64 perl-Term-ANSIColor-4.06-396.el8.noarch perl-Term-Cap-1.17-395.el8.noarch
 perl-Text-ParseWords-3.30-395.el8.noarch perl-Time-Local-1:1.280-1.el8.noarch perl-podlators-4.11-1.el8.noarch rsync-3.1.3-6.el8.x86_64
 MariaDB-common-10.4.12-1.el8.x86_64 galera-4-26.4.3-1.rhel8.0.el8.x86_64
 Complete!
[root@mariadb ~]#

Once installation completed, check for the installed version by running

# mariadb --version
[root@mariadb ~]# mariadb --version
 mariadb Ver 15.1 Distrib 10.4.12-MariaDB, for Linux (x86_64) using readline 5.1
[root@mariadb ~]#

Right after version confirmation, enable the service persistently and start the service. By following check for the service status as well.

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb

Service Status output for reference

[root@mariadb ~]# sudo systemctl status mariadb
  ● mariadb.service - MariaDB 10.4.12 database server
  Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
  └─migrated-from-my.cnf-settings.conf
  Active: active (running) since Thu 2020-04-09 00:01:38 +04; 7s ago
  Docs: man:mysqld(8)
  https://mariadb.com/kb/en/library/systemd/
  Process: 15865 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 15823 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=/usr/bin/galera_recovery; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POS>
  Process: 15821 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Main PID: 15834 (mysqld)
  Status: "Taking your SQL requests now…"
  Tasks: 30 (limit: 4622)
  Memory: 73.4M
  CGroup: /system.slice/mariadb.service
  └─15834 /usr/sbin/mysqld
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] InnoDB: 10.4.12 started; log sequence number 60972; transaction id 21
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] InnoDB: Buffer pool(s) load completed at 200409 0:01:38
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] Plugin 'FEEDBACK' is disabled.
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] Server socket created on IP: '::'.
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] Reading of all Master_info entries succeeded
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] Added new Master_info '' to hash table
  Apr 09 00:01:38 mariadb mysqld[15834]: 2020-04-09 0:01:38 0 [Note] /usr/sbin/mysqld: ready for connections.
  Apr 09 00:01:38 mariadb mysqld[15834]: Version: '10.4.12-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaDB Server
  Apr 09 00:01:38 mariadb systemd[1]: Started MariaDB 10.4.12 database server.
[root@mariadb ~]#

Securing MariaDB

Securing MariaDB by running anyone of below command.

# mysql_secure_installation
# mariadb-secure-installation

By default, the root password will not be there, while running secure installation it will prompt to create the root password, by following it will prompt to remove the anonymous user, disallow remote login and remove the test database. During the secure setup proceed with “Y” for all the options.

[root@mariadb ~]# mariadb-secure-installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on…

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
… Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
… Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
… Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
Dropping test database…
… Success!
Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately

Reload privilege tables now? [Y/n] Y
… Success!

Cleaning up…

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@mariadb ~]#

Right after securing MariaDB, Let’s check whether our DB is allowed to access remotely. To have an isolated DB setup it’s better to stick with running the DB locally inside the box. With the default setup, we are allowed to connect from anywhere.

[root@mariadb ~]# ss -tunlp | grep mysql
 tcp LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=16824,fd=20))
[root@mariadb ~]#

Edit the server configuration file and disable networking option.

# vim /etc/my.cnf.d/server.cnf

Under [mariadb-10.4] section append with skip-networking as shown below.

[mariadb-10.4]
skip-networking

skip-networking tells MariaDB to run without any of the TCP/IP networking options. Restart the MariaDB service to make the changes effective.

$ sudo systemctl restart mariadb

Now while we list the ports it will not return anything.

$ ss -tunlp | grep mysql

We are good with security part.

Checking logs of MariaDB

If you face any issue while configuring MariaDB check for logs using journalctl.

# journalctl -n 10 _SYSTEMD_UNIT=mariadb.service
[root@mariadb ~]# journalctl -n 10 _SYSTEMD_UNIT=mariadb.service
 -- Logs begin at Wed 2020-04-08 20:19:36 +04, end at Thu 2020-04-09 00:40:40 +04. --
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] InnoDB: Waiting for purge to start
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] InnoDB: 10.4.12 started; log sequence number 61026; transaction id 21
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] InnoDB: Buffer pool(s) load completed at 200409 0:40:40
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] Plugin 'FEEDBACK' is disabled.
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] Reading of all Master_info entries succeeded
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] Added new Master_info '' to hash table
 Apr 09 00:40:40 mariadb mysqld[16954]: 2020-04-09 0:40:40 0 [Note] /usr/sbin/mysqld: ready for connections.
 Apr 09 00:40:40 mariadb mysqld[16954]: Version: '10.4.12-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 0 MariaDB Server
[root@mariadb ~]#

Login to MariaDB

Login to MariaDB using anyone of below command.

# mysql -u root -p
# mariadb -u root -p

-u represent user (root), and -p for password. If you need to login using any-other use replace root with your DB user.

Once we login

[root@mariadb ~]# mariadb -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.4.12-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

Creating User and DataBase

Once after the successful login create a Database and DB user. By following provide full privilege for user on specific database.

# Create a Database
  MariaDB [(none)]> CREATE DATABASE prodlinuxdb;

# Create a MariaDB user account
  MariaDB [(none)]> CREATE USER 'produser1'@'localhost' IDENTIFIED BY 'Redhat@123';

# Grant Prilvilege on database for specific User
  MariaDB [(none)]> GRANT ALL ON prodlinuxdb.* TO 'produser1'@'localhost';

# Reload the Privileges
  MariaDB [(none)]> FLUSH PRIVILEGES;

Output for reference

MariaDB [(none)]>
MariaDB [(none)]> CREATE DATABASE prodlinuxdb;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> CREATE USER 'produser1'@'localhost' IDENTIFIED BY 'Redhat@123';
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> GRANT ALL ON prodlinuxdb.* TO 'produser1'@'localhost';
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb ~]#

Print the created Database

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| prodlinuxdb        |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]>

That’s it, we have successfully completed with installing MariaDB and creating a database on it.

Conclusion

We have gone through how to install MariaDB on CentOS 8 and Red Hat based Linux variants. Let’s come up with more MariaDB guide in future. Subscribe to our newsletter to receive our upcoming guides.