How to Install MariaDB on Debian 8/9/10

MariaDB is an free and open source, multi-threaded relational database management system, backward compatible replacement for MySQL. MariaDB is a drop-in replacement of MySQL with more features, new storage engines, and better performance.

At the time of writing this article, the latest MariaDB version available in the Debian main is version 10.4.You can read more on MariaDB 10.4 features from the official website.

This tutorial will tell you how to install MariaDB 10.4 on Debian 10.It also works on Debian 8 and Debian 9。

Prerequisites

Perform the following steps as root or user with sudo privileges to install MariaDB on Debian 10.

Step 1. Update the apt package manager index

Start by updating the packages index,type following the command:

sudo apt update

Step 2. Import MariaDB gpg key and add repository

We need to import MariaDB gpg key which contains the keys used for signing MariaDB Debian packages. Then add the MariaDB repository to your system:

MariaDB gpg key for Debian 10 “buster” :

sudo apt-get install software-properties-common dirmngr
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian buster main'

MariaDB gpg key for Debian 9 “stretche” :

sudo apt-get install software-properties-common dirmngr
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian stretch main'

MariaDB gpg key for Debian 8 “jessie” :

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian jessie main'

Step 3. Installing MariaDB on Debian

Once the key is imported and the repository added you can install MariaDB server and client packages with:

sudo apt update
sudo apt install mariadb-server mariadb-client

Once the installation is complete.The MariaDB service will start automatically. To verify it check the service status:

sudo systemctl status mariadb

Step 4. Securing MariaDB

MariaDB comes with a script that can help you improve the security of installation. To start the script type mysql_secure_installation in your terminal:

sudo mysql_secure_installation

You will be prompted 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.

...
Enter current password for root (enter for none):
...
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
...
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
...
Thanks for using MariaDB!

If selected, the script will reload the privilege tables ensuring that the changes take effect immediately.

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

Step 5. Authentication Methods

By default, the MariaDB root user uses the unix_socketauthentication plugin which checks the effective user id when calling the mysql client tool.

This means that you can connect to the MariaDB server as root only if you are invoking the mysql command as system root or by prepending sudo to the command.

For increased security, it is recommended to keep the default authentication plugin and allow the root user to authenticates only via Unix sockets.

If you want to change the root authentication to the classic one, log in to the MariaDB server:

sudo mysql

Run the following statements to change the authentication plugin:

ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_root_passwd';

You can now connect to the MariaDB server using the new password:

mysql -u root -p

Changing the authentication plugin will also allow you to log in as root from an external program such as phpMyAdmin

Step 6. Manage MariaDB Service

If you need to stop or start MariaDB service. Use the following commands to stop, start,check status and restart MariaDB service.

#start
sudo systemctl start mariadb.service
#stop
sudo systemctl stop mariadb.service 
#status
sudo systemctl status mariadb.service
#restart
sudo systemctl restart mariadb.service

Conclusion

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

Leave a Reply

Your email address will not be published. Required fields are marked *