Web & data

How to install MariaDB on a Debian- or Ubuntu-based Linux system

Install MariaDB on Debian or Ubuntu in three steps: update the packages, install mariadb-server, then run mysql_secure_installation and test the service.

· 5 min read · level: beginner · on Ubuntu 22.04

Introduction

MariaDB is an open source relational database management system, commonly used as an alternative to MySQL, a database often found in the popular LAMP stack ( Linux, Apache, MySQL, PHP / Python / Perl ). MariaDB is intended to be a drop-in replacement for MySQL.

The short version of this installation guide comprises these three steps:

  • Update your package list using apt
  • Install the mariadb-server package using apt. The package also pulls in associated tools to interact with MariaDB
  • Run the mysql_secure_installation security script to restrict access to the server
Illustration 1 — Comment installer MariaDB sur un système Linux basé sur Debian ou Ubuntu

Step 1 — Installing MariaDB

At the time of writing this article, the default APT repositories of Ubuntu 22.04 include version 10.5.12 of MariaDB.

To install it, update the package index on your server with apt:

sudo apt update

Then install the package for the MariaDB server version:

sudo apt install mariadb-server

These commands will install MariaDB, but will not prompt you to set a password or make any other configuration changes. Because the default configuration leaves your MariaDB installation insecure, you will use a script that the mariadb-server package provides to restrict access to the server and to remove unused accounts.

Step 2 — Configuring MariaDB

For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options.

Run the security script:

sudo mysql_secure_installation

This will take you through a series of prompts where you can make changes to the security options of your MariaDB installation. The first prompt will ask you to enter the current password of the root user. Since you have not set one up yet, press ENTER.

Output
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, you'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):

You will be asked whether you want to switch to unix socket authentication. Since you have already protected the root account, you can skip this step. Type n then press ENTER.

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

The next prompt asks whether you want to set the root user password. On Ubuntu, the root account for MariaDB is closely tied to automated system maintenance, so you should not change the authentication methods configured for this account.

Doing so would risk impacting the database system by removing access to the administrative account. Type n then press ENTER.

Output
. . .
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] n

Later, you will go over how to configure an additional administrative account for password access if socket authentication does not suit your use case.

From here, you can press Y then ENTER in order to accept the default values for all the following questions. This will remove some anonymous users and the test database, disable remote root login and load the new rules so that MariaDB immediately implements the changes you have made.

With that, you have finished the initial security configuration of MariaDB. The next step is optional, but you should follow it if you prefer to authenticate to your MariaDB server with a password.

Step 3 — Testing MariaDB

When installed from the default repositories, MariaDB will start running automatically. To test this, check its status.

sudo systemctl status mariadb

You will receive output similar to the following:

Output
● mariadb.service - MariaDB 10.5.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-03-11 22:01:33 UTC; 14min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
. . .

If MariaDB is not running, you can start it with the command sudo systemctl start mariadb.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command tells it to connect to MariaDB as root using the Unix socket and to return the version:

sudo mysqladmin version

You will receive output similar to this:

Output
mysqladmin  Ver 9.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Server version        10.5.12-MariaDB-1build1
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /run/mysqld/mysqld.sock
Uptime:            15 min 53 sec
Threads: 1  Questions: 482  Slow queries: 0  Opens: 171  Open tables: 28  Queries per second avg: 0.505

Conclusion

In this guide, you installed the MariaDB relational database management system and secured it using the mysql_secure_installation script it came with.

Credits

Guide adapted for Awoui from the work of Brian Boucheron/DigitalOcean under the Creative Commons Attribution NonCommercial ShareAlike 4.0 International License