โ˜• XOPSSCHOOL โ€” LEARNING MADE AFFORDABLE
Build the Skills Your Next Career Move Demands.
For the Price of One Coffee a Month.
Why spend hundreds on individual courses? Unlock an entire year of technical learning with one Coffee Pass.
US$4.90 /month
US$58.80/year
โ‚น499 + GST/month
โ‚น5,988 + GST/year
โ‚ฌ4.90 /month
โ‚ฌ58.80/year
12 Months of Access • Billed Annually • No Automatic Renewal
DevOps • Cloud • SRE • Kubernetes • Terraform • DevSecOps • AI • AIOps • MLOps • LLMOps • FinOps • GitOps • Platform Engineering & Much More
192
Premium Courses
1,085
Video Sessions
99
Ops Disciplines
86
Tool Courses
340
Assignments
227
Quizzes
โœ“ QR-Verifiable Certificates   โœ“ Learn at Your Own Pace   โœ“ Hands-on Learning   โœ“ New Courses Included
Learn from real-world, instructor-led video sessions by
Rajesh Kumar
Principal Architect • Trainer • Mentor • 15+ Years of Industry Experience
โ˜• Get My Coffee Pass โ€” Start Learning Today →
One Payment. Every Premium Course. One Full Year.
The best investment you can make is in the person you are becoming.

How to install MySql and MaridaDb in Centos/RHEL?

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)

Terminal running mysql_secure_installation on CentOS, setting root password and removing anonymous users

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.

PlatformPackage managerMySQL sourceMariaDB source
CentOS 7 (end of life since June 2024)yumOracle MySQL repo only. The base repos have no mysql-servermariadb-server (5.5) in base
RHEL 8, Rocky 8, AlmaLinux 8dnfAppStream mysql module or Oracle MySQL repoAppStream mariadb module
RHEL 9, Rocky 9, AlmaLinux 9, CentOS Stream 9dnfAppStream mysql-server (8.0) or Oracle MySQL repoAppStream 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 wget and the second yum update are run without sudo, so they fail for a normal user.
  • The repo package is downloaded over plain http. Use https.
  • 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 before mysql_secure_installation will 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

ErrorCauseFix
No package mysql-server available (CentOS 7)The base repos only include MariaDBAdd the Oracle MySQL repo, or install mariadb-server
No match for argument: mysql-community-server (EL8)The AppStream mysql module hides the Oracle packagessudo dnf module disable mysql, then install again
GPG key check fails during installThe release RPM is old and has an expired signing keyDownload 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 usedUse the password from /var/log/mysqld.log. On MariaDB, use sudo mysql
ERROR 1819 (HY000): Your password does not satisfy the current policy requirementsThe Oracle builds enable the validate_password component by defaultUse a longer password with upper case, lower case, digits and symbols
ERROR 2002 (HY000): Can't connect to local MySQL server through socketThe server is not runningsudo systemctl start mysqld (or mariadb), then check journalctl -u mysqld
Unit mysqld.service not foundMariaDB is installed, or only the client package isUse 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 fcontext and restorecon. 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 upgrade and test major-version upgrades on a copy of the data first.
  • Set up backups with mysqldump or mariadb-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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

Linux Tutorials: How to enable EPEL repository in redhat/Centos?

To enable EPEL (Extra Packages for Enterprise Linux), you first turn on the repository it depends on, which is CodeReady Builder on RHEL, PowerTools on EL 8…

Read More

How to install Ansible in Centos 7/RHEL 7?

On CentOS 7 or RHEL 7 you can install Ansible in two ways: with yum from the EPEL repository, which gives you Ansible 2.9, or with pip…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

What is yum and use cases of yum?

What is yum? yum, short for Yellowdog Updater Modified, is a command-line package management utility for RPM (Red Hat Package Manager)-based Linux distributions, such as CentOS and…

Read More

How to enable http Proxy Settings for Yum Command on RHEL / CentOS Servers?

No username and password required: To configure proxy without username and password: export http_proxy=http://SERVER:PORT/ Username and password authentication: To configure the proxy server with username and password…

Read More

Preparing steps for LAMP Installation

This page shows how to install a LAMP stack (Linux, Apache, MariaDB and PHP) on CentOS 7 with yum, systemctl and firewalld. The sections after the original…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x