How to Set Up MariaDB on a VPS: Installation and Initial Configuration

How to Set Up MariaDB on a VPS: Installation and Initial Configuration

MariaDB has become the go-to MySQL-compatible database for many developers and system administrators.

As a true open-source fork of MySQL with active development and performance improvements, it’s an excellent choice for WordPress sites, web applications, and data-driven projects.

Why Mariadb?

MariaDB maintains binary drop-in compatibility with MySQL, meaning most applications work without modification. It also ships with features like Aria storage engine for crash recovery, thread pool for better concurrency, and Galera Cluster for multi-master replication.

Installing Mariadb

On Ubuntu/Debian:

sudo apt update
sudo apt install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

On AlmaLinux/RHEL:

sudo dnf install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

Securing Mariadb

After installation, run the security script:

sudo mariadb-secure-installation

You’ll be prompted to:
• Set root password
• Remove anonymous users
• Disallow root login remotely
• Remove test database
• Reload privilege tables

Initial Configuration

Edit the configuration file to optimize performance:
/etc/mysql/mariadb.conf.d/50-server.cnf (Ubuntu)
/etc/my.cnf.d/server.cnf (AlmaLinux)

Key settings:
innodb_buffer_pool_size = 2G  # 50-70% of RAM
max_connections = 150
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow-query.log
long_query_time = 2

Creating Databases And Users

Log into MariaDB:

sudo mariadb -u root -p

Create database:

CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Create user and grant privileges:

CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;

ENABLING REMOTE ACCESS (Optional)

Edit configuration and change bind-address to 0.0.0.0
Configure firewall to allow port 3306
Restart MariaDB

Setting Up Automated Backups

Create backup script at /usr/local/bin/mariadb-backup.sh:

#!/bin/bash
BACKUP_DIR="/var/backups/mariadb"
DATE=$(date +%Y%m%d)
mysqldump -u root -p'password' --all-databases | gzip > $BACKUP_DIR/backup_$DATE.sql.gz
find $BACKUP_DIR -mtime +7 -delete

Schedule with cron for daily 2 AM backups:

0 2 * * * /usr/local/bin/mariadb-backup.sh

Need managed MariaDB hosting? InMotion Hosting VPS plans include root access for MariaDB installation, and our managed VPS tier includes database optimization and monitoring as part of Launch Assist.

Share this Article

Leave a Reply