Setting Up MariaDB Database
MariaDB is a powerful, open-source relational database management system that serves as a highly compatible drop-in replacement for MySQL. This guide will walk you through installing the latest version of MariaDB natively.
Hosting Tip: Production databases require high I/O throughput and low disk latency. We recommend hosting on a KVM Root Server with PCIe 4.0 NVMe SSDs in RAID with dedicated resources, ECC memory, and full root access.
1. Adding the Official MariaDB Repository
The default package repositories for your OS often contain older versions of MariaDB. To get the newest release, we need to add the official MariaDB repository.
# Install required software-properties-common package
sudo apt-get install software-properties-common dirmngr apt-transport-https -y
# Download and add the MariaDB GPG signing key
sudo curl -LsSO https://mariadb.org/mariadb_release_signing_key.asc
sudo chmod -c 644 mariadb_release_signing_key.asc
sudo chown root:root mariadb_release_signing_key.asc
sudo mv mariadb_release_signing_key.asc /etc/apt/trusted.gpg.d/
# Add the repository for the latest version (Example for Ubuntu 22.04/24.04 and MariaDB 11.4)
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mirrors.xtom.de/mariadb/repo/11.4/ubuntu focal main'(Note: Always check the MariaDB official repository tool for the exact repository link corresponding to your specific Linux distribution and the absolute latest MariaDB version).
2. Installing MariaDB
Once the repository is added, update your package lists and install the database server.
sudo apt update
sudo apt install mariadb-server mariadb-client -yVerify the installation and check the version:
mariadb --version3. Securing the Installation
MariaDB comes with a security script that helps you remove unsafe default settings, such as remote root login and anonymous users.
Run the secure installation wizard:
sudo mariadb-secure-installationYou will be prompted to answer several questions:
- Current root password: Press
Enter(since none is set yet). - Switch to unix_socket authentication:
Y - Change the root password:
Y(Set a very strong password!) - Remove anonymous users:
Y - Disallow root login remotely:
Y - Remove test database:
Y - Reload privilege tables:
Y
4. Creating a New Database and User
It's bad practice to use the root user for your applications. Instead, create a dedicated user and database for each app.
Log into the MariaDB shell as root:
sudo mariadbCreate a database and a user (replace myappdb, appuser, and securepassword with your own values):
-- Create a new database
CREATE DATABASE myappdb;
-- Create a new user configured to connect from localhost
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'securepassword';
-- Grant all privileges on the new database to the new user
GRANT ALL PRIVILEGES ON myappdb.* TO 'appuser'@'localhost';
-- Apply the changes
FLUSH PRIVILEGES;
-- Exit the shell
EXIT;5. Managing the MariaDB Service
Use systemctl commands to control your database server:
# Check the status of MariaDB
sudo systemctl status mariadb
# Restart the service
sudo systemctl restart mariadb
# Ensure MariaDB automatically starts on system boot
sudo systemctl enable mariadbLooking for the right Linux VPS?
Put your project into practice right away: Our KVM VPS provide full root access, lightning-fast NVMe storage, and reliable KVM isolation. Flexible prepaid billing with no subscription and personal Discord support.