Install MariaDB on CentOS: Step-by-Step Guide
In this article
This guide walks you through installing MariaDB on CentOS, a popular open-source relational database management system that is a drop-in replacement for MySQL. By the end, you'll have a running MariaDB server, secured with a root password, and ready to create databases and users. We'll cover installation from the official CentOS repositories, basic configuration, and common troubleshooting.
What You'll Need
- A CentOS server (version 7, 8, or 9) with root or sudo access.
- An internet connection to download packages.
- Basic familiarity with the Linux command line.
Step-by-Step
- Update your system packages. It's good practice to start with an up-to-date system. Run:
sudo yum update -y(on CentOS 7) orsudo dnf update -y(on CentOS 8/9). - Install MariaDB. On CentOS 7, use
sudo yum install mariadb-server -y. On CentOS 8/9, usesudo dnf install mariadb-server -y. This installs the server and client packages. - Start the MariaDB service. After installation, start the service with
sudo systemctl start mariadb. To ensure it starts automatically on boot, runsudo systemctl enable mariadb. - Check the service status. Verify it's running with
sudo systemctl status mariadb. You should see "active (running)" in the output. - Run the security script. MariaDB includes a script to secure your installation. Run
sudo mysql_secure_installation. This will prompt you to:- Set a root password (choose a strong one).
- Remove anonymous users (answer Y).
- Disallow root login remotely (answer Y).
- Remove the test database and access to it (answer Y).
- Reload privilege tables (answer Y).
- Test the installation. Log in as root to verify:
mysql -u root -p. Enter the password you set. You should see the MariaDB monitor prompt. TypeSHOW DATABASES;to list default databases, then exit withEXIT;. - Create a dedicated database user (optional but recommended). Instead of using root for everything, create a user for your applications. In the MySQL monitor, run:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
- Configure firewall (if enabled). If you need to access MariaDB from other machines, open the default port 3306. On CentOS 7:
sudo firewall-cmd --permanent --add-service=mysql && sudo firewall-cmd --reload. On CentOS 8/9:sudo firewall-cmd --permanent --add-service=mysql && sudo firewall-cmd --reload(the service name may be 'mysql' or 'mariadb' depending on your firewall configuration).
Common Mistakes
- Not enabling the service. If you restart the server, MariaDB won't start unless you ran
systemctl enable. - Skipping the security script. Leaving the default root password blank is a huge security risk.
- Forgetting to open the firewall. You can install and run MariaDB locally, but remote clients will fail to connect if port 3306 is blocked.
- Using the wrong package name. On CentOS 8+, the package is 'mariadb-server', not 'mysql-server'.
Tips
- Use a strong password for the root user and store it in a secure password manager.
- Create a separate database user for each application, with only the privileges it needs (e.g., only SELECT, INSERT on a specific database).
- Back up your databases regularly using
mysqldump. - Monitor MariaDB logs at
/var/log/mariadb/mariadb.logfor errors.
Troubleshooting
- Can't connect to the MySQL monitor: If you get "Access denied" after running
mysql -u root -p, you may have forgotten the root password. Stop the service (sudo systemctl stop mariadb), start it in safe mode (sudo mysqld_safe --skip-grant-tables &), log in without a password, and reset the root password using SQL commands. Then restart normally. - Service fails to start: Check the error log at
/var/log/mariadb/mariadb.log. Common issues include incorrect file permissions or a full disk. Ensure the data directory/var/lib/mysqlis owned by the 'mysql' user. - Remote connections refused: Verify the firewall is open, MariaDB is listening on all interfaces (check
bind-addressin/etc/my.cnfor/etc/my.cnf.d/; set it to 0.0.0.0 if needed), and that the user is allowed to connect from the remote host (e.g., 'myuser'@'%' instead of 'localhost'). - Package not found: If
dnf install mariadb-serverfails, runsudo dnf makecacheto refresh the repository cache, or enable the EPEL repository if the package is missing.
Frequently Asked Questions
What is the default root password?
There isn't one. During installation, the root user has no password by default. The mysql_secure_installation script forces you to set one.
Can I use MariaDB as a drop-in replacement for MySQL?
Yes, MariaDB is designed to be fully compatible with MySQL. Most applications that work with MySQL will work with MariaDB without changes.
How do I start, stop, or restart MariaDB?
Use sudo systemctl start mariadb, sudo systemctl stop mariadb, and sudo systemctl restart mariadb.
Where is the configuration file?
The main configuration file is /etc/my.cnf, and additional configuration files are in /etc/my.cnf.d/.
How do I change the root password later?
Log in as root (mysql -u root -p) and run: ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword'; then FLUSH PRIVILEGES;
How do I uninstall MariaDB completely?
Stop the service, then remove the packages: sudo systemctl stop mariadb and sudo yum remove mariadb-server mariadb (or dnf on CentOS 8/9). Optionally delete the data directory /var/lib/mysql if you want to remove all databases.