Install MySQL on Rocky Linux 9 | Efficient Steps – OrcaCore

Posted on

Install MySQL on Rocky Linux 9 | Efficient Steps - OrcaCore

Install MySQL on Rocky Linux 9 | Efficient Steps – OrcaCore

This guide, brought to you by Orcacore, will walk you through the process of How To Install MySQL on Rocky Linux 9. MySQL is a widely-used, open-source relational database management system (RDBMS) based on a client-server architecture. RDBMS software enables the creation and management of databases that adhere to the relational model, organizing data into tables with rows and columns. Mastering the Install MySQL on Rocky Linux 9 process is crucial for developers and system administrators working with data-driven applications.

Steps To Install and Access MySQL on Rocky Linux 9

Before we begin, ensure you have a non-root user account with sudo privileges on your Rocky Linux 9 server. If you haven’t already, follow our guide on Initial Server Setup with Rocky Linux 9 to configure this. Successfully achieving the Install MySQL on Rocky Linux 9 is contingent on having these prerequisites met.

1. Install MySQL on Rocky Linux 9

The first step is to update your local package index using the dnf package manager. This ensures you have the latest package information.

sudo dnf update -y

Next, install the mysql-server package along with its dependencies. This package contains the MySQL server software and necessary supporting libraries.

sudo dnf install mysql-server -y

Once the installation is complete, you need to start the MySQL service and enable it to start automatically at boot time.

Start and Enable MySQL Service

Use the following commands to start the MySQL service and enable it to launch on system startup:

# sudo systemctl start mysqld.service
# sudo systemctl enable mysqld

You can verify that the MySQL service is active and running by checking its status:

sudo systemctl status mysqld

The output should resemble the following:

Check MySQL status

Now that you have MySQL installed, it’s crucial to secure it by running the provided security script.

2. Change MySQL Default Root Password

MySQL initially generates a temporary root password when the service starts for the first time. This password is located in the MySQL log file at /var/log/mysqld.log.

To retrieve the default root password, use the following command:

grep 'temporary' /var/log/mysqld.log

The output will display the temporary password, similar to this:

Display default MySQL root password

Run the mysql_secure_installation script to enhance the security of your MySQL installation and change the default root password:

sudo mysql_secure_installation

The script will prompt you for the current root password. Enter the temporary password you retrieved from the log file.

You will then be prompted to set a new password:

The existing password for the user account root has expired. Please set a new password. 
New password: <---- SET NEW PASSWORD
Re-enter new password: <---- RE-ENTER NEW PASSWORD

Enter your desired new password and confirm it. The script will then guide you through several security-related questions. It’s generally recommended to answer "yes" to all of them to improve the security of your MySQL installation.

Access MySQL Shell

You can now access the MySQL shell using the following command, providing the new password you just set:

sudo mysql -u root -p

You will be prompted for your password. After entering it, you should see the MySQL console:

Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 15
Server version: 8.0.31 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.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

For further details, refer to the MySQL Documentation.

3. Uninstall MySQL From Rocky Linux

If you decide to remove MySQL from your system, use the following command:

sudo dnf remove mysqld

Conclusion

This guide has shown you How To Install MySQL on Rocky Linux 9. MySQL on Rocky Linux 9 offers a robust, secure, and high-performance database solution, making it suitable for a wide range of applications.

You might also find these articles helpful:

Alternative Solutions for Installing MySQL on Rocky Linux 9

While the standard dnf method is common, here are two alternative approaches to install MySQL on Rocky Linux 9: using a MySQL Yum Repository and using Docker.

1. Installing MySQL Using the MySQL Yum Repository

This method allows you to have more control over the specific MySQL version you install and receive updates directly from MySQL.

Explanation:

MySQL provides its own Yum repository, which contains the latest versions of MySQL server, client tools, and other related packages. By adding this repository to your system’s package manager configuration, you can install MySQL directly from the source. This is useful if you need a specific version not available in the default Rocky Linux repositories or want to ensure you receive timely updates.

Steps:

  1. Download the MySQL Yum Repository package: Visit the MySQL website’s download page for the MySQL Yum Repository RPM package. Choose the package compatible with your Rocky Linux 9 system. You can download directly from the command line using wget:

    wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

    Note: The exact filename might change depending on the latest version. Check the MySQL website for the correct URL.

  2. Install the MySQL Yum Repository: Use the rpm command to install the downloaded package:

    sudo rpm -ivh mysql80-community-release-el9-1.noarch.rpm
  3. Choose a specific MySQL version (Optional): By default, the latest MySQL 8.0 version will be installed. If you want to install a different version (e.g., MySQL 5.7), you need to disable the default repository and enable the desired one. You can use the dnf config-manager command to do this. First list the available modules.

     sudo dnf module list mysql

    Then enable the desired module.

     sudo dnf module enable mysql:version_number
  4. Install MySQL: Now, use the dnf command to install the mysql-server package.

    sudo dnf install mysql-server -y
  5. Start and Enable MySQL: As before, start and enable the MySQL service.

    sudo systemctl start mysqld
    sudo systemctl enable mysqld

The remaining steps for securing the installation and accessing the MySQL shell are the same as described in the original article.

2. Installing MySQL Using Docker

This method provides a containerized environment for MySQL, isolating it from the host system and simplifying deployment.

Explanation:

Docker allows you to run applications in isolated containers. Using a Docker image for MySQL means you don’t have to install the database directly on your Rocky Linux 9 system. This is particularly useful for development and testing, as it provides a consistent and reproducible environment. It also simplifies the process of managing multiple MySQL instances.

Steps:

  1. Install Docker: If you haven’t already, install Docker on your Rocky Linux 9 system.

    sudo dnf install docker -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the MySQL Docker Image: Pull the official MySQL Docker image from Docker Hub. You can specify a version tag (e.g., 8.0) to get a specific version. If you don’t specify a tag, the latest version will be pulled.

    sudo docker pull mysql:8.0
  3. Run the MySQL Docker Container: Create and run a Docker container based on the MySQL image. You need to set the MYSQL_ROOT_PASSWORD environment variable to define the root password. You also might want to map a port from the container to your host system.

    sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=your_root_password -p 3306:3306 -d mysql:8.0
    • --name mysql-container: Assigns a name to the container.
    • -e MYSQL_ROOT_PASSWORD=your_root_password: Sets the root password for the MySQL instance. Replace your_root_password with a strong password.
    • -p 3306:3306: Maps port 3306 on the host to port 3306 in the container. This allows you to connect to MySQL from your host system.
    • -d: Runs the container in detached mode (in the background).
    • mysql:8.0: Specifies the MySQL image to use.
  4. Access MySQL Shell: You can access the MySQL shell inside the Docker container using the docker exec command:

    sudo docker exec -it mysql-container mysql -u root -p

    You will be prompted for the root password you set when running the container.

These alternative methods offer different advantages depending on your specific needs. Using the MySQL Yum repository provides more control over versioning and updates, while Docker offers isolation and portability. The core article and the two alternative solutions presented provide options that should satisfy most users looking to Install MySQL on Rocky Linux 9.

Leave a Reply

Your email address will not be published. Required fields are marked *