How To Install MariaDB 10.6 on Ubuntu 20.04|18.04

Posted on 363 views

MariaDB is one of the most used open source relational databases. It is developed by MySQL developers but made to be totally free unlike MySQL. The development of MariaDB focuses on stability and performance. The other reasons why MariaDB is preferred is it being robust and scalable, new storage engines and comes with various tools and plugins that make it widely applicable. It is the default database in most Linux distributions.

In this guide, we are going to look at how to install MariaDB 10.6 on Ubuntu 20.04 and Ubuntu 18.04 from MariaDB APT repository.

Features of MariaDB 10.6

MariaDB 10.6 is the current stable version of MariaDB and comes with a number of new features as discussed below:

  • Ignored Indexes – These are indexes that are visible and maintained but not used by the optimizer
  • sys schema supported- This is a “system” database containing views and procedures for investigating performance problems.
  • SKIP LOCKED – Locked tables are skipped from being updated or selected.
  • JSON_TABLE() – can create a JSON table that can be used as a subquery from a JSON document.
  • OFFSET…FETCH…[WITH TIES] – WITH TIES is an optional clause that adds extra functionality. Example as used
  • Oracle compatibility – There are ongoing works in making MariaDB compatible with OracleDB with some Oracle Syntaxes and functions already added.

Some of the improvements on MariaDB 10.6 from its predecessors include:

  • Atomic DDL – CREATE, ALTER, DROP and RENAME are atomic and crash safe. If MariaDB server crashes while processing any of these operations, the change will either e done completely or not done at all.
  • InnoDB improvements – First insert to an empty table is faster. Also writes to temporary tables are avoided.Faster implicit and explicit temporary tables.
  • Improvements in Galera. Ability to enable encrypted connections between two nodes without downtime. Also added flags to specify if galera controversial compatible features should be enabled.
  • Clean up to remove unsupported features such as TukoDB Engine, Cassandra Engine, some InnoDB variables and some innodb_checksum_algorithm.

Step 1: System upgrade

As usual, we begin by running system upgrades to ensure that we are performing our installations on the latest packages. After updates, reboot your server.

sudo apt update
sudo apt upgrade -y
sudo reboot

Step 2: Install Required Packages

Next, we are going to install software-properties-common package

sudo apt install software-properties-common -y

Step 3: Import MariaDB GPG Key and Add MariaDB APT Repository

With the below commands respectively, we are going to add MariaDB signing key and add MariaDB APT repository:

curl -LsS -O
sudo bash mariadb_repo_setup --mariadb-server-version=10.6

You’ll get an output with contents like the ones shown below:

[info] Checking for script prerequisites.
[info] Repository file successfully written to /etc/apt/sources.list.d/mariadb.list
[info] Adding trusted package signing keys...
[info] Running apt-get update...
[info] Done adding trusted package signing keys

Step 4: Install MariaDB 10.6 on Ubuntu 20.04|18.04

Once MariaDB key and APT repositories are added, update the packages and proceed to install MariaDB 10.6 on Ubuntu 20.04|18.04:

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

Step 5: Secure MariaDB Installation

Once MariaDB is installed, run the below MySQL script to secure MariaDB

$ sudo mariadb-secure-installation 


In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 6: Confirm Mariadb Status

MariaDB server should be automatically started. Check status as below:

$ systemctl status mariadb
 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 Tue 2021-10-19 11:37:25 UTC; 9s ago
     Docs: man:mariadbd(8)
 Main PID: 3023 (mariadbd)
   Status: "Taking your SQL requests now..."
    Tasks: 14 (limit: 4703)
   CGroup: /system.slice/mariadb.service
           └─3023 /usr/sbin/mariadbd

Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: performance_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Phase 6/7: Checking and upgrading tables
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Processing databases
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: information_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: performance_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys.sys_config                                     OK
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: OK
[email protected]:~#

Step 7: Enable MariaDB to Start on Server Reboot

Run the below command to enable MariaDB to automatically start when server is rebooted

sudo systemctl enable mariadb

Step 8: Check MariaDB Version

To confirm the installed version, we need to first login to MySQL as below.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.6.3-MariaDB-1:10.6.3+maria~focal binary distribution

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

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

MariaDB [(none)]>

Now run the following command to check MariaDB version

MariaDB [(none)]> SELECT VERSION();
| VERSION()                            |
| 10.6.4-MariaDB-1:10.6.4+maria~bionic |
1 row in set (0.000 sec)

MariaDB [(none)]> QUIT

Step 9: MariaDB Basic Usage

Next, we are going to see some of the MariaDB Dababase basic usage such creating a database, users and so on.

MariaDB Create Database

As seen above, MariaDB uses MySQL syntax. To create a database, you first need to login to mariadb as shown above then run the below command to create a database.

#Create a new database
MariaDB [(none)]> CREATE DATABASE db1;
Query OK, 1 row affected (0.000 sec)

#If the database with the same exists
ERROR 1007 (HY000): Can't create database 'db1'; database exists

#Create a database if already exits
Query OK, 2 rows affected (0.009 sec)

#First check if a database exists 
Query OK, 1 row affected, 1 warning (0.000 sec)

# Check Databases MariaDB
MariaDB [(none)]>  SHOW DATABASES;

MariaDB add user and Grant Privileges

To create a user and grant privileges;

#Create user mariadb
MariaDB [(none)]> CREATE USER 'db1user'@'localhost' IDENTIFIED BY 'mypassword';

#Grant privileges to a specific database
MariaDB [(none)]> GRANT ALL PRIVILEGES ON db1.* TO 'db1user'@'localhost';

#Remember to refresh the privileges
MariaDB [(none)]> FLUSH privileges;

#To check user grants in MariaDB
MariaDB [(none)]> SHOW GRANTS FOR 'db1user'@'localhost';

Create a Table and Add Data MariaDB

Once you have created a database, you can create table and add data into it

MariaDB [(none)]> USE db1;
MariaDB [(none)]> CREATE TABLE employees (id INT, name VARCHAR(20), email VARCHAR(20));
MariaDB [(none)]> INSERT INTO employees (id,name,email) VALUES(01,"User1","[email protected]");

MariaDB Clean up

If you want to completely clean MariaDB installation, you can remove and do a re-install.

sudo apt purge mariadb-server
sudo rm -rf /var/lib/mysql/

This has been a guide n how to install MariaDB 10.6 on Ubuntu 20.04 and Ubuntu 18.04. It is a short and easy installation and I hope that this guide as been helpful.


Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.