How to Install MariaDB on Ubuntu 24.04

Updated on June 12, 2024
How to Install MariaDB on Ubuntu 24.04 header image

Introduction

MariaDB is an open-source relational database management system that works as a drop-in replacement for MySQL with improved reliability, performance, and flexibility. MariaDB uses the same SQL syntax as MySQL to enable the creation and management of databases on a server.

This article explains how to install MariaDB on a Ubuntu 24.04 server. In addition, you will set up MariaDB with production configurations to enable the creation and management of databases on the server.

Prerequisites

Before you begin:

Install MariaDB

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

  1. Install MariaDB.

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

    console
    $ mariadb --version
    

    Output:

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

    console
    $ sudo systemctl enable mariadb
    
  4. Start MariaDB on your server.

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

    console
    $ sudo systemctl status mariadb
    

    Output:

    ● mariadb.service - MariaDB 10.11.7 database server
         Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: enabled)
         Active: active (running) since Mon 2024-06-03 06:11:17 UTC; 8h ago
           Docs: man:mariadbd(8)
                 https://mariadb.com/kb/en/library/systemd/
        Process: 19853 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
        Process: 19863 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
        Process: 19892 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl>    Process: 20014 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
        Process: 20016 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
       Main PID: 19994 (mariadbd)
         Status: "Taking your SQL requests now..."
          Tasks: 10 (limit: 14978)
         Memory: 79.1M (peak: 81.5M)
            CPU: 6.694s
         CGroup: /system.slice/mariadb.service
                 └─19994 /usr/sbin/mariadbd

Secure the MariaDB Server

MariaDB runs with the default root user account without a default password on your server. As a result, only privileged users can access the MariaDB server to create and manage databases. Follow the steps below to secure the MariaDB server with root password, remove anonymous users and disable 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]

Access MariaDB

MariaDB is compatible with graphical management interfaces such as PhpMyAdmin that integrate directly with the database server console. You can access the MariaDB database server console using the mariadb command or mysql. Follow the steps below to access the MariaDB console and create sample databases on the server.

  1. Log in to the MariaDB database server.

    console
    $ sudo 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 'vultr_user'@'localhost' IDENTIFIED BY 'secure_password';
    
  5. Grant the user full privileges to the exampledb database.

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

    sql
    MariaDB [(none)]> FLUSH PRIVILEGES;
    

Conclusion

You have installed and configured MariaDB on a Ubuntu 24.04 server. You can integrate the database server with your existing applications to use MariaDB as the backend. For more information and configuration options, please visit the official MariaDB documentation.