Menu

How to Install MariaDB on Ubuntu 22.04

Install and verify MariaDB from Ubuntu 22.04 (Jammy Jellyfish)’s APT repositories, enable the service, and create a database user.

Posted on By Updated on
On this page

Ubuntu 22.04 (Jammy Jellyfish) is an LTS release, but it is no longer the newest Ubuntu LTS. This tutorial installs the MariaDB package provided by Ubuntu 22.04’s own APT repositories. For MariaDB 12.3 LTS on the current Ubuntu 26.04 release, see our MariaDB installation guide for Ubuntu 26.04.

Prerequisites

Before you begin, make sure you have:

  1. An Ubuntu 22.04 system.
  2. Access to a terminal with sudo or root privileges.
  3. An active internet connection.

Step 1: Update Your System

To ensure your Ubuntu system is up to date with the latest security patches and software, open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will update your system’s package repository and upgrade installed packages.

Step 2: Install MariaDB

MariaDB is available in the Ubuntu 22.04 repositories, making installation straightforward. Use the following command to install MariaDB:

sudo apt install mariadb-server

During the installation, you will be prompted to confirm the package installation. Type ‘Y’ and press Enter.

Step 3: Start and Enable MariaDB

After the installation, start the MariaDB service and enable it to start at boot using the following commands:

sudo systemctl start mariadb
sudo systemctl enable mariadb

You can verify that MariaDB is running with this command:

sudo systemctl status mariadb

Step 4: Secure Your MariaDB Installation

MariaDB comes with a script that helps you secure your database installation. Run the following command:

sudo mysql_secure_installation

You’ll be prompted to set a root password, remove anonymous users, disallow root login remotely, and remove the test database. Follow the prompts and answer ‘Y’ or ‘N’ as needed.

Step 5: Log In to MariaDB

Now that your MariaDB installation is secure, you can log in to the database server using the following command:

sudo mysql -u root -p

Enter the root password you set during the installation when prompted.

Step 6: Create a New Database and User

To create a new database and user, follow these steps:

  1. Log in to MariaDB as the root user.

  2. Create a new database (replace mydatabase with your preferred name):

    CREATE DATABASE mydatabase;
    
  3. Create a runtime application account with row-level privileges. Use a separate migration account for schema changes; replace myuser and the password placeholder with the intended account name and a unique secret from a password manager:

    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'replace-with-a-unique-secret';
    GRANT SELECT, INSERT, UPDATE, DELETE ON mydatabase.* TO 'myuser'@'localhost';
    
  4. Exit the MariaDB prompt:

    exit;
    

Step 7: Test Your MariaDB Installation

You can test your new database and user by logging in with the created credentials:

mysql -u myuser -p

Enter the password when prompted, and you should now have access to the database you created.

Conclusion

You’ve installed MariaDB from Ubuntu 22.04’s repositories and created a database and user. If you are setting up a new Ubuntu 26.04 host and want MariaDB 12.3 LTS, continue with our Ubuntu 26.04 installation guide.