Most websites and many apps keep their data in a MySQL-compatible database. AlmaLinux and Rocky Linux ship MariaDB. This page installs it, keeps it closed to the internet, creates a database with its own user for your website or app, and shows how to reach it safely from your computer. The commands were tested on a dedicated server with AlmaLinux 9 (MariaDB 10.5) and in test machines with AlmaLinux 10 (MariaDB 10.11) and Rocky Linux 9; run them as root.
1. Install and Start MariaDB
dnf install mariadb-server systemctl enable --now mariadb systemctl status mariadb
Unlike on Ubuntu, MariaDB does not start by itself after the installation: the second line starts it now and after every restart. The status then shows active (running).
2. Keep It Closed to the Internet
After the installation, MariaDB listens on all addresses of the server (ss -ltn | grep 3306 shows 0.0.0.0:3306). firewalld blocks port 3306 as long as you do not open it, but if the firewall is off, the database can be reached from the internet. Let MariaDB listen only on the server itself: create the file /etc/my.cnf.d/60-local.cnf with nano:
[mysqld] bind-address = 127.0.0.1
systemctl restart mariadb ss -ltn | grep 3306
Now the line shows 127.0.0.1:3306: programs on the server itself can use the database, the internet cannot.
Many older guides tell you to run mariadb-secure-installation next. It is not needed: there are no anonymous users and no test database, and the database administrator root can only log in as the system user root of the server, without a password.
3. Log In as Administrator
As root on the server, simply type:
mariadb
The prompt changes to MariaDB [(none)]>. Every command ends with a semicolon. SHOW DATABASES; lists the databases, exit leaves.
4. A Database and a User for Your Website or App
Give every website or app its own database and its own user, which can only use this database. In mariadb, replace shop with your name and choose a strong password:
CREATE DATABASE shop; CREATE USER 'shop'@'localhost' IDENTIFIED BY 'a-strong-password'; GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost';
Your app then uses these settings: host localhost (or 127.0.0.1), port 3306, database shop, user shop and the password. To try them, log in as the new user; -p asks for the password:
mariadb -u shop -p shop
The user shop can create tables in its database, but CREATE DATABASE other ends with Access denied for user 'shop'@'localhost': the user cannot touch other databases.
- A new password for the user:
ALTER USER 'shop'@'localhost' IDENTIFIED BY 'new-password'; - Remove the user and the database with all its data:
DROP USER 'shop'@'localhost';andDROP DATABASE shop;
5. Open the Database From Your Computer
Keep port 3306 closed and use an SSH tunnel instead. On your computer:
ssh -L 3306:127.0.0.1:3306 root@your-server
As long as this SSH session is open, a database program on your computer, for example HeidiSQL, DBeaver or MySQL Workbench, connects to host 127.0.0.1, port 3306, with the user and password from step 4. If MariaDB also runs on your computer, use another local port, for example -L 13306:127.0.0.1:3306 and port 13306.
6. More Memory for a Busy Database
MariaDB keeps the most used data in memory, in the InnoDB buffer pool. By default it is only 128 MB, also on a server with much more RAM. For a busy database, give it more in a file of your own, for example /etc/my.cnf.d/60-tuning.cnf:
[mysqld] innodb_buffer_pool_size = 2G
Then systemctl restart mariadb. Check the value with mariadb -e "SELECT @@innodb_buffer_pool_size/1024/1024"; it shows the size in MB (with 1G it shows 1024). Leave enough memory for your other programs: on a server that also runs a website, about a quarter to a half of the RAM is a common start.
Good to Know
- The settings are in
/etc/my.cnf.d/and the data is in/var/lib/mysql. Afterdnf remove mariadb-server, the data folder with all databases stays on the disk; delete it yourself only if you no longer need any of them. - If MariaDB does not start, its messages are in
journalctl -u mariadb. - Back up your databases every night: Back Up Your MariaDB or MySQL Databases Every Night. Its steps work here too; install the database tools with
dnfinstead ofapt. - The same steps on Ubuntu: MariaDB on Your Linux Server.