How to Install MariaDB on Ubuntu (20.04 / 22.04 / 24.04) – Complete Setup & Security Guide

目次

Fastest Way to Install MariaDB on Ubuntu (20.04 / 22.04 / 24.04 Supported)

If you want to start using MariaDB on Ubuntu immediately, simply follow the steps below to complete installation and startup verification. Unless you have a specific reason, installing from the Ubuntu default repository is the safest and most stable option.

① Update Package Information

sudo apt update
sudo apt upgrade -y

② Install MariaDB

sudo apt install -y mariadb-server mariadb-client

No special configuration is required during installation. After completion, the MariaDB service will start automatically.

③ Verify That MariaDB Is Running

sudo systemctl status mariadb

If active (running) is displayed, MariaDB is running properly.

④ Enable Auto-Start (Usually Not Required but Recommended to Confirm)

sudo systemctl enable mariadb

⑤ Connect to MariaDB and Verify Operation

sudo mysql

If you see the following prompt, the installation was successful:

MariaDB [(none)]>

To exit, type exit;.

Mandatory Security Initialization After Installation

Immediately after installation, MariaDB only has minimal security settings applied. Be sure to run the following command:

sudo mysql_secure_installation

Generally, the following answers are recommended:

  • Remove anonymous users → Y
  • Disallow root login remotely → Y
  • Remove test database → Y
  • Reload privilege tables → Y

This completes the setup of a secure MariaDB environment on Ubuntu.

MariaDB Version Installed by Ubuntu Version

  • Ubuntu 20.04 → MariaDB 10.3 series
  • Ubuntu 22.04 → MariaDB 10.6 series
  • Ubuntu 24.04 → MariaDB 10.11 series (LTS)

For most users, the Ubuntu default version is sufficient. Only consider the official repository if you need the latest features.

Quick Checks If Installation Fails or Service Does Not Start

  • Check errors with sudo systemctl status mariadb
  • Check detailed logs with sudo journalctl -xe
  • Check disk space with df -h

In most cases, the issue is either insufficient disk space or a configuration file typo.

Choosing the Right MariaDB Version for Your Ubuntu Version

When using MariaDB on Ubuntu, the safest option in most cases is the default repository version. Security updates and dependency compatibility are maintained automatically, making it ideal for beginners and intermediate users.

Ubuntu Default Repository Version (Recommended)

  • Ubuntu 20.04 → MariaDB 10.3 series
  • Ubuntu 22.04 → MariaDB 10.6 series
  • Ubuntu 24.04 → MariaDB 10.11 series (LTS)

Unless you have a specific requirement, these versions are fully practical for production use.

If You Need the Latest Version (Add the Official MariaDB Repository)

If you need the newest features, you can add the official MariaDB repository. Since apt-key is deprecated, this uses the signed-by method.

① Save the GPG Key

sudo mkdir -p /etc/apt/keyrings
sudo curl -fsSL https://mariadb.org/mariadb_release_signing_key.asc \
  | sudo gpg --dearmor -o /etc/apt/keyrings/mariadb.gpg

② Check Your Ubuntu Codename

lsb_release -cs

Examples:

  • 20.04 → focal
  • 22.04 → jammy
  • 24.04 → noble

③ Add the Official Repository (Example: 10.11 Series)

echo "deb [signed-by=/etc/apt/keyrings/mariadb.gpg] \
https://mirror.mariadb.org/repo/10.11/ubuntu \
$(lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/mariadb.list

④ Update Packages and Install

sudo apt update
sudo apt install mariadb-server mariadb-client

Notes When Using the Official Repository

  • You can use newer features than the Ubuntu default version
  • There is a future risk of dependency conflicts
  • Thorough testing is required for production servers

For production, clearly decide whether you prioritize stability or the latest features.

How to Check the Currently Installed MariaDB Version

mysql --version

Example:

mysql  Ver 15.1 Distrib 10.6.16-MariaDB

By explicitly confirming the version, troubleshooting and migration work become much easier.

Complete Guide to Initial MariaDB Security Setup (mysql_secure_installation)

Immediately after installing MariaDB, only minimal security settings are applied. To operate safely, be sure to run the following command:

sudo mysql_secure_installation

When executed, several prompts will appear. In most cases, the following answers are recommended.

Recommended Answers

  • Enter current password for root: → Press Enter (not set on first run)
  • Switch to unix_socket authentication? → Y (Recommended on Ubuntu)
  • Change the root password? → N (If using unix_socket authentication)
  • Remove anonymous users? → Y
  • Disallow root login remotely? → Y
  • Remove test database and access to it? → Y
  • Reload privilege tables now? → Y

This completes the basic security configuration.

Why You Cannot Log In as root (Access Denied Error)

On Ubuntu, the root user uses unix_socket authentication by default. Therefore, standard password-based login will not work.

Example Symptom

mysql -u root -p

Access denied for user 'root'@'localhost'

Correct Login Method (Ubuntu Default)

sudo mysql

This is the safest and recommended method.

How to Change root to Password Authentication (Only If Necessary)

Only change the authentication method if you must connect as root from external tools or applications.

① Connect to MariaDB

sudo mysql

② Change the Authentication Plugin

ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'StrongPasswordHere';

FLUSH PRIVILEGES;

③ Confirm the Change

SELECT user, host, plugin FROM mysql.user;

If mysql_native_password is displayed, the change was successful.

How to Check the Current Authentication Method

sudo mysql -e "SELECT user, host, plugin FROM mysql.user;"

Main authentication methods:

  • unix_socket → Ubuntu default, secure
  • mysql_native_password → Password-based authentication

Unless there is a specific reason, it is safest to keep root using unix_socket authentication.

Common Errors and Solutions

ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

  • Cause: Attempting password-based login
  • Solution: Log in using sudo mysql

mysql_secure_installation Not Found

  • Check with which mysql_secure_installation
  • If not installed, run sudo apt install mariadb-server

Set Character Encoding to utf8mb4 (Prevent Garbled Text)

To safely handle multilingual text and emoji in MariaDB, it is recommended to use utf8mb4. Without this setting, you may encounter encoding issues or data loss.

① Edit the Configuration File

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

Add or modify the following lines in the [mysqld] section:

[mysqld]
character-set-server = utf8mb4
collation-server     = utf8mb4_general_ci

② Restart MariaDB

sudo systemctl restart mariadb

③ Verify the Settings

sudo mysql -e "SHOW VARIABLES LIKE 'character_set%';"

If utf8mb4 is displayed, the configuration is successful.

Specify utf8mb4 When Creating a Database (Recommended)

If you want to explicitly define the character set regardless of global settings, specify it during database creation.

CREATE DATABASE sample_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;

All tables created within this database will inherit utf8mb4.

Create an Application User and Grant Permissions

In production environments, never use the root user for application connections. Create a dedicated user and grant only the minimum required privileges.

① Log in to MariaDB

sudo mysql

② Create a User (Local Connection)

CREATE USER 'app_user'@'localhost'
IDENTIFIED BY 'StrongPasswordHere';

③ Grant Database Privileges

GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'localhost';

FLUSH PRIVILEGES;

Create a Remote Access User (Only If Needed)

Configure this only if you need to connect from an external server or cloud environment.

CREATE USER 'app_user'@'%'
IDENTIFIED BY 'StrongPasswordHere';

GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'%';

FLUSH PRIVILEGES;

Note: The “%” wildcard allows connections from any host. In production environments, specifying a particular IP address is safer.

How to Check Current Users and Privileges

SELECT user, host FROM mysql.user;

SHOW GRANTS FOR 'app_user'@'localhost';

Common Permission Errors and Solutions

Access denied for user ‘app_user’

  • Incorrect host specification (difference between localhost and %)
  • FLUSH PRIVILEGES was not executed
  • Incorrect database name during connection

Unknown database

  • Database name typo
  • No privileges granted for the target database

How to Allow Remote Connections in MariaDB (Fix When External Access Fails)

By default, MariaDB allows local connections only. If you need to connect from an external server or cloud environment, configure the following settings.

① Change bind-address

Edit the configuration file:

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

Find the following line:

bind-address = 127.0.0.1

To allow external connections, change it to:

bind-address = 0.0.0.0

After saving, restart MariaDB:

sudo systemctl restart mariadb

② Open Port 3306 in the Firewall

If you are using UFW on Ubuntu:

sudo ufw allow 3306
sudo ufw reload

For improved security, specify the source IP address:

sudo ufw allow from 192.168.1.10 to any port 3306

③ Create a Remote Access User

CREATE USER 'app_user'@'%'
IDENTIFIED BY 'StrongPasswordHere';

GRANT ALL PRIVILEGES
ON sample_db.*
TO 'app_user'@'%';

FLUSH PRIVILEGES;

For better security:

CREATE USER 'app_user'@'192.168.1.10'
IDENTIFIED BY 'StrongPasswordHere';

Checklist If You Cannot Connect Externally

1. Is MariaDB Running?

sudo systemctl status mariadb

2. Is Port 3306 Listening?

sudo ss -tulnp | grep 3306

If it shows 0.0.0.0:3306, it is correctly listening on all interfaces.

3. If Using a Cloud Environment (AWS / GCP / VPS)

  • Ensure port 3306 is open in the security group
  • Confirm that no external firewall is blocking the connection

4. Causes by Error Message

Can’t connect to MySQL server

  • Port is closed
  • bind-address not changed
  • Firewall blocking the connection

Access denied for user

  • Incorrect user host specification
  • Incorrect password
  • Privileges not granted

Connection timed out

  • Cloud-side security settings
  • Network routing issues

Important Security Notes

  • Avoid using “%” in production environments
  • Use VPN access whenever possible
  • Consider enabling SSL/TLS connections
  • Never allow remote root login

Troubleshooting When MariaDB Fails to Start (failed Error)

① Check Service Status

sudo systemctl status mariadb

② Check Detailed Logs

sudo journalctl -xe
sudo cat /var/log/mysql/error.log

③ Common Causes

  • Configuration file syntax errors
  • Insufficient disk space
  • Port conflict (MySQL already running)

④ Check Disk Space

df -h

Summary of Access Denied Error Causes

  • Incorrect password
  • Incorrect host specification (difference between localhost and %)
  • FLUSH PRIVILEGES not executed
  • Attempting standard login for root (requires sudo mysql)

Basic Performance Improvements (For Beginners)

Advanced tuning is not required, but at minimum, confirm the following settings.

① Enable Slow Query Log

Add the following to 50-server.cnf:

slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2

Restart MariaDB:

sudo systemctl restart mariadb

② Check Index Usage

Slow queries may be missing proper indexes.

EXPLAIN SELECT * FROM sample_table WHERE column_name = 'value';

③ Check Server Resources

top
htop

If memory is insufficient, consider upgrading your server plan.

Frequently Asked Questions (FAQ)

How can I lock the MariaDB version on Ubuntu 22.04?

Use apt-mark hold mariadb-server.

How do I reset the root password if I forgot it?

You can reset it by starting in safe mode and reconfiguring the password. See our dedicated guide for detailed steps.

How do I change the MariaDB port number?

Modify 50-server.cnf to something like port = 3307, then restart the service.

Conclusion: Using MariaDB Safely on Ubuntu

  • Using the default repository version is the safest option
  • Always run mysql_secure_installation
  • Never use root for application connections
  • Enable remote access only when necessary
  • When troubleshooting, checking logs is the top priority

By following these steps, you can build a secure and production-ready MariaDB environment on Ubuntu.