Sunday, November 22, 2026
TR
LinuxOpen Source

How to Install MariaDB on Ubuntu 20.04

How to Install MariaDB on Ubuntu 20.04
Contents
  1. Prerequisites
  2. Step 1 – Set Up the Repository
  3. Step 2 – Install MariaDB on Ubuntu
  4. Step 3 – Post-Installation Setup
  5. Step 4 – Connect to MariaDB
  6. Step 5 – Manage the MariaDB Service
  7. Step 6 – Create a Database and User (Optional)
  8. Conclusion

MariaDB is an open-source relational database server. It is built by the original MySQL developers. MariaDB aims to maintain high compatibility with the MySQL server, APIs and commands.

This tutorial will help you install the MariaDB database server on your Ubuntu 20.04 LTS system.

Prerequisites

  • A running Ubuntu 20.04 system
  • Shell access with a root or sudo-privileged account

Step 1 – Set Up the Repository

The default package repositories contain older versions of the MariaDB server. It is therefore better to use the official repository, which provides the latest versions of the packages.

First, add the package signing key to your system by running the commands:

sudo apt install software-properties-common sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

Next, create an appropriate configuration file on your Ubuntu system. To create the file, run:

sudo nano /etc/apt/sources.list.d/mariadb.list

Add the following content to the file.

# MariaDB 10.4 Repository deb [arch=amd64] http://nyc2.mirrors.digitalocean.com/mariadb/repo/10.4/ubuntu focal main deb-src http://nyc2.mirrors.digitalocean.com/mariadb/repo/10.4/ubuntu focal main

Save and close the file.

Step 2 – Install MariaDB on Ubuntu

The official MariaDB repository contains the latest version of the MariaDB server packages.

After adding the package repository, run the following commands to install the MariaDB server packages and the required dependencies.

sudo apt update sudo apt install mariadb-server

The command above will install the MariaDB server, client tools and other required packages on your system.

Step 3 – Post-Installation Setup

The MariaDB package provides a command line utility to complete the post-installation steps. These include configuring a password for the root user and removing unnecessary databases from the default installation.

To run the MariaDB post-installation wizard, type:

sudo mysql_secure_installation

A wizard will prompt you for some input to set the MariaDB root user password. Secure your MariaDB installation by removing anonymous users, disabling root login from remote hosts and removing the test database.

The recommended choice is to answer yes ('y') to every question:

  • Enter current password for root (enter for none): [JUST PRESS ENTER]
  • Set root password? [Y/n] y
  • New password: [ENTER A SECURE PASSWORD]
  • Re-enter new password: [ CONFIRM YOUR PASSWORD ]
  • 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

Step 4 – Connect to MariaDB

The MariaDB packages provide a command line utility to connect to the database server from the command line. This is where you can fully manage your database server.

To connect to the MariaDB server, run the following command in the terminal:

mysql -u root -p

Enter the password of the root account configured in the previous step.

Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 57 Server version: 10.4.13-MariaDB-1:10.4.13+maria~focal-log mariadb.org 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)]>

Step 5 – Manage the MariaDB Service

Recent versions of Ubuntu use systemd to manage services. You can use the following commands to manage the MariaDB service on your system.

sudo systemctl start mariadb        # To start the service 
sudo systemctl stop mariadb         # To stop the service 
sudo systemctl status mariadb       # To check the service status 
sudo systemctl restart mariadb      # To stop and then start the service 

Step 6 – Create a Database and User (Optional)

This is an optional step for you. The following commands help you create a database, create a user, and assign privileges on the database to that user.

To use the commands below, you first need to connect to the MariaDB server from the command line.

CREATE DATABASE mydb;
CREATE USER ‘myuser’@’localhost’ IDENTIFIED BY ‘my_secure_password’;
GRANT ALL ON mydb.* TO ‘myuser’@’localhost’;
FLUSH PRIVILEGES;

Conclusion

In this tutorial, you learned how to install MariaDB on an Ubuntu Linux system.

Kerim Koç

Systems and infrastructure engineer. I write about Linux, open source, high availability and security.