How to Install MariaDB on Rocky Linux 9

Updated on July 9, 2024
How to Install MariaDB on Rocky Linux 9 header image

Introduction

MariaDB is an open-source drop-in replacement for MySQL that offers increased efficiency, and improved database performance with support for a wide range of data types using multiple storage engines. MariaDB uses SQL (Structured Query Language) to manage and manipulate information in relational databases. This article explains how to install MariaDB on Rocky Linux 9 and set up databases on your server.

Prerequisites

Before you begin:

Install MariaDB

MariaDB is available in the default repositories on Rocky Linux 9. In the following steps, verify the available packages and install MariaDB on your server.

  1. Search all available MariaDB packages in the default DNF repositories.

    console
    $ sudo dnf list mariadb
    

    Output:

    Available Packages
    mariadb.x86_64                                   3:10.5.22-1.el9_2                                    appstream
  2. Install MariaDB on your server.

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

    console
    $ mariadb --version
    

    Output:

    mariadb  Ver 15.1 Distrib 10.5.22-MariaDB, for Linux (x86_64) using  EditLine wrapper

Manage the MariaDB System Service

  1. Enable the MariaDB service to automatically start at boot.

    console
    $ sudo systemctl enable mariadb
    

    Output:

    Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
  2. Start the MariaDB service:

    console
    $ sudo systemctl start mariadb
    
  3. Stop the MariaDB service:

    console
    $ sudo systemctl stop mariadb
    
  4. Restart the MariaDB service:

    console
    $ sudo systemctl restart mariadb
    
  5. View the MariaDB service status and verify that it's running.

    console
    $ sudo systemctl status mariadb
    

    Your output should be similar to the one below:

    ● mariadb.service - MariaDB 10.5 database server
         Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
         Active: active (running) since Wed 2024-06-26 14:46:30 UTC; 53min ago
           Docs: man:mariadbd(8)
                 https://mariadb.com/kb/en/library/systemd/
       Main PID: 57634 (mariadbd)
         Status: "Taking your SQL requests now..."
          Tasks: 9 (limit: 11073)
         Memory: 87.7M
            CPU: 848ms
         CGroup: /system.slice/mariadb.service
                 └─57634 /usr/libexec/mariadbd --basedir=/usr

Secure the MariaDB Server

The MariaDB package includes the mysql_secure_installation script used to disable default configurations and secure your MariaDB server. Follow the steps below to run the script and enable secure authentication for all database users on your server.

  1. Run the secure installation script.

    console
    $ sudo mysql_secure_installation
    
    • Press Enter when prompted to enter the default root user password.
    Enter current password for root (enter for none): 
    • Enter Y and press Enter to enable secure socket authentication on your server.
    Switch to unix_socket authentication [Y/n] y
    • Enter Y and press Enter to set a new database root user password.
    Change the root password? [Y/n] y
    • Enter Y and press Enter when prompted to remove anonymous users on your server.
    Remove anonymous users? [Y/n] y
    • Enter Y when prompted to disable remote access to the database root user account.
    Disallow root login remotely? [Y/n] y
    • Enter Y to remove the default MariaDB test database on your server.
    Remove test database and access to it? [Y/n] y
    • Reload the MariaDB privilege tables to apply changes.
    Reload privilege tables now? [Y/n] y
     ... Success!

Access MariaDB

  1. Log in to the MariaDB console as the root database user.

    console
    $ mariadb -u root -p
    

    Enter the root user password you set earlier to access the database console.

  2. Create a new sample database example-vultr.

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

    sql
    MariaDB [(none)]> SHOW DATABASES;
    

    Output:

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

    sql
    MariaDB [(none)]> CREATE USER 'vultr_user'@'localhost' IDENTIFIED BY 'STRONG-PASSWORD';
    
  5. Grant the user full privileges to the example_vultr database.

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

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

    sql
    MariaDB [(none)]> EXIT
    
  8. Log in to the MariaDB console using the new database user and password.

    console
    $ mariadb -u vultr_user -p
    

Conclusion

You have installed MariaDB on your Rocky Linux 9 server and secured the database server to enable the creation of relational databases. MariaDB integrates with other applications on your server using modules compatible with MySQL. For more information and configuration options, please visit the official MariaDB documentation.