Almost every dynamic website stores its data in a relational database, and on Linux that usually means MariaDB or MySQL. MariaDB is a community-developed fork of MySQL and the default in Debian, Ubuntu and RHEL-family repositories; for most applications (WordPress, Laravel, Magento, Nextcloud) the two are interchangeable. A database exposed to the internet with a weak password is one of the most common causes of data breaches, and an untuned database is one of the most common causes of slow sites. This guide covers both.
Install
# Ubuntu / Debian
sudo apt update
sudo apt install mariadb-server -y
# AlmaLinux / Rocky
sudo dnf install mariadb-server -y
sudo systemctl enable --now mariadb
mariadb --version
If you need a newer release than your distribution ships, MariaDB provides official repositories for each version. Stay on a long-term support release (for example 10.11 or 11.4) for servers.
Run the security script
sudo mariadb-secure-installation
- Remove anonymous users: yes
- Disallow root login remotely: yes
- Remove the test database: yes
- Reload privilege tables: yes
On Debian and Ubuntu, the root account uses unix_socket authentication: you log in as root with sudo mariadb, no password needed, and nobody can log in as the database root user from the network. Keep it that way.
Create users with least privilege
Give every application its own database and its own user, with access to nothing else:
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, CREATE TEMPORARY TABLES, LOCK TABLES
ON shop.* TO 'shop'@'localhost';
FLUSH PRIVILEGES;
Most applications are happy with ALL PRIVILEGES ON shop.*, which is still limited to one database. Never grant ALL ON *.* to an application user.
A read-only user for reporting or backups:
CREATE USER 'report'@'localhost' IDENTIFIED BY 'another-password';
GRANT SELECT, SHOW VIEW ON shop.* TO 'report'@'localhost';
Useful checks:
SELECT user, host FROM mysql.user;
SHOW GRANTS FOR 'shop'@'localhost';
Always use utf8mb4. The older utf8 in MySQL/MariaDB supports only three-byte characters and fails on emoji and some rare Chinese characters.
Remote access: do it safely
By default MariaDB listens only on 127.0.0.1, which is what you want when the website and database are on the same server. If the database is on a separate server:
- Prefer a private network. Connect the servers with WireGuard and bind MariaDB to the private address.
- Set the bind address in
/etc/mysql/mariadb.conf.d/50-server.cnf(Debian/Ubuntu) or/etc/my.cnf.d/mariadb-server.cnf(RHEL family):
[mysqld]
bind-address = 10.10.0.2
- Create the user for the specific client address, never
'%':
CREATE USER 'shop'@'10.10.0.1' IDENTIFIED BY 'long-random-password';
GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'10.10.0.1';
To administer a database remotely from your laptop, use an SSH tunnel instead of opening the port:
ssh -L 3307:127.0.0.1:3306 deploy@203.0.113.10
# then connect your client to 127.0.0.1:3307
Avoid installing phpMyAdmin on a public URL; if you must, protect it with an IP allow-list and HTTP authentication.
Tune the important settings
MariaDB’s defaults are conservative. A few settings make most of the difference. Create /etc/mysql/mariadb.conf.d/99-tuning.cnf (or /etc/my.cnf.d/99-tuning.cnf):
[mysqld]
# The most important setting: memory for caching InnoDB data and indexes
innodb_buffer_pool_size = 1G
# Redo log size: larger = fewer flushes on write-heavy workloads
innodb_log_file_size = 256M
# Durability vs speed (see below)
innodb_flush_log_at_trx_commit = 1
max_connections = 150
tmp_table_size = 64M
max_heap_table_size = 64M
# Slow query log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
Restart: sudo systemctl restart mariadb.
Sizing the buffer pool
| Server | Database-only server | Web + database on one server |
|---|---|---|
| 2 GB RAM | 1 GB | 256–512 MB |
| 8 GB RAM | 5–6 GB | 2–3 GB |
| 32 GB RAM | 22–24 GB | 8–16 GB |
Ideally the buffer pool is larger than your active data. Check total InnoDB data size:
SELECT ROUND(SUM(data_length + index_length)/1024/1024) AS mb
FROM information_schema.tables WHERE engine = 'InnoDB';
Leave enough memory for PHP, Nginx and the operating system, or the server will start swapping.
Durability trade-off
innodb_flush_log_at_trx_commit = 1 writes every transaction safely to disk — the right choice for shops and anything with money or orders. Setting 2 is faster for write-heavy, less critical data (analytics, logs) at the risk of losing up to about a second of transactions if the server crashes.
Check connections
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
SHOW VARIABLES LIKE 'max_connections';
If Max_used_connections reaches max_connections, increase it — but first check whether the application is leaking connections or PHP has too many workers.
Find slow queries
With the slow query log enabled:
sudo mariadb-dumpslow -s t /var/log/mysql/slow.log | head -30
For a query that appears often, run EXPLAIN to see whether it uses an index:
EXPLAIN SELECT * FROM orders WHERE customer_email = 'a@example.com';
type: ALL with many rows means a full table scan — usually fixed by adding an index:
ALTER TABLE orders ADD INDEX idx_customer_email (customer_email);
The tool MySQLTuner gives a quick health report with suggestions (sudo apt install mysqltuner, then sudo mysqltuner). Treat its advice as hints, and let the server run for at least a day before trusting it.
Monitor
sudo mariadb -e "SHOW PROCESSLIST;"
sudo mariadb -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';"
sudo mariadb-admin status
SHOW PROCESSLIST shows queries running right now — the first thing to check when a site suddenly becomes slow.
Backups
A database is only as safe as its last tested backup. Use mariadb-dump --single-transaction for consistent dumps of InnoDB tables, or mariabackup for large databases. Full instructions with rotation and off-site copies: backing up MySQL and MariaDB.
Upgrades
- Minor updates (10.11.x) come through normal system updates.
- Major upgrades (10.11 → 11.4) change the data format: back up first, follow the release notes, run
mariadb-upgradeafterwards, and test the application.
Checklist
- Security script run; root uses unix_socket
- One user per application, privileges limited to its database
- utf8mb4 everywhere
- Listening on localhost or a private network only
- Buffer pool sized for RAM and data
- Slow query log enabled and reviewed
- Automated, tested backups off the server