How to Install MariaDB on CentOS 8 (Step by Step Guide)

MariaDB on CentOS is a free, and open source database management system managed by the parent companies – MariaDB Corporation AB and MariaDB Foundation. It has many in-built features including, storage engines, improved security systems, and more. It is an upgraded version of MySQL compatible with all operating systems, such as Windows, Linux, etc. It is written in multiple languages, including Perl, C, bash, and C++. It offers faster caching and supports native non-blocking operations.

 

MariaDB Server Features

 

  • Free and open-source software distributed under the GPL license.
  • Offers stability, faster speed, and performance.
  • Compared to MySQL, MariaDB supports a lot of storage engines.
  • Switch to MariaDB is simple and easier.
  • Offers better user support for both beginner and experienced users.

 

In this post, we will show you how to install MariaDB server on CentOS 8.

Install MariaDB on CentOS

install mariadb ubuntu 20.04

Table of Contents

Add MariaDB Repo

By Default, the MariaDB package is not included in the Cent OS 8 default repo. So you will need to create a repo for MariaDB. You can create it with the following command:

				
					nano /etc/yum.repos.d/mariadb.repo
				
			

Add the following lines:

				
					[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

				
			

Save and close the file then update the repo cache with the following command:

				
					dnf makecache
				
			

You should see the following output:

				
					CentOS-8 - AppStream                                                                                            14 MB/s | 9.2 MB     00:00    
CentOS-8 - Base                                                                                                 14 MB/s | 7.5 MB     00:00    
CentOS-8 - Extras                                                                                               65 kB/s |  12 kB     00:00    
MariaDB                                                                                                        2.4 MB/s | 596 kB     00:00    
Metadata cache created.

				
			

Now, verify the MariaDB repo using the following command:

				
					dnf repolist
				
			

You should see the MariaDB repo in the following output:

				
					repo id                                                           repo name
AppStream                                                         CentOS-8 - AppStream
BaseOS                                                            CentOS-8 - Base
extras                                                            CentOS-8 - Extras
mariadb                                                           MariaDB

				
			

Install MariaDB on CentOS 8

You can now install the MariaDB server by running the following command:

				
					dnf install mariadb-server -y
				
			

Once the MariaDB is installed, you will need to start the MariaDB service and enable it to start at system reboot. You can do it with the following command:

				
					systemctl start mariadb
systemctl enable mariadb
				
			

You can now verify the status of the MariaDB service using the following command:

				
					systemctl status mariadb
				
			

You should see the following output:

				
					● mariadb.service - MariaDB 10.5.12 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Sat 2021-09-25 04:19:50 EDT; 6s ago
     Docs: man:mariadbd(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 3917 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 3895 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -e>
  Process: 3893 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 3905 (mariadbd)
   Status: "Taking your SQL requests now..."
    Tasks: 11 (limit: 12524)
   Memory: 71.9M
   CGroup: /system.slice/mariadb.service
           └─3905 /usr/sbin/mariadbd

Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] InnoDB: 10.5.12 started; log sequence number 45094; transaction id 20
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] Plugin 'FEEDBACK' is disabled.
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] InnoDB: Buffer pool(s) load completed at 210925  4:19:50
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] Server socket created on IP: '::'.
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] Reading of all Master_info entries succeeded
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] Added new Master_info '' to hash table
Oct 6 04:19:50 centos8 mariadbd[3905]: 2021-09-25  4:19:50 0 [Note] /usr/sbin/mariadbd: ready for connections.
Oct 6 04:19:50 centos8 mariadbd[3905]: Version: '10.5.12-MariaDB'  socket: 'https://net.cloudinfrastructureservices.co.uk/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
Oct 6 04:19:50 centos8 systemd[1]: Started MariaDB 10.5.12 database server.

				
			

Now, verify the installed version of MariaDB using the following command:

				
					mysqladmin version
				
			

You should see the MariaDB version in the following output:

				
					mysqladmin  Ver 9.1 Distrib 10.5.12-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version		10.5.12-MariaDB
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/lib/mysql/mysql.sock
Uptime:			39 sec

Threads: 1  Questions: 1  Slow queries: 0  Opens: 17  Open tables: 10  Queries per second avg: 0.025

				
			

Secure MariaDB Installation

By default, MariaDB installation is not secured on Cent OS. So it is recommended to secure the MariaDB installation and set the MariaDB root password. You can do it by running the mysql_secure_installation script:

				
					mysql_secure_installation
				
			

You will be asked to provide your root password:

				
					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): 

				
			

Just press Enter. You will be asked to switch to unix_socket authentication:

				
					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] n
				
			

Type n and hit Enter. You will be asked to set a root password:

				
					 ... skipping.

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

Change the root password? [Y/n] Y

				
			

Press Y and hit Enter. You will be asked to set your root password:

				
					New password: 
Re-enter new password: 

				
			

Provide your root password and hit Enter. You will be asked to remove the anonymous users:

				
					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

				
			

Press Y and hit Enter. You will be asked to disallow root login remotely:

				
					 ... 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

				
			

Type Y and hit Enter. You will be asked to remove test database:

				
					 ... 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

				
			

Press Y and hit Enter. You will be asked to reload the privilege table:

				
					 - 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

				
			

Type Y and hit Enter to finish the installation.

				
					 ... 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!

				
			

Create Database and User in MariaDB

First, you will need to log in to the MariaDB shell.

				
					mysql -u root -p
				
			

Provide your MariaDB root password and hit Enter to log in.

				
					Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.5.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)]>

				
			

To create a MariaDB database named testdb, run:

				
					MariaDB [(none)]> CREATE DATABASE testdb;
				
			

To create a user named testuser and set a password, run:

				
					MariaDB [(none)]> CREATE USER 'testuser'@localhost IDENTIFIED BY 'password';
				
			

To list all databases, run:

				
					MariaDB [(none)]> SHOW DATABASES;
				
			

You should see all the databases in the following output:

				
					+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+

				
			

To list all users, run:

				
					MariaDB [(none)]> SELECT User FROM mysql.user;
				
			

You should see all the users in the following output:

				
					+-------------+
| User        |
+-------------+
| mariadb.sys |
| mysql       |
| root        |
| testuser    |
+-------------+

				
			

To grant all the privileges on testdb database to testuser, run:

				
					MariaDB [(none)]> GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@localhost IDENTIFIED BY 'password';
				
			

Now, flush the privileges to apply the changes:

				
					MariaDB [(none)]> FLUSH PRIVILEGES;
				
			

You can see all granted privileges using the following command:

				
					MariaDB [(none)]> SHOW GRANTS FOR 'testuser'@localhost;
				
			

You should see the following output:

				
					+-----------------------------------------------------------------------------------------------------------------+
| Grants for testuser@localhost                                                                                   |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `testuser`@`localhost` IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' |
| GRANT ALL PRIVILEGES ON `testdb`.* TO `testuser`@`localhost`                                                    |
+-----------------------------------------------------------------------------------------------------------------+

				
			

Create a Table in MariaDB

If you want to create a table in MariaDB, first log in to MariaDB with the following command:

				
					mysql -u root -p
				
			

Once you are logged in, switch the database to testdb using the following command:

				
					MariaDB [(none)]> USE testdb;
				
			

Next, create a table named testtable using the following command:

				
					MariaDB [testdb]> CREATE TABLE testtable (id int, name varchar(20), surname varchar(20));
				
			

To list all tables, run the following command:

				
					MariaDB [testdb]> SHOW TABLES;
				
			

You should see all tables in the following output:

				
					+------------------+
| Tables_in_testdb |
+------------------+
| testtable        |
+------------------+

				
			

To insert some data in the first row of the table, run the following command:

				
					MariaDB [testdb]> INSERT INTO testtable VALUES (1,"raj","shah");
				
			

To insert data in the second row of the table, run the following command:

				
					MariaDB [testdb]> INSERT INTO testtable VALUES (2,"ketan","shah");
				
			

To verify your table’s data, run the following command:

				
					MariaDB [testdb]> SELECT * FROM testtable;
				
			

You should see the following output:

				
					+------+-------+---------+
| id   | name  | surname |
+------+-------+---------+
|    1 | raj   | shah    |
|    2 | ketan | shah    |
+------+-------+---------+

				
			

Delete Table, Database and User in MariaDB

To delete a table, run the following command:

				
					MariaDB [testdb]> DROP TABLE testtable;
				
			

To delete a database, run the following command:

				
					MariaDB [testdb]> DROP DATABASE testdb;
				
			

To delete a user, run the following command:

				
					MariaDB [(none)]> DROP USER testuser@localhost;
				
			

MariaDB on CentOS Conclusion

In the above guide, you learned how to install the MariaDB database server on CentOS 8. You also learned how to create a database, user and table in MariaDB. You can now start using the MariaDB as a database backend with your application.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x