Report this

What is the reason for this report?

How To Install MySQL on CentOS

Updated on February 16, 2026

Not using CentOS 7?
Choose a different version or distribution.
CentOS 7
How To Install MySQL on CentOS

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LEMP (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. It uses a relational database and SQL (Structured Query Language) to manage its data.

CentOS prefers MariaDB by default, a fork of MySQL managed by the original MySQL developers and designed as a replacement for MySQL. If you run yum install mysql (or dnf install mysql on newer versions), it is MariaDB that is installed rather than MySQL. If you’re wondering about MySQL vs. MariaDB, MariaDB will generally work seamlessly in place of MySQL, so unless you have a specific use-case for MySQL, see the How To Install MariaDB on Centos 7 guide.

This tutorial will explain how to install MySQL version 8 on a CentOS or compatible RHEL-based server.

Compatibility Note: This tutorial was originally written for CentOS 7 and is also compatible with CentOS Stream 9 and other RHEL-based distributions. CentOS 7 (end of life June 30, 2024) and CentOS 8 (end of life December 31, 2021) have reached end of life. For production deployments, use a supported RHEL-based distribution such as CentOS Stream.

Key Takeaways

  • MariaDB is the default database on CentOS. Installing MySQL via yum or dnf will install MariaDB by default, not Oracle MySQL.
  • To install Oracle MySQL, use the official MySQL repository. Download and enable the MySQL Community Yum Repository to access official MySQL packages.
  • Package managers differ by CentOS version. Use yum on CentOS 7 and dnf on CentOS 8/Stream 9 and newer RHEL-based distributions.
  • Secure your installation. Run mysql_secure_installation after installation to set a root password and secure your MySQL server.

Prerequisites

To follow this tutorial, you will need:

  • A CentOS or RHEL-based server with a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in the Initial Server Setup with CentOS 7 guide for CentOS and other RHEL-based systems.

Step 1 — Installing MySQL

As mentioned in the introduction, running the Yum command to install MySQL will actually install MariaDB, not Oracle MySQL.

To install MySQL, you’ll need to visit the MySQL Community Yum Repository which provides the official MySQL packages.

CentOS 7 uses yum; CentOS Stream 9 and later use dnf. On newer CentOS versions, replace yum with dnf in the commands below. The repository package must match your system’s RHEL version (el7, el8, or el9).

In a web browser, visit:

https://dev.mysql.com/downloads/repo/yum/

Note that the prominent Download links don’t lead directly to the files. Instead, they lead to a subsequent page where you’re invited to log in or sign up for an account. If you don’t want to create an account, you can locate the text “No thanks, just start my download”, then right-click and copy the link location, or you can edit the version number in the commands below.

Locate the desired version, and update it as needed in the link below. The repository designation (el7, el8, or el9) corresponds to your OS: el7 for CentOS 7, el8 and el9 for newer releases. Select the matching repository for your system. Verify your release before selecting the repository package:

cat /etc/os-release

The example below uses an el7 package; on el8 or el9, download the matching repository package for your system.

Screencapture highlighting current yum repo name

curl -sSLO https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm

Once the rpm file is saved, we will verify the integrity of the download by running md5sum and comparing it with the corresponding MD5 value listed on the site:

md5sum mysql80-community-release-el7-5.noarch.rpm
Output
e2bd920ba15cd3d651c1547661c60c7c mysql80-community-release-el7-5.noarch.rpm

Compare this output with the appropriate MD5 value on the site:

Screencapture highlighting md5dsum

Now that we’ve verified that the file wasn’t corrupted or changed, we’ll install the package:

sudo rpm -ivh mysql80-community-release-el7-5.noarch.rpm

This adds two new MySQL yum repositories, and we can now use them to install MySQL server:

sudo yum install mysql-server

Note: If you’re using CentOS Stream 9 or a similarly recent release, the package manager has changed from yum to dnf. In that case, you should install MySQL with the following command instead:

sudo dnf install mysql-server

Press y to confirm that you want to proceed. Since we’ve just added the package, we’ll also be prompted to accept its GPG key. Press y to download it and complete the install.

Step 2 — Starting MySQL

We’ll start the daemon with the following command:

sudo systemctl start mysqld

systemctl doesn’t display the outcome of all service management commands, so to be sure we succeeded, we’ll use the following command:

sudo systemctl status mysqld

If MySQL has successfully started, the output should contain Active: active (running) and the final line should look something like:

Dec 01 19:02:20 centos-512mb-sfo2-02 systemd[1]: Started MySQL Server.

Note: MySQL is automatically enabled to start at boot when it is installed. You can change that default behavior with sudo systemctl disable mysqld

During the installation process, a temporary password is generated for the MySQL root user. Locate it in the mysqld.log with this command:

sudo grep 'temporary password' /var/log/mysqld.log
Output
2022-01-24T19:54:46.313728Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: mqRfBU_3Xk>r

Make note of the password, which you will need in the next step to secure the installation and where you will be forced to change it. The default password policy requires 12 characters, with at least one uppercase letter, one lowercase letter, one number and one special character.

Step 3 — Configuring MySQL

MySQL includes a security script to change some of the less secure default options for things like remote root logins and sample users.

Use this command to run the security script.

sudo mysql_secure_installation

This will prompt you for the default root password. As soon as you enter it, you will be required to change it.

Output
The existing password for the user account root has expired. Please set a new password. New password:

Enter a new 12-character password that contains at least one uppercase letter, one lowercase letter, one number and one special character. Re-enter it when prompted.

You’ll receive feedback on the strength of your new password, and then you’ll be immediately prompted to change it again. Since you just did, you can confidently say No:

Output
Estimated strength of the password: 100 Change the password for root ? (Press y|Y for Yes, any other key for No) :

After we decline the prompt to change the password again, we’ll press Y and then ENTER to all the subsequent questions in order to remove anonymous users, disallow remote root login, remove the test database and access to it, and reload the privilege tables.

Now that we’ve secured the installation, let’s test it.

Step 4 — Testing MySQL

We can verify our installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MySQL as root (-u root), prompt for a password (-p), and return the version.

mysqladmin -u root -p version

You should see output similar to this:

Output
mysqladmin  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version  8.0.28
Protocol version 10
Connection  Localhost via UNIX socket
UNIX socket   /var/lib/mysql/mysql.sock
Uptime:   3 min 2 sec

Threads: 2  Questions: 14  Slow queries: 0  Opens: 133  Flush tables: 3  Open tables: 49  Queries per second avg: 0.076

This indicates your installation has been successful.

FAQs

Why does yum install MariaDB?
CentOS and other RHEL-based distributions ship the mysql package as a compatibility alias for MariaDB. Installing Oracle MySQL requires adding the official MySQL repository.

Can I use this tutorial on CentOS Stream?
Yes. Use dnf instead of yum and choose the repository package that matches your release (el8 or el9).

How do I check my CentOS version?
Run cat /etc/os-release to see your version and pick the correct el7, el8, or el9 repository.

Can MySQL and MariaDB run on the same server?
Running both on the same host is not recommended. They use the same client tools and port (3306) and can conflict; use one or the other per server.

Conclusion

In this tutorial, we’ve installed and secured MySQL on a CentOS server. To learn more about using MySQL, this guide to learning more about MySQL commands can help. You might also consider implementing some additional security measures.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Vinayak Baranwal
Vinayak Baranwal
Editor
Technical Writer II
See author profile

Building future-ready infrastructure with Linux, Cloud, and DevOps. Full Stack Developer & System Administrator. Technical Writer @ DigitalOcean | GitHub Contributor | Passionate about Docker, PostgreSQL, and Open Source | Exploring NLP & AI-TensorFlow | Nailed over 50+ deployments across production environments.

Still looking for an answer?

Was this helpful?


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Failed to start server, this is erorr information

code

```Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@centos-512mb-sfo1-01 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Thu 2016-12-08 12:16:10 UTC; 13s ago
  Process: 17625 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 17604 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: Failed to start MySQL Server.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: Unit mysqld.service entered failed state.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: mysqld.service failed.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: mysqld.service holdoff time over, scheduling ...rt.
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: start request repeated too quickly for mysqld...ice
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: Failed to start MySQL Server.
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: Unit mysqld.service entered failed state.
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: mysqld.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos-512mb-sfo1-01 ~]# journalctl -xe
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: Failed to start MySQL Server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has failed.
-- 
-- The result is failed.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: Unit mysqld.service entered failed state.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: mysqld.service failed.
Dec 08 12:16:09 centos-512mb-sfo1-01 systemd[1]: mysqld.service holdoff time over, scheduling restar
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: start request repeated too quickly for mysqld.servi
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: Failed to start MySQL Server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has failed.
-- 
-- The result is failed.
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: Unit mysqld.service entered failed state.
Dec 08 12:16:10 centos-512mb-sfo1-01 systemd[1]: mysqld.service failed.

```code```

Hi, Thanks for the very details steps. I was able to follow all the way to the sudo mysql_secure_installation under Step 3: Configuration. I got an erro saying “Error: Access denied for user ‘root’@‘localhost’ (using password: YES)”. I know I entered the correct temporary password I found from the log file. Also, I realized that I got the same error no matter what password I used. So this appears to be a problem with the user name itself.

Any help is appreciated.

i am getting the same error ,

Thanks for the very details steps. I was able to follow all the way to the sudo mysqlsecureinstallation under Step 3: Configuration. I got an erro saying “Error: Access denied for user ‘root’@‘localhost’ (using password: YES)”. I know I entered the correct temporary password I found from the log file. Also, I realized that I got the same error no matter what password I used. So this appears to be a problem with the user name itself.

Any help is appreciated.

i also tried the

If you skip Step 3 (for now) and try to connect with the first command in Step 4, mysqladmin -u root -p version you should also be prompted for the root password. If you enter the temporary password from the log file, are you able to connect?

no luck same error.

pz help

Hi,

After the step “mysql_secure_installation” i always get an “Estimated strength of the password: 0” What could it be the problem? can I disable this plugin?

Another brilliant article. Thanks for sharing!

Excellent article. Thanks for sharing!

If you have 1GB ram, and use some of it for some process, like tomcat, then service starting may fail because of insufficient memory. You can create swap memory to solve this problem.

here a solution: https://stackoverflow.com/a/13999800/167288

Hi,

I have followed the installation guide and it seems that it hangs when I start the sql server for the first time. Below is a record of the status. Please assist. Thanks.

[root@iZwz92280sqlsr3912i7atZ ~]# sudo systemctl status mysqld mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: activating (start) since Mon 2018-07-23 12:16:43 CST; 4h 54min ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 27963 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0 /SUCCESS) Main PID: 28033 (mysqld) CGroup: /system.slice/mysqld.service ââ28033 /usr/sbin/mysqld

Hello, when I go in step 3, you give me the following error, please can you help me solve it.

Error: Access denied for user ‘root’@‘localhost’ (using password: YES) [root@processagile ~]# mysqladmin -u root -p version Enter password: mysqladmin: connect to server at ‘localhost’ failed error: ‘Access denied for user ‘root’@‘localhost’ (using password: YES)’

Thank you,

I have a problem with this tutorial, after i md5sum mysql80-community-release-el7-3.noarch.rpm command to verify the output is ‘4e07071a356ec12db0d09e1cdd739250 mysql80-community-release-el7-3.noarch.rpm’ that not equals with value on https://dev.mysql.com/downloads/repo/yum/ and then i cannot proceed to install mysql on my server, please help me.

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.