How to Install MariaDB on Ubuntu

Install MariaDB on Ubuntu

Installing a reliable database management system is crucial for any Ubuntu server setup, and MariaDB has emerged as the go-to choice for developers and system administrators worldwide. Whether you’re setting up a web application, managing enterprise data, or building a development environment, this comprehensive guide will walk you through every step of installing MariaDB on Ubuntu.

Table of Contents

What is MariaDB and Why Choose It?

MariaDB is an open-source relational database management system that serves as a powerful alternative to MySQL. Created by the original developers of MySQL, MariaDB is designed to be a drop-in replacement that offers enhanced performance, improved security features, and better compatibility with modern applications.

The database system has gained tremendous popularity among developers and organizations because it provides superior efficiency in handling complex queries compared to traditional MySQL installations. With over 12 million installations worldwide, MariaDB powers everything from small personal websites to large enterprise applications.

Key advantages of choosing MariaDB include:

  • Enhanced performance for complex database operations
  • Improved security features with regular security updates
  • Better storage engine options including Aria and TokuDB
  • Active community support and regular feature updates
  • Complete MySQL compatibility for seamless migration

Prerequisites for Installation

Before diving into the installation process, ensure your system meets these essential requirements:

  • Ubuntu 18.04 or higher (this guide covers Ubuntu 20.04, 22.04, and 24.04)
  • Root or sudo privileges for system administration tasks
  • Active internet connection for downloading packages
  • At least 1GB of available disk space for MariaDB installation
  • Basic terminal knowledge for command execution

You should also have a non-root administrative user configured and consider setting up UFW firewall rules for enhanced security.

Ubuntu Version Compatibility

MariaDB installation varies slightly across Ubuntu versions, with each offering different default versions:

  • Ubuntu 20.04: Ships with MariaDB 10.3 by default
  • Ubuntu 22.04: Includes MariaDB version 10.5.12
  • Ubuntu 24.04: Features MariaDB 10.11.7 or newer

These version differences don’t affect the installation process significantly, but knowing your system’s default version helps in troubleshooting and optimization.

Method 1 – Installing MariaDB from Ubuntu Repositories

The most straightforward approach is installing MariaDB directly from Ubuntu’s official repositories. This method ensures stability and automatic security updates through your system’s package manager.

Step 1: Update Your System

Start by updating your package index to ensure you’re working with the latest available packages:

sudo apt update

This command refreshes your system’s package list, preventing compatibility issues and ensuring you get the most recent stable version available in Ubuntu’s repositories.

Step 2: Install MariaDB Server Package

Install the MariaDB server package using the following command:

sudo apt install mariadb-server -y

For a more comprehensive installation that includes both server and client tools, use:

sudo apt install mariadb-server mariadb-client -y

The -y flag automatically confirms installation prompts, streamlining the process. The installation typically takes 2-5 minutes depending on your internet connection and system specifications.

Step 3: Verify Installation Success

Check if MariaDB installed correctly by viewing the installed version:

mariadb --version

You should see output similar to:

mariadb Ver 15.1 Distrib 10.11.7-MariaDB, for debian-linux-gnu (x86_64)

Step 4: Start and Enable MariaDB Service

Ensure MariaDB starts automatically at boot time and is currently running:

sudo systemctl enable mariadb
sudo systemctl start mariadb

Verify the service status to confirm it’s active and running:

sudo systemctl status mariadb

A successful installation will show the service as “active (running)” with no error messages.

Method 2 – Installing from Official MariaDB Repository

For accessing the latest MariaDB features and versions, installing from the official MariaDB repository provides more current releases than Ubuntu’s default repositories.

Adding the MariaDB GPG Key

First, add the MariaDB GPG key to verify package authenticity:

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

Configuring the Repository

Add the official MariaDB repository to your system:

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.accretive-networks.net/mariadb/repo/10.3/ubuntu bionic main'

If you encounter an “add-apt-repository command not found” error, install the required package first:

sudo apt install software-properties-common

Installing Latest MariaDB Version

Update your package list and install MariaDB from the new repository:

sudo apt update
sudo apt install mariadb-server

This method ensures you get the most recent stable release with the latest features and security improvements.

Essential Post-Installation Configuration

After installation, securing your MariaDB setup is crucial for production environments. The default installation leaves several security vulnerabilities that need addressing.

Running the Security Installation Script

MariaDB includes a built-in security script that addresses common security concerns:

sudo mysql_secure_installation

This interactive script will guide you through several important security configurations:

  1. Setting a root password (if not already set)
  2. Removing anonymous users that can access your database
  3. Disabling remote root login to prevent unauthorized access
  4. Removing test databases that shouldn’t exist in production
  5. Reloading privilege tables to activate changes immediately

Setting Up Root Password

During the security script, you’ll be prompted to set a strong root password. Choose a complex password containing:

  • At least 12 characters
  • Mix of uppercase and lowercase letters
  • Numbers and special characters
  • No dictionary words or personal information

Configuring Database Security Settings

Answer “Y” (Yes) to most security script prompts for maximum protection:

  • Remove anonymous users: Yes
  • Disallow root login remotely: Yes
  • Remove test database: Yes
  • Reload privilege tables: Yes

These settings create a secure foundation for your MariaDB installation suitable for production use.

Testing Your MariaDB Installation

Verifying your installation works correctly prevents future headaches and ensures everything is configured properly.

Checking Service Status

Confirm MariaDB is running smoothly:

sudo systemctl status mariadb

Look for “Active: active (running)” status, which indicates successful operation.

Logging Into MariaDB Console

Access the MariaDB command-line interface:

sudo mariadb

Or using the traditional MySQL client syntax:

mysql -u root -p

Enter your root password when prompted. A successful login displays the MariaDB prompt:

MariaDB [(none)]>

Performing Basic Database Operations

Test basic functionality with these commands:

SHOW DATABASES;
CREATE DATABASE test_db;
USE test_db;
SHOW TABLES;

These commands verify that MariaDB can create databases, switch contexts, and display information correctly.

Creating and Managing Database Users

While the root user provides full administrative access, creating dedicated users with specific privileges improves security and follows database best practices.

Creating Administrative Users

The root MariaDB user typically authenticates using the unix_socket plugin rather than passwords. For applications requiring password authentication, create a dedicated administrative user:

GRANT ALL ON *.* TO 'admin_user'@'localhost' IDENTIFIED BY 'secure_password' WITH GRANT OPTION;

Replace ‘admin_user’ and ‘secure_password’ with your chosen credentials.

Setting User Permissions and Privileges

Create users with specific permissions for different applications:

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'app_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON app_database.* TO 'app_user'@'localhost';

Always flush privileges after making user changes:

FLUSH PRIVILEGES;

User Management Best Practices

Follow these security principles:

  • Create separate users for different applications
  • Grant minimum required permissions only
  • Use strong, unique passwords for each user
  • Regularly review and audit user permissions
  • Remove unused accounts promptly

Performance Optimization and Configuration

Fine-tuning your MariaDB installation improves performance and resource utilization, especially important for production environments.

Basic Configuration Tweaks

Edit the main configuration file located at /etc/mysql/mariadb.conf.d/50-server.cnf:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Key settings to consider:

  • innodb_buffer_pool_size: Set to 70-80% of available RAM
  • max_connections: Adjust based on expected concurrent users
  • query_cache_size: Enable for read-heavy applications

Memory and Resource Settings

For systems with 4GB RAM or more, consider these optimizations:

innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_limit = 2M
query_cache_size = 64M

Restart MariaDB after configuration changes:

sudo systemctl restart mariadb

Troubleshooting Common Installation Issues

Even with careful installation, you might encounter issues. Here are solutions to the most common problems.

Service Startup Problems

If MariaDB fails to start, check the error log:

sudo journalctl -u mariadb

Common solutions include:

  • Fixing file permissions: sudo chown -R mysql:mysql /var/lib/mysql
  • Checking disk space: Ensure adequate free space in /var/lib/mysql
  • Verifying configuration syntax: Test config files for errors

Connection and Authentication Issues

“Access denied” errors often result from:

  • Incorrect username or password
  • User doesn’t have proper host permissions
  • Authentication plugin mismatches

Reset the root password if necessary:

sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables &
mysql -u root

Permission and Access Problems

File permission issues typically require:

sudo chown -R mysql:mysql /var/lib/mysql
sudo chmod -R 755 /var/lib/mysql

MariaDB Security Best Practices

Maintaining security requires ongoing attention beyond initial installation.

Network Security Configuration

Configure MariaDB to listen only on necessary interfaces. Edit the configuration file and set:

bind-address = 127.0.0.1

This restricts connections to localhost only, preventing external access unless specifically configured.

User Access Management

Implement principle of least privilege:

  • Create application-specific users
  • Limit user access to required databases only
  • Use SSL for remote connections
  • Regularly audit user permissions

Regular Maintenance and Updates

Keep your system secure with regular maintenance:

sudo apt update && sudo apt upgrade

Monitor security advisories and apply patches promptly to address vulnerabilities.

Managing MariaDB Service Operations

Understanding service management commands helps maintain and troubleshoot your database installation.

Service Control Commands

Essential MariaDB service commands:

# Start MariaDB
sudo systemctl start mariadb

# Stop MariaDB
sudo systemctl stop mariadb

# Restart MariaDB
sudo systemctl restart mariadb

# Check service status
sudo systemctl status mariadb

# Enable auto-start at boot
sudo systemctl enable mariadb

# Disable auto-start
sudo systemctl disable mariadb

Monitoring Database Performance

Monitor your MariaDB installation using built-in tools:

SHOW PROCESSLIST;
SHOW STATUS LIKE 'Connections';
SHOW STATUS LIKE 'Uptime';

These commands help identify performance bottlenecks and monitor system health.

Frequently Asked Questions

1. What’s the difference between installing MariaDB from Ubuntu repositories versus official MariaDB repositories?

Ubuntu repositories provide stable, tested versions that integrate well with your system and receive automatic security updates through the standard package manager. Official MariaDB repositories offer the latest features and versions but may require more manual maintenance. For production systems, Ubuntu repositories are generally recommended unless you need specific newer features.

2. Can I run MariaDB and MySQL simultaneously on the same Ubuntu system?

While technically possible, running both databases simultaneously isn’t recommended due to potential port conflicts and resource competition. MariaDB is designed as a drop-in replacement for MySQL, so migration is usually the better approach rather than parallel installation.

3. How much disk space does MariaDB require for installation?

A basic MariaDB installation requires approximately 200-300MB of disk space. However, plan for additional space based on your data storage needs. Database files, logs, and temporary files can grow significantly with usage, so allocating several GB for database operations is prudent.

4. Why can’t I connect to MariaDB after installation even with correct credentials?

The most common cause is MariaDB’s default configuration using unix_socket authentication for the root user instead of password authentication. Try connecting with sudo mariadb instead of mysql -u root -p, or create a new user with password authentication as described in the user management section.

5. How do I completely uninstall MariaDB if needed?

To completely remove MariaDB, stop the service first, then remove packages and data:

sudo systemctl stop mariadb
sudo apt remove --purge mariadb-server mariadb-client mariadb-common
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql

Warning: This permanently deletes all databases and configuration files, so ensure you have backups before proceeding.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts