MariaDB installation on Ubuntu Server 20.04

MariaDB is an open source relationaldatabase compatible with MySQL. It has prooven itself as a very stable, well performing and popular solution. It now often the choice for building a LAMP stack (Linux, Apache, MySQL, PHP).

How to install MariaDB

You can install it pretty easy and fast on Ubuntu Server.

You don’t need any prerequisites just a cloud server running Ubuntu Server 20.04.

Of course it’s recommended to start with an update of the packages lists:

apt update

The package we are installing is mariadb-server:

apt install mariadb-server

Once the installation is complete you can use the mysql client to connect to your new database instance.

MariaDB prompt

Some basic security and configuration

You can make some basic security improvements on you new installation by running the mysql_secure_installation script. You simply need to run it:

mysql_secure_installation

The script will guide you through several steps including:

  • Setting a password for root accounts.
  • Removing root accounts that are accessible from outside the local host.
  • Removing anonymous-user accounts.
  • Removing the test database, which by default can be accessed by anonymous users.

Running the mysql_secure_installation script is generally recommended for all installations including production ones.

The configuration files

To manage your database server you may need to change some MySQL configs at some point. These configuration files are in the /etc/mysql/ directory.

In MariaDB there is an additional mariadb.conf.d directory and there are the default configuration files:

/etc/mysql/mariadb.conf.d/50-client.cnf  
/etc/mysql/mariadb.conf.d/50-mysql-clients.cnf  
/etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf  
/etc/mysql/mariadb.conf.d/50-server.cnf

Starting, stopping and reloading

If you change some configuration you will need to restart the service.

Managing the mariadb service is very easy with systemctl. To restart the service:

systemctl reload mariadb

Checking the status:

systemctl status mariadb

And of course start and stop:

systemctl stop mariadb
systemctl start mariadb

Creating a database and an user

You now have a running and basically secured database installation. Usually the next step is to create your first database. To do this:

CREATE DATABASE db_name;

To create new database user:

CREATE USER 'user1'@'localhost' IDENTIFIED BY 'MyPassword123';

And finally to grant that user, access to the new database:

GRANT ALL PRIVILEGES ON db_name TO 'user1'@'localhost';

Summary

With these simple steps you now have a running MariaDB server. The secure installation script provides you a basic security. Congratulations!