main logo

How to Install MySQL on Rocky Linux 9

How to Install MySQL on Rocky Linux 9

Rocky Linux 9, a leading enterprise-grade operating system, provides a stable and secure foundation for deploying critical services like MySQL. This tutorial will guide you through the installation of MySQL Community Server on Rocky Linux 9. We will cover enabling the appropriate MySQL module, installing the server packages, performing essential security configurations, and managing the MySQL service. For comprehensive server administration, including ongoing security and performance optimization, consider leveraging Linux Server Support services.

MySQL is a widely adopted open-source relational database management system (RDBMS), essential for a multitude of web applications and software that require robust data storage. By following this guide, you will set up a functional MySQL 8.0 (or newer, as per available stable streams) installation on your Rocky Linux 9 server.

Prerequisites

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

Ensure you have a sudo-enabled user before proceeding. Standard initial server setup guides for Rocky Linux 9 can provide instructions if needed.

Step 1 — Enabling the MySQL Module

Rocky Linux 9 utilizes AppStreams for software package management, and MySQL is provided as a module. Typically, the latest stable MySQL stream (e.g., MySQL 8.0 or a newer LTS version if available and default) is active. You can verify the available MySQL modules and their current status using:

sudo dnf module list mysql

This command will output a list of MySQL streams, such as mysql:8.0. The [d] flag indicates the default stream, and [e] signifies the enabled stream. For most fresh installations on Rocky Linux 9, the latest stable MySQL stream will be the default and enabled.

If you had a specific requirement to use a different stream (which is generally not recommended unless for specific, understood legacy needs), you would first reset the module and then enable your chosen stream. For example:

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

For this tutorial, we will proceed with the default active MySQL stream, which is typically MySQL 8.0 or a more recent LTS version.

Step 2 — Installing MySQL

With the appropriate MySQL module stream confirmed as active, you can install the MySQL server packages using the dnf package manager. The @mysql group package is a convenient way to install mysql-community-server along with its dependencies.

sudo dnf install @mysql

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

Once the installation process is complete, you must start the MySQL service:

sudo systemctl start mysqld

To confirm that the service has started successfully, check its status:

sudo systemctl status mysqld

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

During its first startup, MySQL generates a temporary password for the root user. This password is vital for the initial security setup and is logged in the MySQL error log. You can retrieve it using the grep command:

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

Make a secure note of this temporary password, as it will be required in the next step.

Step 3 — Securing MySQL

MySQL includes a crucial security script named mysql_secure_installation. This script automates several important security hardening tasks, such as setting a new root password, removing anonymous user accounts, disallowing remote root logins, and removing the default test database.

Execute the script with sudo privileges:

sudo mysql_secure_installation
  1. Enter Temporary Password: The script will first prompt you for the temporary root password that you retrieved from the mysqld.log file.
  2. VALIDATE PASSWORD Component: You will then be asked if you wish to set up the VALIDATE PASSWORD component. This component helps enforce strong password policies and is highly recommended for production environments. If you choose Yes (Y), you will be prompted to select a password validation policy level: 0 (LOW), 1 (MEDIUM), or 2 (STRONG).
  3. Set New Root Password: After addressing the VALIDATE PASSWORD component, you will be prompted to create and confirm a new password for the MySQL root user. Ensure you choose a strong, unique password.
  4. Security Questions: The script will proceed with a series of yes/no questions designed to enhance your MySQL server’s security:
    • Remove anonymous users? (Recommended: Yes) Anonymous users pose a security risk and should be removed.
    • Disallow root login remotely? (Recommended: Yes) Restricting root login to localhost only is a critical security best practice.
    • Remove test database and access to it? (Recommended: Yes) The test database is intended for testing purposes only and should be removed from production servers.
    • Reload privilege tables now? (Recommended: Yes) This action applies all the security changes you have made immediately.

Successfully completing these steps will significantly improve the security posture of your MySQL installation. For organizations that require advanced security configurations or ongoing database management, considering professional MySQL support can provide valuable expertise and peace of mind.

Step 4 — Testing MySQL

After securing your MySQL installation, it is important to test that you can log in using the new root password.

mysql -u root -p

Enter the new root password you configured during the mysql_secure_installation process. A successful login will present you with the MySQL monitor prompt (mysql>).

You can execute a simple command to verify functionality, such as displaying the MySQL server version:

SELECT VERSION();

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

The MySQL service on Rocky Linux 9 is managed using the systemctl utility.

  • 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 configure MySQL to start automatically when the server boots up:
    bash
    sudo systemctl enable mysqld
  • Disable MySQL on Boot: To prevent MySQL from starting automatically at boot:
    bash
    sudo systemctl disable mysqld

Conclusion

You have now successfully installed and secured MySQL on your Rocky Linux 9 server. Your database server is prepared to support your applications with a reliable data storage solution. It is crucial to implement regular backup procedures and keep your system and MySQL packages updated to maintain security and stability.

For additional information and related tutorials, you may find the following resources helpful:

Proactive database management is key to ensuring optimal application performance and data security. If your organization requires specialized assistance with database administration, or if you are considering comprehensive Managed IT Services, Medha Cloud offers a suite of solutions designed to support your infrastructure effectively. Furthermore, for Managed Service Providers (MSPs), Medha Cloud provides white-label managed IT services, enabling you to broaden your service offerings under your own brand.

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.