Home UbuntuHow to Install and Secure MariaDB on Ubuntu 24.04

How to Install and Secure MariaDB on Ubuntu 24.04

by Knowledge Base Linux
How to Install and Secure MariaDB — How to Install and Secure MariaDB on Ubuntu 24.04

This article covers How to Install and Secure MariaDB in detail. MariaDB on Ubuntu 24.04 is the database most Ubuntu servers end up running when an application asks for “MySQL”. MariaDB began as a community fork of MySQL and remains a drop-in replacement: same protocol, same SQL dialect, same client tools, but with a fully open-source license and a faster release cadence. Ubuntu 24.04 ships MariaDB 10.11 LTS in its repositories, which makes installing MariaDB on Ubuntu 24.04 a five-minute job. Doing it securely takes a few more steps, and this guide covers all of them: installation, the hardening script, proper user management and the configuration checks that keep the database off the open internet.

Why Choose MariaDB on Ubuntu 24.04? – How to Install and Secure MariaDB

Three reasons dominate. First, compatibility: WordPress, WooCommerce and most PHP applications cannot tell MariaDB apart from MySQL, so migrations are painless. Second, licensing and governance: MariaDB is developed in the open by the MariaDB Foundation, with no commercial dual-licensing surprises. Third, support: the 10.11 series in Ubuntu 24.04 receives updates until 2028. If you prefer the other open-source giant, we also have a guide to PostgreSQL on Ubuntu; the installation patterns are similar, the database philosophy is not.

Prerequisites – How to Install and Secure MariaDB

  • An Ubuntu 24.04 server with sudo privileges and at least 1 GB of RAM.
  • No existing MySQL installation: MariaDB and MySQL cannot listen on the same port, and Ubuntu will refuse conflicting packages.

Step 1: Install MariaDB

sudo apt update
sudo apt install mariadb-server mariadb-client -y
sudo systemctl enable --now mariadb
systemctl status mariadb

The status output should show active (running). Confirm the version with mariadb --version.

Step 2: Run the Security Hardening Script

MariaDB ships an interactive script that removes the dangerous defaults: anonymous users, remote root login and the test database:

sudo mariadb-secure-installation

On Ubuntu, root authenticates through the unix_socket plugin, so when the script asks for the current root password, just press Enter. Decline switching away from socket authentication if you want sudo mariadb to keep working password-free, set a root password only if your tooling requires it, and answer Y to removing anonymous users, disallowing remote root, dropping the test database and reloading privileges.

Inspecting data in MariaDB on Ubuntu 24.04

Step 3: Create an Admin and Application Users

Because root logs in through the socket, create a named administrator for any tool that connects over TCP, plus one limited user per application:

sudo mariadb -e "CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'AdminPass123!'; GRANT ALL PRIVILEGES ON *.* TO 'dbadmin'@'localhost' WITH GRANT OPTION;"
sudo mariadb -e "CREATE DATABASE appdb; CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'AppPass123!'; GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES;"

Test both logins:

mariadb -u appuser -p -e "SHOW DATABASES;"

The application user must see appdb and the system schemas, but nothing belonging to other applications. Day-to-day data work after that, such as moving databases between servers, is covered by our tutorial on importing and exporting MySQL/MariaDB databases.

Step 4: Confirm MariaDB Only Listens Locally

Unless another server must connect, the database should not be reachable from the network at all. Check the bind address in /etc/mysql/mariadb.conf.d/50-server.cnf:

grep -E 'bind-address|port' /etc/mysql/mariadb.conf.d/50-server.cnf
sudo ss -tlnp | grep 3306

The default bind-address = 127.0.0.1 is what you want, and the socket listing should confirm MariaDB is bound to localhost only. If you genuinely need remote access, bind to the private network interface instead, create users with explicit host restrictions such as 'appuser'@'10.0.0.5', and allow port 3306 in the firewall for that source address only.

Step 5: One Sensible Performance Setting

Defaults are conservative. On a dedicated database server, the single most valuable change is the InnoDB buffer pool, which caches data and indexes in RAM. Set it to roughly 50–70% of memory for a dedicated box in /etc/mysql/mariadb.conf.d/50-server.cnf:

[mysqld]
innodb_buffer_pool_size = 1G
sudo systemctl restart mariadb
sudo mariadb -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"

Troubleshooting

“Access denied” when an application uses root. That is socket authentication working as designed. Create a dedicated user as in Step 3 instead of weakening root.

The service fails to start after a config edit. Read the real error with journalctl -u mariadb -e and /var/log/mysql/error.log; a typo in 50-server.cnf is the usual cause. Validate by starting the daemon manually only in a maintenance window.

Port 3306 is already in use. Something else, often a leftover MySQL or a Docker container, owns the port. Find it with sudo ss -tlnp | grep 3306 and decide which database should really run.

Remote clients time out. Work through the chain: bind address, user host restriction, then firewall. Do not “fix” it by binding to 0.0.0.0 with a wildcard user; that is how databases end up in breach reports.

Conclusion

Installing MariaDB on Ubuntu 24.04 takes minutes; the value is in the hardening: run the secure-installation script, keep root on socket authentication, give every application its own limited user, and verify the server listens only where it should. With those basics in place, MariaDB on Ubuntu 24.04 will quietly serve your applications for years. The official MariaDB documentation covers replication, Galera clusters and advanced tuning when you outgrow a single node.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.