This guide shows how to install MySQL Community Server or MariaDB on CentOS, RHEL and compatible distributions such as Rocky Linux and AlmaLinux, then secure and check the installation. The commands above only cover an old MySQL 5.6 setup on CentOS 7, so the sections below give current commands for RHEL 8 and 9, the missing MariaDB steps, and fixes for the errors people hit most often.
$ sudo yum update
$ yum install wget
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
$ yum update
$ sudo yum install mysql-server
$ sudo systemctl start mysqld
$ sudo mysql_secure_installation
$ mysql -u root -pCode language: JavaScript (javascript)

Choose your OS and database first
MySQL and MariaDB both provide the mysql client command and use port 3306, but they are separate products. Their server packages conflict with each other, so install only one of them on a host. Which commands you use depends on the OS release.
| Platform | Package manager | MySQL source | MariaDB source |
|---|---|---|---|
| CentOS 7 (end of life since June 2024) | yum | Oracle MySQL repo only. The base repos have no mysql-server | mariadb-server (5.5) in base |
| RHEL 8, Rocky 8, AlmaLinux 8 | dnf | AppStream mysql module or Oracle MySQL repo | AppStream mariadb module |
| RHEL 9, Rocky 9, AlmaLinux 9, CentOS Stream 9 | dnf | AppStream mysql-server (8.0) or Oracle MySQL repo | AppStream mariadb-server |
Note: CentOS 7 and CentOS Linux 8 no longer get security updates. Use RHEL, Rocky Linux, AlmaLinux or CentOS Stream for new servers. On these systems yum is an alias for dnf, so the older commands still run.
What is outdated in the commands above
The mysql-community-release-el7-5 package sets up the Oracle repository for MySQL 5.6 on EL7 only. MySQL 5.6 has been end of life for years. The sequence also leaves out steps you need with any current MySQL version:
yum install wgetand the secondyum updateare run withoutsudo, so they fail for a normal user.- The repo package is downloaded over plain
http. Usehttps. - The service is started but not enabled, so MySQL will not come back after a reboot.
- MySQL 5.7 and later create a random temporary root password in
/var/log/mysqld.log. You need it beforemysql_secure_installationwill run. - MariaDB is in the title but there are no MariaDB steps.
Install MySQL 8.x from the official MySQL repository (RHEL 8/9)
Use the Oracle repository if you need a specific MySQL release line, such as the 8.4 LTS series. The release RPM file name changes over time, so check the current name for your EL version on the MySQL Yum repository download page (dev.mysql.com/downloads/repo/yum/) and set it in the variable below.
sudo dnf install -y wget
# Example for EL9. Use the current file name from the MySQL Yum repo page.
RPM=mysql84-community-release-el9-1.noarch.rpm
wget https://repo.mysql.com/$RPM
sudo dnf install -y ./$RPM
# Confirm the MySQL repos are enabled
dnf repolist enabled | grep -i mysql
# EL8 ONLY: the AppStream mysql module hides the Oracle packages
sudo dnf module disable -y mysql
# Install the server, then start it and enable it at boot
sudo dnf install -y mysql-community-server
sudo systemctl enable --now mysqld
systemctl status mysqld --no-pagerCode language: PHP (php)
When mysqld starts for the first time, it initialises the data directory and writes a temporary root password to its log. Read it from there:
sudo grep 'temporary password' /var/log/mysqld.log
# [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: q;Xk8f#2rT1aCode language: PHP (php)
Run sudo mysql_secure_installation and enter the temporary password when asked. It makes you set a new root password and offers to remove anonymous users, disable remote root login and drop the test database. Answer yes to all of these on production servers.
Note: If you only need MySQL 8.0, the simplest option on RHEL 9 is sudo dnf install -y mysql-server from AppStream, then sudo systemctl enable --now mysqld. That build does not write a temporary password, so run sudo mysql_secure_installation straight away to set the root password.
Corrected commands for legacy CentOS 7
If you must keep a CentOS 7 host running, this is the same flow as the example above with the fixes applied. Get the current EL7 release RPM name from the MySQL download page. Oracle may have stopped publishing new versions for EL7, so check before you rely on it.
sudo yum install -y wget
wget https://repo.mysql.com/<current-el7-release>.noarch.rpm
sudo rpm -ivh <current-el7-release>.noarch.rpm
sudo yum install -y mysql-community-server
sudo systemctl enable --now mysqld
sudo grep 'temporary password' /var/log/mysqld.log
sudo mysql_secure_installation
mysql -u root -pCode language: JavaScript (javascript)
Install MariaDB
MariaDB comes from the distribution's own repositories, so you do not need a third-party repo. The service is called mariadb, not mysqld.
# RHEL / Rocky / AlmaLinux 8 and 9 (use yum on CentOS 7)
sudo dnf install -y mariadb-server
sudo systemctl enable --now mariadb
systemctl status mariadb --no-pager
# Secure it. Newer MariaDB versions also provide mariadb-secure-installation
sudo mysql_secure_installation
# Connect as root
sudo mysqlCode language: PHP (php)
There is no temporary password. MariaDB 10.4 and later authenticate the local root account through the Unix socket, so sudo mysql logs in without a password. On CentOS 7 (MariaDB 5.5), root starts with an empty password, so press Enter at the first mysql_secure_installation prompt. If you need a newer MariaDB release than your distribution ships, MariaDB provides its own RPM repositories.
Note: Do not install mariadb-server and mysql-community-server on the same host. They conflict over files, the port and the data directory in /var/lib/mysql. To switch from one to the other, remove the first package and move its data directory out of the way first.
Verify the installation and create an application user
mysql --version
sudo ss -tlnp | grep 3306
mysql -u root -p -e "SELECT VERSION();"Code language: JavaScript (javascript)
Applications should never connect as root. Create a dedicated database and a user with privileges on that database only:
CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'Ch@ngeMe-Str0ng-2024';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
SHOW GRANTS FOR 'appuser'@'localhost';Code language: JavaScript (javascript)
To allow connections from other hosts, create the user for that network, for example 'appuser'@'10.0.0.%', check that the server is not bound only to 127.0.0.1 (the bind-address setting in /etc/my.cnf or /etc/my.cnf.d/), and open the port in firewalld:
sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --reload
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
No package mysql-server available (CentOS 7) | The base repos only include MariaDB | Add the Oracle MySQL repo, or install mariadb-server |
No match for argument: mysql-community-server (EL8) | The AppStream mysql module hides the Oracle packages | sudo dnf module disable mysql, then install again |
| GPG key check fails during install | The release RPM is old and has an expired signing key | Download and install the latest release RPM, which includes the current key |
ERROR 1045 (28000): Access denied for user 'root'@'localhost' | Wrong password, or the temporary password was not used | Use the password from /var/log/mysqld.log. On MariaDB, use sudo mysql |
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements | The Oracle builds enable the validate_password component by default | Use a longer password with upper case, lower case, digits and symbols |
ERROR 2002 (HY000): Can't connect to local MySQL server through socket | The server is not running | sudo systemctl start mysqld (or mariadb), then check journalctl -u mysqld |
Unit mysqld.service not found | MariaDB is installed, or only the client package is | Use systemctl start mariadb, or install the server package |
Security and operations tips
- Keep SELinux in enforcing mode. If you move the data directory, label it with
semanage fcontextandrestorecon. Do not disable SELinux to work around it. - Open port 3306 only to the application subnets that need it. Never expose it to the internet.
- Keep packages patched with
sudo dnf upgradeand test major-version upgrades on a copy of the data first. - Set up backups with
mysqldumpormariadb-dump, and run regular test restores to check they work. - Use the same install method on every server. Mixing AppStream and Oracle packages makes upgrades harder to predict.
Frequently asked questions
What is the default MySQL root password on CentOS or RHEL?
Oracle repository builds of MySQL 5.7 and later generate a random temporary password, which you can read with sudo grep 'temporary password' /var/log/mysqld.log. MariaDB and the RHEL AppStream MySQL build do not create one. Set the root password with mysql_secure_installation.
Can MySQL and MariaDB be installed on the same server?
Not from the standard packages. They conflict over files, the default port 3306 and /var/lib/mysql. Install one, or run the second one in a container on a different port.
Why does yum install mysql give me MariaDB on CentOS 7?
CentOS 7 replaced MySQL with MariaDB in its base repositories, and the mysql name points to the MariaDB client. To get real MySQL, add Oracle's MySQL Yum repository first.
What is the service name for MySQL and MariaDB?
MySQL runs as mysqld (systemctl status mysqld). MariaDB runs as mariadb (systemctl status mariadb).
How do I check which MySQL or MariaDB version is installed?
Run mysql --version for the client, or SELECT VERSION(); inside the client for the server. rpm -qa | grep -Ei 'mysql|mariadb' lists the installed packages.
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals