How to Install and Start Using MariaDB on Ubuntu 20.04 (Step-by-Step Guide)

Published on September 10, 2024, Updated on December 17, 2024

MariaDB is an open-source, freely accessible database management software that serves as a viable alternative to Oracle's MySQL database server. It's a community-supported, multi-user, and multi-threaded database server. 

It originated from the efforts of the original Oracle MySQL database server community developers. Moreover, you can install the MariaDB server on a wide range of operating systems, including various Linux distributions.

When compared to MySQL, MariaDB offers superior performance, speed, and security. Additionally, it surpasses MySQL in terms of replication, thanks to its efficient asynchronous Master-slave (MariaDB Client and MariaDB Server) replication system.

This article will demonstrate the step-by-step guidelines on how to install MariaDB on Ubuntu 22.04in two different ways. In addition, after installing MariaDB, we will learn how to start using it on the Ubuntu 22.04 system. 

METHODS TO INSTALL MARIADB ON UBUNTU 22.04

You can install the MariaDB server on Ubuntu using two different methods that we will discuss in this section. 

  1. Install MariaDB on Ubuntu using Ubuntu APT repositories.
  2. Install MariaDB using the Official MariaDB repository.

METHOD 1: INSTALLING MARIADB ON UBUNTU 22.04 USING UBUNTU APT REPOSITORIES

This method is the quickest and easiest way to install MariaDB on Ubuntu 22.04. However, this method will not install the latest MariaDB version on your Ubuntu system. To install MariaDB from Ubuntu APT repositories, follow the below-given steps:

STEP 1: UPDATE UBUNTU APT REPOSITORIES

It is recommended that you first update all Ubuntu APT repositories before installing any new packages on your system. You can update the system APT repositories using this command:

$ sudo apt update

STEP 2: INSTALL MARIADB ON UBUNTU

You can install MariaDB on Ubuntu 22.04 from the default Ubuntu APT repositories. To do this, type the following command on the terminal:

$ sudo apt install mariadb-server mariadb-client

After executing the above command, a confirmation prompt will display on the terminal window. Press “y” and hit “Enter” to download and install all required MariaDB packages on your Ubuntu system. In a while, you will notice the installation is completed on your system, and you will see the following output:

STEP 3: VERIFY THE MARIADB INSTALLATION

You can verify the MariaDB installation on your system by displaying the MariaDB version using the following command:

$ sudo mysqladmin version

After running the above command, you will see the installed MariaDB version on the terminal window as follows:

METHOD 2: INSTALLING MARIADB ON UBUNTU 22.04 USING THE OFFICIAL MARIADB REPOSITORIES

As we mentioned above, installing MariaDB using Ubuntu APT repositories doesn’t provide you with the latest MariaDB version. However, you can install the latest MariaDB version from the official MariaDB repositories. 

This method is the recommended way to install MariaDB on the Ubuntu system. In this instance, you need to use the official MariaDB repository to install the latest MariaDB version. To install MariaDB using the official MariaDB repository, follow the steps below:

STEP 1: INSTALL PREREQUISITES OR DEPENDENCIES

First, install all required packages and dependencies that are necessary to run MariaDB on Ubuntu 22.04:

$ sudo apt install -y software-properties-common

STEP 2: IMPORT THE GPG KEY

In this step, you will import the GPG key using the following command:

$ sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

STEP 3: ADD THE MARIADB APT REPOSITORY

After successfully importing the GPG signing key, you will include the MariaDB APT repository in your Ubuntu system. You can enable the repository specifically for MariaDB version 10.11, but you can replace this version based on your requirements with the one you wish to install. At the time of writing this article, the stable MariaDB version is 10.11.

$ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mariadb.mirror.liquidtelecom.com/repo/10.11/ubuntu focal main'

STEP 4: UPDATE THE LOCAL APT REPOSITORIES

To complete the process, ensure that you refresh your local repositories. After that, install the MariaDB server and MariaDB client by using the APT package manager:

$ sudo apt update 

$ sudo apt install mariadb-server mariadb-client

The above command will install the MariaDB 10.11 version, including all additional required dependencies and packages on Ubuntu 22.04.

STEP 5: CHECK MARIADB VERSION

After installing MariaDB on Ubuntu, verify its installation. Check the installed MariaDB version using the following command:

$ mariadb --version 

STEP 6: ENABLE AND START THE MARIADB SERVICE

The MariaDB database will automatically start running on your system upon successful installation. You can check the MariaDB running status using the following command:

$ sudo systemctl status mariadb

As you can see, the MariaDB server is active and running on this system. If this is not enabled and running by default on your system, you can enable and start the MariaDB service using the following commands:

$ sudo systemctl start mariadb

$ sudo systemctl enable mariadb

The command mentioned above guarantees that MariaDB will initiate automatically whenever you restart or reboot your system.

STEP 7: START AND USE MARIADB

Now, you can start using the MariaDB database on your system. To start the MariaDB database, use the following command:

$ sudo mariadb

As you can see, the MariaDB shell launched on the terminal where you can run database commands. 

CONFIGURE SECURE MARIADB INSTALLATION 

It's recommended that you execute the security script provided by MariaDB to improve the security of your MariaDB installation. This script is designed to secure the MariaDB installation by addressing less secure configuration issues that may exist, such as eliminating sample users and preventing remote root logins. To make your MariaDB configuration secure, run the following security script:

$ sudo mysql_secure_installation

After executing the above command, you will need to answer a series of user confirmation prompts that will change the security options for the MariaDB database secure configuration:

  1. In the first user prompt, you will provide the current root account password. If none is set up yet, just press “Enter” from your keyboard.
  2. Set up a root password for your MariaDB database. It ensures that no one can access or log in as a root user inside your database without password authentication. So, here, you will press the “y” key. Enter the password for your database and then confirm it. Here, we have already set a secure root password. However, you will change it based on your requirements.

After that, you will need to press “y” to configure the secure MariaDB installation for the following options:

  • Removes anonymous users from the database server: “Y.”
  • Disables remote root login: “Y.” This option ensures that only the root user can access and log in to the MariaDB database server using “localhost.”
  • Remove the test database that comes with MariaDB by default: “Y.”
  • Reload privilege tables now. “Y” ensures that all changes take effect immediately. 

Congratulations! We have successfully configured the secure installation plugin for the MariaDB database. If you are using a VPS server, you can follow the same steps to configure this plugin on your Linux server.

CREATE AN ADMINISTRATIVE USER FOR PASSWORD AUTHENTICATION

To create an administrative user, you will create a new user account using the MariaDB database and assign privileges to that user. First, log in as root user to access the MariaDB database:

$ sudo mariadb -u root -p 

Now, create a user with your desired name and assign a password to this user using the following command:

> CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'secure_password' WITH GRANT OPTION;

In the command above, replace “user_name” and “secure_password” with your credentials.

You can also grant privileges separately using the following command:

GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'localhost';

The use of the "." wildcard notation signifies that the user is granted permission to perform any operation on any database within the database server.

To commit changes or apply all changes, run the following command:

> FLUSH PRIVILEGES;

To exit from the MariaDB database, use this command:

>exit

TEST MARIADB CONFIGURATION FOR ADMINISTRATIVE USER

Once you configured your MariaDB database on Ubuntu, you can now log in to your database and execute queries. To log in to the database, you need to use the above administrative user credentials. To do this, execute the following command:

$ sudo mariadb -u samreena -p

After running this command, enter your database administrative user password and hit “Enter.” The following screen will display on your terminal:

LIST ALL DATABASES

Here, you can run different database queries. For example, let’s say you want to list all MariaDB databases. To show all databases, use the following command:

> show databases

CREATE A NEW DATABASE

To create a new database, execute the following database query:

> create database new_testdb

LIST USERS

To list all localhost users for your database, run this query:

> select host, user from mysql.user;

CONCLUSION

In this tutorial, we learned how to install MariaDB on the Ubuntu 22.04 system using two different methods. First, how to install MariaDB using the Ubuntu APT repository and second, how to install MariaDB from the official MariaDB repositories. 

In addition, we have also figured out how to configure the secure MariaDB installation and execute queries on it. If you want to learn more about MariaDB database commands and repositories, you can visit the official MariaDB documentation.