MariaDB is an open-source, multi-threaded relational database  management system, backward compatible replacement for MySQL. It is  maintained and developed by the MariaDB Foundation including some of the original developers of the MySQL.

With the release of Debian 9, MySQL was replaced with MariaDB as the default database system.

In  this tutorial we will show you two different methods on how to install  MariaDB on your Debian 9 machine. The first method will walk you through  the steps necessary to install MariaDB from the Debian repositories  while the second one will teach you how to install the latest version of  MariaDB from the official MariaDB repositories.

Generally, it is recommended to install MariaDB from the Debian repositories.

If you prefer MySQL over MariaDB, check the How to Install MySQL on Debian 9 tutorial. If your application does not have any the specific  requirements, you should stick with MariaDB, the default database system  in Debian 9.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Installing MariaDB on Debian 9

At the time of writing this article, the latest MariaDB version available in the Debian main is version 10.1.

To install MariaDB on Debian 9, follow these steps:

First update the apt packages index by typing:

sudo apt update

Once the packages list is updated, install MariaDB by running the following command:

sudo apt install mariadb-server

The MariaDB service will start automatically. You can verify it by typing:

sudo systemctl status mariadb

The output should look something like this:

● mariadb.service - MariaDB database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset
Active: active (running) since Wed 2018-09-12 15:10:40 UTC; 1min 48s ago
Main PID: 11356 (mysqld)
Status: "Taking your SQL requests now..."
CGroup: /system.slice/mariadb.service
        └─11356 /usr/sbin/mysqld

Installing MariaDB on Debian 9 from MariaDB Repositories

At  the time of this writing the latest version of MariaDB is version 10.3  which can be installed from the official MariaDB repositories. Before  continuing with the next step visit the MariaDB Repository page and check if there is a new version available.

To install the MariaDB 10.3 on your Debian 9 system, follow these steps:

The first step is to enable the MariaDB repository and import the repository GPG key to your system to do so run the following commands:

sudo apt install software-properties-common dirmngr

Once the repository is enabled, update the packages list and install MariaDB with:

sudo apt update

The MariaDB service will start automatically, to verify it run the following command:

sudo systemctl status mariadb
● mariadb.service - MariaDB 10.3.8 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
        └─migrated-from-my.cnf-settings.conf
Active: active (running) since Sun 2018-07-29 19:36:30 UTC; 56s ago
    Docs: man:mysqld(8)
        https://mariadb.com/kb/en/library/systemd/
Main PID: 16417 (mysqld)
Status: "Taking your SQL requests now..."
    Tasks: 31 (limit: 507)
CGroup: /system.slice/mariadb.service
        └─16417 /usr/sbin/mysqld

Securing MariaDB

To improve the security of the MariaDB installation run the mysql_secure_installation script:

sudo mysql_secure_installation

The  script will prompt you to set a password for the root account , remove  the anonymous user, restrict root user access to the local machine and  remove the test database.

At the end the script will reload the privilege tables ensuring that all changes take effect immediately.

All steps are explained in detail and it is recommended to answer “Y” (yes) to all questions.

Connect to MariaDB from the command line

To connect to the MariaDB server through the terminal as the root account type:

mysql -u root -p

You will be prompted to enter the root password you have previously set up using the mysql_secure_installation script.

Once you enter the password you will be presented with the MariaDB shell as shown below:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Conclusion

In this tutorial, we’ve shown you how to install and secure MariaDB on a Debian 9 server.

Now  that your MariaDB server is up and running and you know how to connect  to the MariaDB server from the command line, you might want to check the  following guides: