Search

How To Install MariaDB or MySQL on CentOS 7

How To Install MariaDB or MySQL on CentOS 7

How To Install MariaDB or MySQL on CentOS 7

Introduction

MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system, commonly installed as part of the popular LEMP (Linux, Apache, Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. It uses a relational database and SQL (Structured Query Language) to manage its data. MariaDB is a fork of MySQL managed by the original MySQL developers. It’s designed as a replacement for MySQL, uses some commands that reference mysql, and is the default package on CentOS 7.

In this tutorial, we will explain how to install the latest version of MariaDB on a CentOS 7 server. If you specifically need MySQL, see the How to Install MySQL on CentOS 7 guide. If you’re wondering about MySQL vs. MariaDB, MariaDB is the preferred package and should work seamlessly in place of MySQL.

Prerequisites

To follow this tutorial, you will need:

A CentOS 7 with a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in the Initial Server Setup with CentOS 7 guide.

Step 1 — Installing MariaDB

We’ll use Centos 7 Yum repositories to install the MariaDB package or you can added MariaDB offical repo from mariadb.com , pressing y when prompted to confirm that we wish to proceed:

sudo yum install mariadb-server

MariaDB latest version offical repo from mariadb.com just type in Terminal:

 curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash 

MariaDB V-10.3 offical repo from mariadb.com just type in Terminal:

 curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-10.3"   

if you run this command then the output should be:

 
[root@linuxits ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
[info] Checking for script prerequisites.
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo
[info] Adding trusted package signing keys...
/etc/pki/rpm-gpg ~~
[info] Successfully added trusted package signing keys
[info] Cleaning package cache...
Loaded plugins: fastestmirror, priorities
Cleaning repos: base elrepo extras mariadb-main mariadb-maxscale mariadb-tools updates
Cleaning up list of fastest mirrors
Other repos take up 11 M of disk space (use --verbose for details)
[root@linuxits ~]#  

MariaDB Install from mariadb.com just type in Terminal:

 sudo yum install MariaDB-server MariaDB-client 

Once the installation is complete, we’ll start the daemon with the following command:

sudo systemctl start mariadb

Systemctl doesn’t display the outcome of all service management commands, so to be sure we succeeded, we’ll use the following command:

sudo systemctl status mariadb

If MariaDB has successfully started, the output should contain “Active: active (running)` and the final line should look something like:

 
[root@linuxits ~]# sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.3 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 Wed 2021-07-21 04:29:11 PDT; 11s ago
     Docs: man:mariadbd(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 1542 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 1516 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
  Process: 1514 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 1528 (mariadbd)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           └─1528 /usr/sbin/mariadbd

Jul 21 04:29:11 linuxits.linuxits.com mariadbd[1528]: 2021-07-21  4:29:11 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
..............
..............
Jul 21 04:29:11 linuxits.linuxits.com mariadbd[1528]: 2021-07-21  4:29:11 0 [Note] /usr/sbin/mariadbd: ready for connections.
Jul 21 04:29:11 linuxits.linuxits.com mariadbd[1528]: Version: '10.6.3-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
Jul 21 04:29:11 linuxits.linuxits.com systemd[1]: Started MariaDB 10.6.3 database server.
Hint: Some lines were ellipsized, use -l to show in full.

Next, let’s take a moment to ensure that MariaDB starts at boot, using the systemctl enable command, which will create the necessary symlinks.

sudo systemctl enable mariadb

Step 2 — Securing the MariaDB Server Set ROOT Password

MariaDB includes a security script to change some of the less secure default options for things like remote root logins and sample users. Use this command to run the security script:

sudo mysql_secure_installation

From MariaDB 10.4.6, mariadb-secure-installation is a symlink to mysql_secure_installation, the script for enabling you to improve the security of your MariaDB installation. 

From MariaDB 10.5.2, mysql_secure_installation is the symlink, and mariadb-secure-installation the binary name.

sudo mariadb-secure-installation

The script provides a detailed explanation for every step. The first prompts asks for the root password, which hasn’t been set so we’ll press ENTER as it recommends. Next, we’ll be prompted to set that root password, which we’ll do.

Then, we’ll accept all the security suggestions by pressing Y and then ENTER for the remaining prompts, which will remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables.

Finally, now that we’ve secured the installation, we’ll verify it’s working.

Step 3 — Testing the Installation

We can verify our installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MariaDB as root (-u root), prompt for a password (-p), and return the version.

 mysqladmin -u root -p version

if you run this command then the output should be:

 
[root@linuxits ~]# mysqladmin -u root -p version
Enter password:
mysqladmin  Ver 9.1 Distrib 10.6.3-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.6.3-MariaDB
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 11 min 30 sec

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

Conclusion

In this tutorial, we’ve installed and secured MariaDB on a CentOS 7 server.

Sys Admin

Sys Admin

I am a Red Hat Certified Engineer (RHCE) and working as an IT Professional since 2012...