How to install MariaDB on Ubuntu 22.04

Updated on 2026-10-07 14:00:44 16 visits

What is MariaDB?

MariaDB is a very popular open-source database; furthermore, its use has spread across web applications worldwide. It was created to provide a community-driven and free alternative to MySQL.

MariaDB is compatible with most applications written for MySQL. It is often used as a replacement for MySQL in operating systems such as Ubuntu. It is worth noting that it offers high availability, performance, and scalability, making it an excellent choice for web and server applications.

Ubuntu users, like you, will be able to easily install this database on their web server thanks to this manual. In other words, here you will learn how to install the MariaDB package, configure the database, and verify the installation. Keep reading to find out.

Step 1: System Update

Before starting the installation, first, it is important that you update the system. In this way, you ensure that you are using the latest versions of the system packages, thus avoiding any possible problem or version conflict. To do this, you must open the terminal and run the following command:

sudo apt-get update

This command will update the list of available packages in the Ubuntu repositories. Then, run the following command to update the packages installed on your system:

sudo apt-get upgrade

This command will update all packages installed on your Ubuntu system to the latest available version.

info Keep in mind that some packages may require confirmation before proceeding with the update. Once the update is complete, you will be ready to continue with the installation of MariaDB.

Step 2: Add the MariaDB Repository

The second step is to add the MariaDB repository to the system. This will allow the system to download and install the most recent version of MariaDB available in the repository. To do this, follow these steps:

1. Add the MariaDB repository signing key to the system

This command is used to add this key:

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

2. Add the MariaDB repository to the system:

Here we show you the command to add it:

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirror.23media.com/mariadb/repo/10.7/ubuntu jammy main'

info Here, MariaDB version 10.7 and Ubuntu version Jammy Jellyfish (22.04) are used. If you are using a different version of Ubuntu or wish to install a different version of MariaDB, make sure to change the corresponding values in the command.

3. Update the system package list again

Once you have added the MariaDB repository to the system, you must update the system package list again so that the system has access to the latest version of MariaDB. Here we show you the command to do it:

sudo apt-get update

Step 3: Install the MariaDB Package:

Now it is time to install the MariaDB package. Once you have added the MariaDB repository to the system, you can install the MariaDB package using the following command in the terminal:

sudo apt-get install mariadb-server

This command will download and install the MariaDB package on your Ubuntu system. During the installation process, you will be asked to enter a password for the MariaDB root user. Make sure to choose a secure password and remember it, as you will need it later to log into the database.

Once the installation is complete, the MariaDB service will start automatically and run in the background. You can verify if the service is running correctly using this command:

sudo systemctl status mariadb

This command will show you the current status of the MariaDB service and should indicate that it is running correctly. With this, the MariaDB installation on Ubuntu has been completed successfully.

Step 4: Configure MariaDB Security

By default, the MariaDB installation does not have a very strict security configuration, which can make the database vulnerable to external attacks.

To improve the security of your MariaDB instance, follow these steps:

  1. Run this command to start the MariaDB security script
sudo mysql_secure_installation
  1. The script will guide you through a series of questions to configure the security of your MariaDB instance. Below, we list the questions and the recommended answers:
  • Enter the MariaDB root password: Enter the password you chose during the MariaDB installation.

  • Do you want to switch to the unix_socket authentication socket?: unix_socket authentication in MariaDB uses the operating system identity instead of passwords to authenticate users.

  • Do you want to change the MariaDB root password? If you chose a secure password during installation, you can opt not to change it. Otherwise, change the password to one that is secure.

  • Do you want to remove anonymous users?: We strongly recommend selecting "yes" for security reasons.

  • Do you want to disable remote root access to the database? Say "yes" to disable remote access, as this limits database access via the command line.

  • Do you want to remove the MariaDB test database? Again, answer "yes" to remove the test database, as this database is not necessary and can be a security risk.

  • Do you want to reload the privileges now? Here, also answer "yes" to save the changes and apply them to your MariaDB instance.

info Once you have completed the security script, the security configuration of your MariaDB instance should be more secure and better protected against potential threats.

Step 5: Configure MariaDB for Web Server

Now it's time to configure the database to use it with a web server, for example, Apache or Nginx. To do this, you must follow these steps:

1. Open the MariaDB configuration file

Here is the command to carry it out:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

2. Look for the [mysqld] section

Make sure that the configuration of the "bind-address" and "default_authentication_plugin" parameters is as follows:

bind-address = 127.0.0.1
default_authentication_plugin = mysql_native_password

The first line specifies the IP address to which the database server is bound. In this case, the web server running on the same server as the database. The second line sets the default authentication plugin, necessary for authentication to work with some content management systems, such as WordPress.

When you have finished this step, you must save and close the configuration file.

3. Restart the MariaDB service

You must do this with this command:

sudo systemctl restart mariadb

4. Log in to MariaDB as root user

Run the following command to start the session:

sudo mysql -u root -p

5. Create a new database and a new user for your website

Here is the command you can use to create them:

CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

In this command, mydatabase is the name of the database you want to create, myuser is the username you are going to create, and mypassword is the password you set for that user. It is important to note that you must ensure the user has all the necessary privileges in the database.

success With these steps, your MariaDB database on Ubuntu 22.04 is configured for use with a web server. And it is now ready for you to make the most of it.

Step 6: Grant Permissions

To finish, we will show you how you can grant permissions to users so they can access the database. Follow these steps and you will know how to do it:

1. Log in to MariaDB as the root user

Here is the command to do it:

sudo mysql -u root -p
  1. Grant the user permissions to access a database

You can carry it out with the following command:

GRANT permission_type ON database_name.* TO 'username'@'localhost';

In this command, permission_type is the type of permission you wish to grant to the user. For example, SELECT, INSERT, UPDATE, DELETE, etc. Then, database_name is the name of the database you want the user to have access to. If you want to grant access to all databases, use the asterisk (*) instead of the database name. Finally, username is the username to whom you want to grant permissions.

3. Update the privileges

We show you how:

FLUSH PRIVILEGES;

4. Close the MariaDB session

To finish, just run this command:

exit;

info With these steps, you have granted permissions to a user to access a specific database in your MariaDB installation on Ubuntu 22.04.

Conclusion

By following the steps indicated in this manual, you can have the MariaDB database installed on your Ubuntu server in a very short time. Security configuration and granting permissions are critical steps that must be followed carefully to ensure secure and correct access to the database. With MariaDB installed and configured correctly, you can make the most of the advantages of this powerful database management system on your web server.