
How to Install MariaDB on latest CentOS
Installing MariaDB on the latest CentOS is one of those tasks that seems straightforward until you hit a snag with package repositories or configuration issues. Whether you’re setting up a development environment or deploying a production database server, understanding the proper installation process can save you hours of troubleshooting. This guide walks you through the complete installation process, covers common gotchas, and provides configuration examples to get your MariaDB instance running smoothly on CentOS Stream or CentOS 8/9.
Why MariaDB Over MySQL
Before diving into the installation, it’s worth understanding why MariaDB has become the go-to choice for many developers. Created by the original MySQL developers, MariaDB offers better performance, more storage engines, and enhanced security features. It’s also the default MySQL replacement in most Linux distributions, including RHEL and CentOS.
Feature | MariaDB | MySQL |
---|---|---|
Performance | Generally faster, optimized queries | Good performance, Oracle optimizations |
Storage Engines | 12+ engines including Aria, TokuDB | Limited to InnoDB, MyISAM |
Licensing | GPL v2, fully open source | Dual license (GPL/Commercial) |
Thread Pool | Available in all versions | Enterprise edition only |
Installation Methods Overview
You’ve got three main approaches for installing MariaDB on CentOS:
- AppStream Repository – Default CentOS repos (usually older versions)
- MariaDB Official Repository – Latest stable releases with regular updates
- Source Compilation – Maximum customization but time-consuming
For most use cases, the official MariaDB repository is your best bet. It provides the latest stable versions with security patches and performance improvements.
Step-by-Step Installation Guide
Method 1: Using MariaDB Official Repository (Recommended)
First, let’s set up the official MariaDB repository. This ensures you get the latest stable version with ongoing updates.
# Create the MariaDB repository file
sudo tee /etc/yum.repos.d/mariadb.repo << EOF
[mariadb]
name = MariaDB
baseurl = https://rpm.mariadb.org/10.11/centos/\$releasever/\$basearch
gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled = 1
EOF
# Import the GPG key
sudo rpm --import https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
# Update package cache
sudo dnf clean all
sudo dnf makecache
Now install MariaDB server and client:
# Install MariaDB server and client
sudo dnf install -y mariadb-server mariadb
# Start and enable MariaDB service
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Check service status
sudo systemctl status mariadb
Method 2: Using CentOS AppStream Repository
If you prefer using the default CentOS repositories (note: usually older versions):
# Install from default repos
sudo dnf install -y mariadb-server mariadb
# Start and enable the service
sudo systemctl start mariadb
sudo systemctl enable mariadb
Initial Security Configuration
The default MariaDB installation is pretty insecure. Running the security script is crucial, especially for production environments:
# Run the security installation script
sudo mysql_secure_installation
You'll be prompted to:
- Set a root password (choose a strong one)
- Remove anonymous users (recommended: yes)
- Disallow root login remotely (recommended: yes for production)
- Remove test database (recommended: yes)
- Reload privilege tables (recommended: yes)
Basic Configuration and Testing
Let's verify the installation and perform basic configuration:
# Connect to MariaDB as root
mysql -u root -p
# Check MariaDB version and status
MariaDB [(none)]> SELECT VERSION();
MariaDB [(none)]> SHOW STATUS;
MariaDB [(none)]> EXIT;
Creating Your First Database and User
# Connect as root
mysql -u root -p
# Create a new database
MariaDB [(none)]> CREATE DATABASE myapp_db;
# Create a new user with permissions
MariaDB [(none)]> CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'secure_password123';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
# Verify the user was created
MariaDB [(none)]> SELECT User, Host FROM mysql.user WHERE User = 'myapp_user';
MariaDB [(none)]> EXIT;
Configuration File Customization
The main configuration file is located at /etc/my.cnf.d/mariadb-server.cnf
. Here's a sample configuration for a development environment:
# Backup the original configuration
sudo cp /etc/my.cnf.d/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf.backup
# Edit the configuration file
sudo nano /etc/my.cnf.d/mariadb-server.cnf
Add these optimizations under the [mysqld]
section:
[mysqld]
# Basic settings
bind-address = 127.0.0.1
port = 3306
# Performance tuning
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 2
query_cache_size = 32M
query_cache_type = 1
# Connection settings
max_connections = 100
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
# Logging
log_error = /var/log/mariadb/mariadb.log
slow_query_log = 1
slow_query_log_file = /var/log/mariadb/mariadb-slow.log
long_query_time = 2
Restart MariaDB to apply changes:
sudo systemctl restart mariadb
Firewall Configuration
If you need remote access to your MariaDB instance, configure the firewall:
# Allow MariaDB through firewall (only if needed for remote access)
sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --reload
# Or allow specific port
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
# Check firewall status
sudo firewall-cmd --list-all
Common Issues and Troubleshooting
Service Won't Start
If MariaDB fails to start, check the logs:
# Check service status
sudo systemctl status mariadb -l
# Check MariaDB logs
sudo journalctl -u mariadb -f
# Check error logs
sudo tail -f /var/log/mariadb/mariadb.log
Can't Connect to Database
Common connection issues and solutions:
# Check if MariaDB is listening on the correct port
sudo netstat -tlnp | grep :3306
# Test local connection
mysql -u root -p -h localhost
# Check user permissions
mysql -u root -p -e "SELECT User, Host, authentication_string FROM mysql.user;"
Performance Issues
Use these commands to diagnose performance problems:
# Check current configuration
mysql -u root -p -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
# Monitor active processes
mysql -u root -p -e "SHOW PROCESSLIST;"
# Check slow queries
mysql -u root -p -e "SHOW STATUS LIKE 'Slow_queries';"
Real-World Use Cases and Examples
Web Application Stack
For a typical LAMP/LEMP stack setup, you might want to configure MariaDB for web applications:
# Create a WordPress database setup
mysql -u root -p << EOF
CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'wp_secure_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EOF
Development Environment
For development work, you might want less restrictive settings:
# Allow root remote access (development only!)
mysql -u root -p << EOF
CREATE USER 'root'@'%' IDENTIFIED BY 'dev_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
Performance Benchmarking
Here's how to run basic performance tests using MariaDB's built-in tools:
# Install MySQL benchmark tools
sudo dnf install -y mariadb-test
# Run basic benchmark
mysqlslap --user=root --password --host=localhost --concurrency=20 --iterations=10 --create-schema=benchmark --query="SELECT BENCHMARK(1000000, ENCODE('hello','goodbye'));"
# More comprehensive test
mysqlslap --user=root --password --host=localhost --concurrency=50 --iterations=20 --number-of-queries=1000 --create="CREATE TABLE t1 (id INT, name VARCHAR(50));" --query="INSERT INTO t1 VALUES (1, 'test');" --delimiter=";" --create-schema=benchmark
Best Practices and Security Considerations
- Regular Updates - Keep MariaDB updated with security patches
- User Privileges - Follow the principle of least privilege for database users
- Network Security - Restrict network access when possible
- Backup Strategy - Implement regular automated backups
- Monitoring - Set up monitoring for performance and security events
Setting Up Automated Backups
# Create backup script
sudo tee /usr/local/bin/mariadb-backup.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/var/backups/mariadb"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
mysqldump --all-databases --routines --triggers --single-transaction \
--master-data=2 -u root -p > $BACKUP_DIR/full_backup_$DATE.sql
# Keep only last 7 days of backups
find $BACKUP_DIR -name "full_backup_*.sql" -mtime +7 -delete
EOF
# Make executable
sudo chmod +x /usr/local/bin/mariadb-backup.sh
# Add to crontab for daily backups at 2 AM
echo "0 2 * * * /usr/local/bin/mariadb-backup.sh" | sudo crontab -
Integration with Development Tools
MariaDB works seamlessly with popular development frameworks. Here are connection examples:
PHP Connection Example
<?php
$servername = "localhost";
$username = "myapp_user";
$password = "secure_password123";
$dbname = "myapp_db";
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4",
$username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Python Connection Example
# pip install mysql-connector-python
import mysql.connector
config = {
'user': 'myapp_user',
'password': 'secure_password123',
'host': 'localhost',
'database': 'myapp_db',
'charset': 'utf8mb4'
}
try:
connection = mysql.connector.connect(**config)
print("Connected to MariaDB successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")
Scaling and High Availability
For production environments requiring high availability, consider these options:
- Master-Slave Replication - For read scaling and backup
- Galera Cluster - For multi-master active-active clustering
- Load Balancing - Using tools like HAProxy or MaxScale
If you're planning to scale your database infrastructure, consider using dedicated hardware from MangoHost's dedicated servers for optimal performance, or start with their VPS solutions for development and testing environments.
Useful Resources and Next Steps
Once you have MariaDB running, explore these advanced topics:
Remember to monitor your database performance regularly and adjust configurations based on your specific workload requirements. MariaDB's flexibility and performance make it an excellent choice for everything from small web applications to large-scale enterprise deployments.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.