How to Install MariaDB on Debian 9

In this article, we will show you how to install MariaDB on a Debian 9 Cloud VPS.

MariaDB is a free and open-source, backwards-compatible, enhanced and drop-in replacement of the popular MySQL database management server software. It is very fast, stable, and scalable, making this database server ideal for almost everyone, which is why it’s one of the most used database servers in the world. Let’s get started with the installation.

Step 1. Log into your Server

First things first, login to your Debian 9 Cloud VPS via SSH as the root user:

ssh root@<span style="color: #ff0000;">IP_Address</span> -p <span style="color: #ff0000;">Port_number</span>

Make sure to replace IP_Address and Port_number with your server’s IP and SSH port.

Step 2. Install MariaDB

Method 1. Install from the Debian Repository

Installing MariaDB from the Debian base repository is a straightforward method of installing MariaDB, but doing this may lead to an older version being installed. If you need the latest version of MariaDB, you can install it through the official MariaDB repository, as shown in Method 2.

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

Run the command mysql_secure_installation to perform the initial setup of MariaDB server once you’ve installed it. Let’s see how:

Method 2. Install MariaDB from MariaDB Repositories

Before continuing with the installation you can visit the MariaDB Repository page and check the latest MariaDB version. At the time of writing this article, the latest version of MariaDB was 10.3.

To install 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 dirmngrsudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.dotsrc.org/mariadb/repo/10.3/debian stretch main'

Once the repository is enabled, update the packages list and install MariaDB with these next two commands:

sudo apt update
sudo apt install mariadb-server

In case you want to start/stop MariaDB, you can use the following commands.

sudo systemctl start mariadb
sudo systemctl stop mariadb

Let’s verify that MariaDB is running.

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

To enable the MariaDB service on system boot, run the following command:

systemctl enable mariadb

In case you want MariaDB disabled on system boot, run the following command:

systemctl disable mariadb

Step 3. Secure MySQL Installation

Execute the following command on your server.  For the highest level of security, answer all prompts with yes by entering the character “y“.

sudo mysql_secure_installation

This program enables you to improve the security of your MariaDB installation in the following ways:

  1. Sets a password for root accounts.
  2. Remove root accounts that are accessible from outside the local host.
  3. Removes anonymous user accounts.
  4. Removes the test database.

The script is fully interactive and prompts for each step in the process.

Now that MariaDB is installed and secured, let’s go over logging into the MariaDB CLI.

Connect to MariaDB from the Command Line

To connect to the MariaDB server through the terminal using the root account, type and execute the following command:

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 should be able to see the following output:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5635
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)]>

You can now create a database by typing the following command:

CREATE DATABASE your_database;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'password'; 
GRANT ALL PRIVILEGES ON your_database.* TO your_user@localhost; 
FLUSH PRIVILEGES; 
quit

To view MariaDB’s command list from within the client, type:

\h
General information about MariaDB can be found at
MariaDB Foundation
List of all MySQL commands: Note that all text commands must be first on line and end with ';' ? (\?) Synonym for `help'. clear (\c) Clear the current input statement. connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. exit (\q) Exit mysql. Same as quit. go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. notee (\t) Don't write into outfile. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. status (\s) Get status information from the server. system (\!) Execute a system shell command. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. use (\u) Use another database. Takes database name as argument. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement. For server side help, type 'help contents'

You can now create databases, tables, users, and perform everything that you need easily with MariaDB. If you need extra help with MariaDB, there are countless resources online detailing the specific features of this SQL-based server.


Of course, you don’t have to install MariaDB on Debian 9 if you use one of our Managed Cloud VPS Hosting services, in which case you can simply ask our expert Linux admins to install MariaDB on Debian 9 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install MariaDB on Debian 9, please share it with your friends on the social networks using the share shortcuts, or simply leave a reply below. Thank you.

Leave a Comment