MariaDB is a widely used database in Linux systems. In this guide, you will explore how to install the database on Ubuntu 22.04 with the help of a few easy-to-follow commands.

Once you've installed and configured MariaDB, you will learn how to interact with it through the MariaDB command line. In the end, you will also see a command to uninstall the database from Ubuntu.

What Is MariaDB?

MariaDB is a popular, free, and open-source database released as a replacement for MySQL. It is a fork of MySQL, which means MariaDB is an improved version of MySQL with similar but better functionality. Here's a guide to set up MySQL on Ubuntu if you'd like to try it out.

MariaDB is a fast, scalable, top-notch performing database, with support for more storage engines. It also maintains many plugins, tools, and utilities that make it one of the best databases out there. MariaDB is mostly used to maintain inventories, and manage transactions and customer information.

How to Install MariaDB on Ubuntu 22.04

Here’s how you can install MariaDB on Ubuntu 22.04:

Step 1: Update Your System

To begin with the installation, you first need to update packages on your Ubuntu system. You can achieve this by running the following command on Ubuntu’s terminal:

        sudo apt update && sudo apt upgrade
    

This may take some time, so hang on!

Step 2: Install the Necessary Packages for MariaDB

Once you have updated the system, you need to install some necessary packages required by MariaDB. Do this by executing the following command:

        sudo apt-get install wget software-properties-common dirmngr ca-certificates apt-transport-https -y
    

Now that you've installed the essential packages, you will finally install MariaDB in this step. To install the database, run the following command as a root user or with sudo:

        sudo apt install mariadb-server mariadb-client
    

To verify that MariaDB has been successfully installed on your system, check the version of the installed database by issuing the following command:

        mariadb --version
    

If you see the version number of the application in the output, this means that MariaDB exists on your system. This is one way to verify the installation. You can also check the active status of the installed database for verification.

Use the following command to check the status of MariaDB:

        systemctl status mariadb
    
MariaDB status is Active

As you can see, MariaDB is up and running.

Step 3: Secure Your MariaDB Database

Once you've installed and verified the database, the next step is to perform some essential configurations in MariaDB. After installation, it is a good practice to run a security script to protect the MariaDB database from being compromised or attacked. Execute the script with this command:

        sudo mysql_secure_installation
    

Next, the script will ask if you want to set a root password. Provide a secure password and hit Enter. Then, it'll ask you to reconfirm the password. Once the password is saved, you will get a “Success” message as shown below.

Output of a MariaDB script

After that, it will ask if you want to remove the anonymous user or not. It is recommended that you remove it.

It will also confirm if you want to disallow remote root login. If you disallow it, you can only log in as a root user from localhost.

mariadb secure installation script

By default, MariaDB installs a test database. The script will ask if you want to remove the database or keep it. It is better to delete it and create a new database. However, if you want to keep it for testing purposes, select no.

In the end, the script will take permission from you to reload privilege tables to update the new changes. To load them, enter Y. After you have made the changes, you will see a success message on the terminal.

mariadb secure installation script

How to Check if MariaDB Is Running

MariaDB begins to run as soon as it is installed. To test it, you can check the status of MariaDB with the following command:

        sudo systemctl status mariadb
    

If MariaDB is running fine, you will see the following output on Ubuntu’s terminal:

MariaDB status is Active

In case, MariaDB is down, you will see a similar output as shown below:

MariaDB status is down

How to Interact With MariaDB on Linux

Now that the installation and configurations are done, let’s explore how you can interact with the database.

1. Access the MariaDB Shell

To enter MariaDB’s shell, run the following command:

        sudo mariadb
    

With this command, the MariaDB monitor will open. Here you can run MariaDB queries to interact with the database.

MariaDB shell to run MariaDB queries

2. Create a MariaDB Admin Account

After getting access to MariaDB, the first thing you need to do is create a new account with the name “admin”. This account will have the same permissions as the root account, but you will mainly use it for password-based authentication.

To create the admin account with root privileges, use the command below. Make sure to use your password when issuing the command.

        GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
    

Next, flush the privileges to update the new changes in MariaDB.

        FLUSH PRIVILEGES;
    

You can now exit the MariaDB command line by typing:

        exit
    

3. Create a Database in MariaDB

To create a new database, make use of the following command and provide a name for your database in place of “TestDB”.

        CREATE DATABASE TestDB;
    

To check if the database has been successfully created, use this command to see all the existing databases in MariaDB:

        Show Databases;
    
A list of databases in MariaDB

As you can see TestDB now exists in the list of MariaDB databases.

How to Uninstall MariaDB From Ubuntu 22.04

In case, you no longer need MariaDB, you can easily uninstall it with the help of a basic Linux command. This command will completely remove all MariaDB folders and dependencies from your system in a few minutes.

        sudo apt-get purge mariadb-*
    
A window asking the user if he wants to remove MariaDB

Select Yes to continue uninstalling MariaDB.

You Can Now Take Full Advantage of MariaDB's Capabilities

Thanks to its amazing features and support, MariaDB is gaining popularity day by day and is becoming one of the most used databases in the market. Many big names such as Facebook and Google use MariaDB for its unique benefits over other databases.

For organizations who want complete flexibility over a database, MariaDB and PostgreSQL are the two best open-source options available.