How to Install MariaDB on Ubuntu 22.04

Updated on 29 April, 2025
How to Install MariaDB on Ubuntu 22.04 header image

MariaDB is an open-source relational database management system that serves as a drop-in replacement for MySQL, offering improved reliability, performance, and flexibility. It uses the same SQL syntax as MySQL for creating and managing databases on a server.

This article explains how to install MariaDB on Ubuntu 22.04 and configure it for production use, enabling you to create and manage databases on your server.

Prerequisites

Before you begin:

Install MariaDB on Ubuntu 22.04

MariaDB is available in the default APT repositories on Ubuntu 22.04. Follow the steps below to install the MariaDB Server package and verify the system service status.

  1. Update the server package index.

    console
    $ sudo apt update
    
  2. Install MariaDB.

    console
    $ sudo apt install mariadb-server -y
    
  3. View the installed MariaDB version on your server.

    console
    $ mariadb --version
    

    Output:

    mariadb  Ver 15.1 Distrib 10.6.21-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
  4. Enable the system service to start at boot time.

    console
    $ sudo systemctl enable mariadb
    
  5. Start the MariaDB service on your server.

    console
    $ sudo systemctl start mariadb
    
  6. View the MariaDB service status to verify that it's running on your server.

    console
    $ sudo systemctl status mariadb
    

    Output:

    ● mariadb.service - MariaDB 10.6.21 database server
        Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
        Active: active (running) since Sat 2025-04-05 16:08:38 UTC; 12min ago
        Docs: man:mariadbd(8)
                https://mariadb.com/kb/en/library/systemd/
    Main PID: 2541 (mariadbd)
        Status: "Taking your SQL requests now..."
        Tasks: 7 (limit: 61946)
        Memory: 61.5M
            CPU: 406ms
        CGroup: /system.slice/mariadb.service
                └─2541 /usr/sbin/mariadbd

Secure the MariaDB Server

MariaDB runs with the default root user account without a password, allowing only privileged users to access the server. Follow the steps below to secure the MariaDB server by setting a root password, removing anonymous users, and disabling remote access for the root user.

  1. Run the following command to start the MariaDB security script.

    console
    $ sudo mysql_secure_installation
    
    • Press Enter to select none as the current root password.

      Enter current password for root (enter for none): 
    • Enter N and press Enter to use MariaDB without unix_socket authentication.

      Switch to unix_socket authentication [Y/n]
    • Enter Y and press Enter to change the default root user password.

      Change the root password? [Y/n] 
    • Enter a new strong password for the root user.

      New password: 
    • Re-enter the new root user password and press Enter to save changes.

      Re-enter new password: 
    • Enter Y and press Enter to delete anonymous users on the MariaDB server.

      Remove anonymous users? [Y/n]
    • Enter Y and press Enter to disable remote access to the database server root user.

      Disallow root login remotely? [Y/n]
    • Enter Y and press Enter to delete the test database.

      Remove test database and access to it? [Y/n]
    • Enter Y to refresh your MariaDB privilege tables and apply your new configuration changes.

      Reload privilege tables now? [Y/n]
  2. Restart MariaDB on your server.

    console
    $ sudo systemctl restart mariadb
    

Access MariaDB

MariaDB is compatible with graphical management tools like PhpMyAdmin, which integrate directly with the database server. You can access the MariaDB console using the mariadb or mysql command. Follow the steps below to access the MariaDB console and create sample databases on your server.

  1. Log in to the MariaDB database server.

    console
    $ mariadb -u root -p
    

    Enter the database root user password you set earlier when prompted.

  2. Create a new sample database. For example, exampledb.

    sql
    MariaDB [(none)]> CREATE DATABASE exampledb;
    
  3. View all databases on the server and verify that the new database is available.

    sql
    MariaDB [(none)]> SHOW DATABASES;
    

    Output:

    +--------------------+
    | Database           |
    +--------------------+
    | exampledb          |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.001 sec)
  4. Create a new database user with a strong password. For example, exampledb_user.

    sql
    MariaDB [(none)]> CREATE USER 'exampledb_user'@'localhost' IDENTIFIED BY 'secure_password';
    
  5. Grant the user full privileges to the exampledb database.

    sql
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON exampledb.* TO 'exampledb_user'@'localhost';
    
  6. Refresh the MariaDB privilege tables to apply the new database changes.

    sql
    MariaDB [(none)]> FLUSH PRIVILEGES;
    
  7. Exit the MariaDB database console.

    sql
    MariaDB [(none)]> EXIT;
    

Conclusion

In this article, you installed and configured MariaDB on an Ubuntu 22.04 server. You can now integrate the database server with your applications to use MariaDB as the backend. For more information and configuration options, please visit the official MariaDB documentation.

Comments

No comments yet.