MariaDB is the default MySQL-compatible database on AlmaLinux and Rocky Linux, and it runs WordPress and most PHP apps without changes. This guide installs it with dnf, shows how to get a newer release from the official MariaDB repository, secures the install and covers the SELinux settings that trip people up.
Before you start
- A Linux VPS running AlmaLinux or Rocky Linux 8 or 9.
- A user with
sudorights, connected over SSH. The login details are in your VPS provider's control panel. - No MySQL server already installed. MySQL and MariaDB use the same port and data folder, so install only one.
Update the system first:
sudo dnf update -yOption 1: install from AppStream
The AppStream repository that ships with the OS includes MariaDB. Check which version it offers:
dnf info mariadb-serverOn version 8, MariaDB is split into module streams. List them to see your choices:
sudo dnf module list mariadbTo use a stream other than the default, enable it before installing, for example sudo dnf module enable mariadb:10.11, using a stream from your list. Then install:
sudo dnf install mariadb-serverAppStream versions receive security fixes for the life of the OS release, which makes this the low-maintenance choice.
Option 2: install from the official MariaDB repository
Use this when your app needs a newer release series than AppStream offers. MariaDB provides a setup script that adds its repository.
- Download the script and look at it before running it:
curl -LsSO https://r.mariadb.com/downloads/mariadb_repo_setup
less mariadb_repo_setupThe MariaDB download page publishes a checksum for the script. Compare it with sha256sum mariadb_repo_setup if you want to be sure the file is genuine.
- Run it with the release series you want. The series below is an example; pick a current long-term support release from mariadb.org:
chmod +x mariadb_repo_setup
sudo ./mariadb_repo_setup --mariadb-server-version="mariadb-11.4"- On version 8 only, stop the AppStream module from competing with the new repository:
sudo dnf module disable mariadb- Install the server. Packages from this repository use capital letters:
sudo dnf install MariaDB-server MariaDB-clientNote: Once you use the MariaDB repository, upgrades to a new series are your job. Read the release notes and take a full backup before moving to a new series.
Start and enable the service
The service is called mariadb for both options:
sudo systemctl enable --now mariadb
systemctl status mariadbYou should see active (running). enable --now starts it immediately and at every boot.
Secure the install
Run the hardening script that ships with MariaDB:
sudo mariadb-secure-installationOn older releases the script is called mysql_secure_installation. Both do the same job. Answer the prompts:
- Enter current password for root: press Enter, as there is none yet.
- Switch to unix_socket authentication: yes is a safe choice. Root can then only log in from a
sudoshell. - Change the root password: optional if you use unix_socket. If you set one, choose your own long, unique password.
- Remove anonymous users, disallow root login remotely, remove test database, reload privilege tables: yes to each.
With unix_socket, log in as root like this:
sudo mariadbWarning: Do not give your web app the root account. Create a dedicated database and user, as shown in create a database and user for a web app.
SELinux notes
AlmaLinux and Rocky Linux run SELinux in enforcing mode by default. With the default paths and port, MariaDB works without changes. Problems start when you move things.
Check the mode:
getenforceIf you move the data folder, for example to /data/mysql, label the new path so MariaDB may use it. Install the tools first:
sudo dnf install policycoreutils-python-utils
sudo semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
sudo restorecon -Rv /data/mysqlIf you change the port from 3306, allow the new port in SELinux too:
sudo semanage port -a -t mysqld_port_t -p tcp 3307To find SELinux denials after a failed start:
sudo ausearch -m avc -ts recentWarning: Do not set SELinux to permissive or disabled to "fix" MariaDB. Label the paths and ports correctly instead, so the rest of the server stays protected.
Check it works
Run a query as root:
sudo mariadb -e "SELECT VERSION();"Then confirm what it listens on:
sudo ss -ltnp | grep 3306MariaDB listens on all addresses by default on this platform. That is fine while firewalld blocks port 3306, which it does unless you opened it. If only local apps use the database, add bind-address=127.0.0.1 under [mysqld] in the server config file and restart the service.
Troubleshooting
No match for argument: MariaDB-server: the MariaDB repository was not added. Check/etc/yum.repos.d/mariadb.repoexists and rerun the setup script.- dnf installs the AppStream version instead: on version 8, disable the module with
sudo dnf module disable mariadb, then reinstall. - Service fails after moving the data folder: usually an SELinux label or ownership issue. Run
ausearchas above andsudo chown -R mysql:mysqlon the new folder. ERROR 1698 Access denied for user 'root': root uses unix_socket. Runsudo mariadbinstead of a password login.- Another server cannot connect: firewalld blocks 3306 by default. Allow only that server's IP with a rich rule; see firewalld on AlmaLinux and Rocky Linux.
Next steps
- Not sure MariaDB is the right pick? Read MySQL vs MariaDB.
- Add app users with the right rights: Create a MySQL user and grant privileges.
