How to install MariaDB on AlmaLinux and Rocky Linux
Install MariaDB on AlmaLinux or Rocky Linux 8 and 9 from AppStream or the official MariaDB repository, secure it, enable the service and handle SELinux.
- Beginner
- Linux VPS
- 5 min read
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 RunRacks Linux VPS running AlmaLinux or Rocky Linux 8 or 9.
- A user with
sudorights, connected over SSH. The login details are in your Dashboard. - 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-clientOnce 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 mariadbDo 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 recentDo 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. Run
sudo 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.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Databases
Reset a Forgotten MySQL Root Password on Linux
Recover access to MySQL or MariaDB on a Linux VPS by starting the server with skip-grant-tables, setting a new root password and restarting normally.
- Intermediate
- Linux VPS
- 5 min read
Databases
MySQL Point-in-Time Recovery Using Binary Logs
Recover a MySQL or MariaDB database to an exact moment after a bad write or dropped table by restoring a full backup and replaying binary logs up to the incident.
- Advanced
- Linux VPS
- 7 min read
Databases
How to create a database and user for a web app in MySQL or MariaDB
Set up a utf8mb4 database and a dedicated user with a strong password for WordPress, Laravel or any web app, then test the login before you configure the app.
- Beginner
- Linux VPS
- 4 min read