How to Install MySQL on CentOS or Rocky Linux

December 10, 2024

Introduction

MySQL is a popular open-source relational database management application. It is part of the LAMP Stack (Linux, Apache, MySQL, PHP), a software stack that powers web servers.

This guide will show you how to install MySQL on CentOS or Rocky Linux.

How to install MySQL on CentOS or Rocky Linux - a tutorial.

Prerequisites

Guide to Installing MySQL on CentOS or Rocky Linux

MySQL availability in CentOS and Rocky Linux default repositories depends on the system version. In CentOS 7, MySQL was removed from the repository, and MariaDB was included by default as the relational database in their official repositories.

MySQL was later added back to the appstream repo, although it is usually not the latest available version. In this tutorial, we will use the Oracle MySQL repository to obtain the latest available program version.

Step 1: Download MySQL Repository

Follow the steps below to download the MySQL repository to your system and prepare it for use:

1. Open a browser and navigate to the official MySQL yum repository download page.

2. From the list of repository packages, find the one for your system and click the Download button. For this tutorial, we will use Red Hat Enterprise Linux 9 / Oracle Linux 9 for our Rocky Linux 9 system.

Repository setup packages

3. Before the download starts, the page prompts you to create an Oracle Web Account. Scroll to the bottom of the page and find the No thanks, just start my download link and copy it.

4. Open the terminal and use the wget command to save the file. The syntax is:

sudo wget [link]

For example:

sudo wget https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
Download MySQL yum repository to Rocky Linux.

Wait for the system to download the files.

Step 2: Add the Software Repository

Next, install the downloaded repository. Use the syntax below:

sudo rpm -Uvh [file_name]

Replace the [file_name] with the exact file name of the repository you downloaded in the previous step. The easiest way is to copy the file name from the download output. For example:

sudo rpm -Uvh mysql84-community-release-el9-1.noarch.rpm
Installing the MySQL yum repository.

The command adds the new Yum repository from which we can get MySQL.

Step 3: Install MySQL

After adding the repository, you can install the latest MySQL version. Follow the steps below:

1. Update the system package index with:

sudo yum update
Updating the system package index to get the latest program version.

The system now reaches out to the new repository and updates the package index.

2. Install MySQL with:

sudo yum install mysql-server -y
Installing MySQL on CentOS/Rocky Linux.

Wait for the process to complete.

Note: MySQL includes several security plugins to authenticate connections to the server, password verification and securing storage for sensitive data. All of these are available in the free version.

Using MySQL

After the installation completes, you can start using the MySQL server. This section covers the basic setup after installation.

Managing MySQL Service

Run the following command to start the MySQL service after installation:

sudo systemctl start mysqld

To check the status of MySQL, use the command:

sudo systemctl status mysqld
Checking the MySQL service status after installation.

The system displays several lines of information about the MySQL service. In the Active line, it should display the active: (running) message. Return to the terminal with q.

By default, the MySQL service is set to launch at startup. To disable it, you can use the disable command:

sudo systemctl disable mysqld

To stop the MySQL service, use the stop command:

sudo systemctl stop mysqld

Find Temporary Password

The MySQL installation routine sets up a default temporary password. Use the grep command to find the password:

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

Copy the password for the next step.

Obtaining the temporary root password after MySQL installation.

Configuring and Securing

Your new MySQL installation comes with a security script to simplify security configuration. Refer to our mysql_secure_installation guide to learn more about the script and how to improve MySQL server security significantly.

Launch the script with the following terminal command:

sudo mysql_secure_installation

Use the password you copied in the previous step to access the script and set up a new one. Follow the steps outlined in the guide to complete your MySQL server configuration.

Note: If you receive an access denied error, see how to resolve the Access denied for user 'root'@'localhost' error.

Log into MySQL

To launch MySQL from the command line, use the command:

mysql -u root -p

Enter the password you set up with the security script in the previous step, and the system should respond by displaying the MySQL shell:

Logging in to the MySQL shell as the root user.

It lists the necessary information about the MySQL software, and your command prompt will change to mysql> to indicate that you are logged into it.

Once you are done, you can log out with:

exit

The command prompt changes, indicating that you have logged out of MySQL.

Conclusion

This tutorial showed how to install and configure MySQL on a CentOS or Rocky Linux system. The fast, reliable, and open-source relational database management system is widely used for efficiently managing and querying structured data in applications.

Next, check out our article on PostgreSQL vs MySQL if you are looking for an alternative Relational Database Management System (RDBMS).

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Create MySQL Database in Workbench
October 17, 2024

Workbench is a cross-platform, open-source, visual tool for database management. It provides a user-friendly...
Read more
How to Improve MySQL Performance With Tuning
April 25, 2024

The performance of MySQL databases is an essential factor in the optimal operation of your server. Make sure...
Read more
How to Install ClickHouse on CentOS 7
August 23, 2019

ClickHouse is an open-source column-oriented database management system. It is a fast, scalable, and...
Read more
How to Check the MySQL Version In Linux
July 11, 2019

It is essential to know which version of MySQL you have installed. The version number helps to determine if a...
Read more