How to Install MariaDB on Ubuntu 18.04

Updated on

4 min read

How to Install MariaDB on Ubuntu 18.04

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.

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

Generally, it is recommended to use the first method and install MariaDB packages provided by Ubuntu.

If you want to install MySQL instead of MariaDB, check the How to Install MySQL on Ubuntu 18.04 tutorial.

Prerequisites

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

Installing MariaDB on Ubuntu 18.04

At the time of writing this article, MariaDB version 10.1 is included in the Ubuntu main repositories.

To install MariaDB on Ubuntu 18.04, follow these steps:

  1. Update packages index.

    sudo apt update
  2. Once the packages list is updated, install MariaDB by issuing the following command:

    sudo apt install mariadb-server
  3. The MariaDB service will start automatically. You can verify it by typing:

    sudo systemctl status mariadb
        ● mariadb.service - MariaDB database server
    Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset
    Active: active (running) since Sun 2018-07-29 19:31:31 UTC; 38s ago
    Main PID: 13932 (mysqld)
    Status: "Taking your SQL requests now..."
        Tasks: 27 (limit: 507)
    CGroup: /system.slice/mariadb.service
            └─13932 /usr/sbin/mysqld

    You can also check the MariaDB version with:

    mysql -V
    mysql  Ver 15.1 Distrib 10.1.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Installing MariaDB on Ubuntu 18.04 from the MariaDB Repositories

At the time of writing this article, the latest version of MariaDB available from the official MariaDB repositories is MariaDB version 10.3. Before continuing with the next step you should visit the MariaDB Repository page and check if there is a new version available.

To install MariaDB 10.3 on your Ubuntu 18.04 server perform the following steps:

  1. First add the MariaDB GPG key to your system using the following command:

    sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
  2. Once the key is imported, add the MariaDB repository with:

    sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.accretive-networks.net/mariadb/repo/10.3/ubuntu bionic main'

    If you get an error message saying add-apt-repository command not found install the software-properties-common package.

  3. To be able to install packages from the MariaDB repository you’ll need to update the packages list:

    sudo apt update
  4. Now that the repository is added install the MariaDB package with:

    sudo apt install mariadb-server
  5. The MariaDB service will start automatically, to verify it type:

    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

    And print the MariaDB server version, with:

    mysql -V
    mysql  Ver 15.1 Distrib 10.3.8-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Securing MariaDB

Run the mysql_secure_installation command to improve the security of the MariaDB installation:

sudo mysql_secure_installation

The script will prompt you to set up the root user password, 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 we can use the MariaDB client.

To log in to the MariaDB server as the root user type:

mysql -u root -p

You will be prompted to enter the root password you have previously set when the mysql_secure_installation script was run.

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 49
Server version: 10.1.29-MariaDB-6 Ubuntu 18.04

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

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

Conclusion

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:

If you prefer a web interface over command line, you can install phpMyAdmin and manage your MariaDB databases and users through it.