main logo

How to Install MySQL on CentOS 8

How to Install MySQL on CentOS 8

Note: CentOS 8 reached its End Of Life (EOL) on December 31, 2021. This guide is provided for informational purposes for systems that may still be in use, but it is strongly recommended to migrate to a supported operating system like Rocky Linux 8 or AlmaLinux 8. For assistance with migration or managing your existing servers, consider exploring Linux Server Support options.

MySQL is a powerful open-source relational database management system used by a vast number of applications worldwide. This tutorial will walk you through installing MySQL Community Server on a CentOS 8 server. We will cover enabling the MySQL module, installing the packages, securing your installation, and managing the MySQL service.

Before you begin, ensure you have a CentOS 8 server and a non-root user with sudo privileges.

Prerequisites

  • A CentOS 8 server.
  • A non-root user with sudo privileges.
  • Access to a terminal or command line interface.

If you need to set up a user with sudo privileges, refer to initial server setup guides for CentOS 8.

Step 1 — Enabling the MySQL Module

CentOS 8 uses AppStreams, and MySQL is available as a module. By default, MySQL 8.0 is the active stream. You can verify this and see available streams:

sudo dnf module list mysql

You should see mysql:8.0 listed, likely with [d] for default and [e] for enabled if it’s the default active stream.

If you needed to switch to a different stream (e.g., if a mysql:5.7 stream were available and desired, though 5.7 is also nearing EOL), you would first reset the module and then enable the specific stream:

# Example for switching (not typically needed for 8.0 on a fresh setup)
# sudo dnf module reset mysql
# sudo dnf module enable mysql:5.7 

For this guide, we will proceed with the default MySQL 8.0 stream.

Step 2 — Installing MySQL

With the MySQL 8.0 module stream active (which is usually the case by default), you can install the MySQL server package using dnf:

sudo dnf install @mysql

The @mysql group package installs mysql-community-server and its dependencies. Press y and then Enter when prompted to confirm the installation.

Once the installation is complete, start the MySQL service:

sudo systemctl start mysqld

Verify that the MySQL service is running:

sudo systemctl status mysqld

The output should indicate that the service is active (running).

MySQL creates a temporary root password during the installation process. You can find this password in the MySQL error log file:

sudo grep 'temporary password' /var/log/mysql/mysqld.log

Note down this temporary password, as you will need it for the next step.

Step 3 — Securing MySQL

MySQL provides a script, mysql_secure_installation, to help you improve the security of your MySQL server. This script will guide you through setting the root password, removing anonymous users, disallowing remote root login, and removing the test database.

Run the script with sudo:

sudo mysql_secure_installation

You will first be prompted to enter the temporary root password you retrieved from the log file.

Next, the script will ask if you want to use the VALIDATE PASSWORD component. Enabling this component enforces password strength policies. It is generally recommended for production environments. If you choose to enable it, you will select a password validation policy level (0 for LOW, 1 for MEDIUM, or 2 for STRONG).

After this, you will be prompted to set a new root password. Choose a strong, unique password.

The script will then ask a series of security-related questions:

  • Remove anonymous users? (Recommended: Yes)
  • Disallow root login remotely? (Recommended: Yes, unless remote root access is absolutely necessary and secured through other means. It’s a significant security risk.)
  • Remove test database and access to it? (Recommended: Yes)
  • Reload privilege tables now? (Recommended: Yes, to apply your changes immediately.)

Answering these prompts appropriately will harden your MySQL installation. For advanced security configurations or ongoing database management, consider seeking professional MySQL support.

Step 4 — Testing MySQL

Once you have secured your installation, you can test access to the MySQL server using the new root password:

mysql -u root -p

Enter the root password you configured in the previous step. A successful login will bring you to the MySQL monitor prompt:

mysql>

You can execute a simple query, like checking the server version:

SELECT VERSION();

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

MySQL on CentOS 8 is managed using systemctl.

To start the MySQL service:

sudo systemctl start mysqld

To stop the MySQL service:

sudo systemctl stop mysqld

To restart the MySQL service:

sudo systemctl restart mysqld

To check the status of the MySQL service:

sudo systemctl status mysqld

To enable MySQL to start automatically when the server boots:

sudo systemctl enable mysqld

To disable MySQL from starting automatically at boot:

sudo systemctl disable mysqld

Conclusion

You have now successfully installed and secured MySQL 8.0 on your CentOS 8 server. Although CentOS 8 is EOL, this setup provides a functional database server. It is crucial to plan a migration to a supported OS for long-term stability and security.

For further information and related guides, you may find these useful:

Effective database administration is an ongoing task. If your business requires expert assistance with database management, security, or is considering Managed IT Services, Medha Cloud offers a suite of solutions. For Managed Service Providers (MSPs) looking to enhance their offerings, Medha Cloud also provides white-label managed IT services.

Medha Cloud
Medha Cloud
Medha Cloud, a leading multi-cloud service provider, focuses on cloud computing and digital transformation. Every article published under the Medha Cloud brand is a collaborative effort by our team. We are committed to sharing insights, best practices, how-tos, reviews, and real-world examples to help organizations stay ahead of the curve and thrive in the digital age
Share
Contents

Related Articles

medhacloud logo
USA:
Medha Cloud Solutions LLC
30 N Gould St Ste R, Sheridan, WY 82801,
Phone: +1 646 775 2855

India:
Medha Cloud Solutions Private Limited
#74, 7th Cross, Krishna Garden InCity Layout. Chikka Kammanahalli, Banneraghatta Road, Bangalore 560083
Phone:+91 93536 44646

E-Mail: sales@medhahosting.com
©Medha Cloud 2024. All rights reserved.