main logo

How to Install MySQL on Rocky Linux 8

How to Install MySQL on Rocky Linux 8

Rocky Linux 8, a community-driven enterprise operating system, serves as a robust platform for deploying database servers like MySQL. This guide will walk you through the installation of MySQL Community Server on Rocky Linux 8, ensuring you have a stable and secure database environment. We will cover enabling the MySQL module, installing the necessary packages, securing your MySQL installation, and managing the service. For broader server management needs, including security and optimization, consider exploring Linux Server Support services.

MySQL is a popular open-source relational database management system (RDBMS) widely used for web applications and other software requiring persistent data storage. Following this tutorial will provide you with a functional MySQL 8.0 installation.

Prerequisites

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

If you haven’t set up a sudo user, please do so before continuing. You can find guides on initial server setup for Rocky Linux 8.

Step 1 — Enabling the MySQL Module

Rocky Linux 8, like CentOS 8, utilizes AppStreams for software management, and MySQL is available as a module. By default, the MySQL 8.0 stream is typically active. You can check the available MySQL modules and their status with the following command:

sudo dnf module list mysql

This command will display a list of available MySQL streams, such as mysql:8.0. The [d] flag indicates the default stream, and [e] indicates the enabled stream. For most new installations, MySQL 8.0 will be the default and enabled stream.

If you needed to switch to a different stream (though generally not recommended unless you have specific legacy requirements and understand the implications), you would first reset the module and then enable your desired stream. For instance, to enable a hypothetical older stream (not applicable for a standard MySQL 8.0 install):

# sudo dnf module reset mysql
# sudo dnf module enable mysql:<stream_version>

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

Step 2 — Installing MySQL

Once you have confirmed that the MySQL 8.0 module stream is active (or enabled it if necessary), you can install the MySQL server packages using the dnf package manager. The @mysql group package conveniently installs mysql-community-server and its dependencies.

sudo dnf install @mysql

You will be prompted to confirm the installation and the packages to be downloaded. Press y and then Enter to proceed.

After the installation is complete, you need to start the MySQL service:

sudo systemctl start mysqld

To ensure the service has started correctly, check its status:

sudo systemctl status mysqld

The output should show the service as active (running).

During the initial startup, MySQL generates a temporary password for the root user. This password is logged in the MySQL error log. You can retrieve it using grep:

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

Carefully note this temporary password, as you will need it in the next step to secure your MySQL installation.

Step 3 — Securing MySQL

MySQL includes a security script called mysql_secure_installation that helps you perform several crucial security configurations. This script will guide you through setting a new root password, removing anonymous users, disallowing remote root logins, and removing the test database.

Run the script with sudo privileges:

sudo mysql_secure_installation
  1. Enter Temporary Password: The script will first ask for the temporary root password you noted from the mysqld.log.
  2. VALIDATE PASSWORD Component: You will then be asked if you want to set up the VALIDATE PASSWORD component. This component enforces password strength policies. It is highly recommended to enable this for production servers. If you choose Yes (Y), you will be asked to select a password validation policy level: 0 (LOW), 1 (MEDIUM), or 2 (STRONG).
  3. Set New Root Password: After configuring (or skipping) the VALIDATE PASSWORD component, you will be prompted to set a new password for the MySQL root user. Choose a strong, unique password.
  4. Security Questions: The script will then ask a series of yes/no questions to improve security:
    • Remove anonymous users? (Recommended: Yes) Anonymous users are a security risk.
    • Disallow root login remotely? (Recommended: Yes) Restricting root login to localhost only is a critical security measure.
    • Remove test database and access to it? (Recommended: Yes) The test database is for testing purposes and should be removed on production servers.
    • Reload privilege tables now? (Recommended: Yes) This applies all the changes you’ve made immediately.

Completing these steps will significantly enhance the security of your MySQL server. For organizations requiring advanced security configurations or ongoing database administration, exploring professional MySQL support can provide expert assistance.

Step 4 — Testing MySQL

After securing your MySQL installation, you should test if you can log in with the new root password.

mysql -u root -p

Enter the new root password you set during the mysql_secure_installation process. If the login is successful, you will see the MySQL monitor prompt (mysql>).

You can run a simple command to verify, such as checking the MySQL version:

SELECT VERSION();

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

The MySQL service on Rocky Linux 8 is managed using systemctl.

  • Start MySQL:
    bash
    sudo systemctl start mysqld
  • Stop MySQL:
    bash
    sudo systemctl stop mysqld
  • Restart MySQL:
    bash
    sudo systemctl restart mysqld
  • Check MySQL Status:
    bash
    sudo systemctl status mysqld
  • Enable MySQL on Boot: To have MySQL start automatically when the server boots:
    bash
    sudo systemctl enable mysqld
  • Disable MySQL on Boot: To prevent MySQL from starting automatically:
    bash
    sudo systemctl disable mysqld

Conclusion

You have successfully installed and secured MySQL 8.0 on your Rocky Linux 8 server. Your database server is now ready to host your application data. Remember to perform regular backups and keep your system and MySQL packages updated.

For further learning and related guides, consider the following:

Effective database management is crucial for application performance and security. If your business requires expert assistance with database administration, or if you are looking for comprehensive Managed IT Services, Medha Cloud offers a range of solutions to support your infrastructure. For Managed Service Providers (MSPs), Medha Cloud also provides white-label managed IT services, allowing you to expand your service offerings seamlessly.

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.