Install MariaDB on Debian Last updated: 2021.12.05


Debian 20.04, Debian 18.04

Install MariaDB on Debian and complete the initial configuration. MariaDB is an drop-in replacement for Oracle’s MySQL.


Update & Upgrade

Before getting started, run an obligatory system update. This will make sure you have the latest updates and patches for all installed packages.

sudo apt update && sudo apt upgrade
  • apt update downloads updated package information from all the configured repositories on your system.
  • apt upgrade upgrades all installed packages to the most recent versions available.

You may also run the command apt autoremove to uninstall any package dependencies that are no longer needed.

Install MariaDB

Naturally, the first step in using MariaDB on your server is installing it. The MariaDB version included in the default Debian repositories tends to be out of date, so it is recommended to add a MariaDB repository to your apt sources. We will be using OSUOSL’s mirror in this bit, but you can choose your own from MariaDB’s repository mirror list.

Start by verifying that the required packages software-properties-common, dirmngr, and apt-transport-https are installed.

sudo apt install software-properties-common dirmngr apt-transport-https

Add the repository signing key for MariaDB to Debian.

sudo apt-key adv --fetch-keys ''

Add the MariaDB repository for your applicable Debian version to the sources list.

Debian 11

sudo add-apt-repository 'deb [arch=amd64,i386,arm64,ppc64el] bullseye main'

Debian 10

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] buster main'

Debian 9

sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el,arm64] stretch main'

Finally, update the Debian sources and install MariaDB using apt.

sudo apt update && sudo apt install mariadb-server

MariaDB is enabled by default, meaning it will automatically start when the system is booted. Check that the MariaDB service is running.

sudo systemctl status mariadb

If all is well, the status should resemble the following.

● mariadb.service - MariaDB 10.6.4 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mariadb.service.d
     Active: active (running) since Fri 2021-04-02 02:22:20 EST; 6min ago
       Docs: man:mariadbd(8)
   Main PID: 17187 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 8 (limit: 4617)
     Memory: 73.3M
     CGroup: /system.slice/mariadb.service
             └─17187 /usr/sbin/mariadbd

Should the service not be active, make sure you start it before continuing to the next step.

sudo systemctl start mariadb

Secure Installation

MariaDB comes packaged with a configuration script to improve the security of your MariaDB server by implementing some basic security recommendations. Execute the script using the included command.

sudo mysql_secure_installation

The first step in the script will ask for the current MariaDB root password. By default there is no root password, so simply press enter to move on.

Enter current password for root (enter for none):

You will then have option to choose between setting a root password, or using unix_socket authentication. In this bit, password authentication will be used due to some of the limitations detailed in MariaDB’s Unix Socket documentation.

Enter N when asked about switching to unix_socket authentication, and Y when asked to change the root password.

Switch to unix_socket authentication [Y/n] N
Change the root password? [Y/n] Y

Generate a secure password using your password manager of choice, and enter it when prompted.

New password:
Re-enter new password:

Following password creation, a series of questions will be presented to complete the basic security setup. All of these should be answered with Y.

Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.