MariaDB is an open-source relational database management system. It was originally designed as a backward-compatible, binary drop-in replacement of MySQL.

MariaDB is developed and maintained by the original developers of MySQL and by the open-source community.

This guide explains how to install and secure MariaDB on Ubuntu 20.04.

Prerequisites

We’re assuming that you have administrative access to the Ubuntu server, either as root or a user with sudo permissions.

Installing MariaDB on Ubuntu

At the time of writing this article, the latest MariaDB version available in Ubuntu’s repositories is version 10.3. To install it run the following commands:

sudo apt updatesudo apt install mariadb-server

Once the installation is completed, the MariaDB service will start automatically. To verify that the database server is running, type:

sudo systemctl status mariadb

The output should show that the service is enabled and running:

     ...

Securing MariaDB

MariaDB server package comes with a script named mysql_secure_installation that allows you to easily improve the database server security.

Execute the script without arguments:

sudo mysql_secure_installation

The script will prompt you to enter the root password:

Enter current password for root (enter for none):

Since you haven’t set the root password yet, just press “Enter” here.

On the next prompt, you will be asked to set a password for the MySQL root user:

Set root password? [Y/n] n

Type n. On Ubuntu, MariaDB root user is authenticated by the auth_socket plugin by default. This plugin works by checking whether the local system user invoking the client program matches the specified MariaDB user name.

Next, you’ll be asked to remove the anonymous user, restrict root user access to the local machine, remove the test database, and reload privilege tables. You should answer Y to all of the questions:

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

Login as root

To interact with the MariaDB server from the command line, use the mysql client utility or its alias mariadb. This tool is installed as a dependency of the MariaDB server package.

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file. This means that you can’t authenticate as root by providing a password.

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

sudo mysql

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 61
Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.04

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)]> Bye

If you want to login to your MariaDB server as root using an external program such as phpMyAdmin, you have two options.

The first one is to change the authentication method from auth_socket to mysql_native_password. You can do that by running the following command:

">ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';">FLUSH PRIVILEGES;

The second, recommended option is to create a new dedicated administrative user with access to all databases:

">GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';

You can name the administrative user anything you want, but make sure you use a strong password.

Conclusion

We have shown you how to install MariaDB on Ubuntu 20.04. Now that your database server is up and running, your next step could be to learn how to manage MariaDB user accounts and databases.

If you have any questions or feedback, feel free to leave a comment.