How to Install MySQL 8 on Rocky Linux 9

In this detailed tutorial, we will go through the steps to install MySQL 8 on Rocky Linux 9.

Posted on

MySQL 8 is the latest major release of MySQL which comes with a lot of new features and improvements such as atomic DDL support, window functions, common table expressions, invisible indexes and many more.

In this tutorial, we will install the community version of MySQL 8 on Rocky Linux 9 from the default repositories. We will also look at how to manage the MySQL service, secure the installation, create a database and tables to verify the installation.

Prerequisites

Before starting, make sure you have:

  • A server running Rocky Linux 9 with root access or a non-root user with sudo privileges.
  • Internet connectivity to download MySQL packages.

Step 1 - Enable MySQL 8 Module

First, we need to enable the MySQL 8 module repository on Rocky Linux 9:

sudo dnf module enable mysql -y

This will enable the community MySQL 8.0 modules so we can install the packages.

Step 2 - Install MySQL Server

With the repository enabled, run the below command to install MySQL server:

sudo dnf install @mysql -y

This will download and install MySQL server 8.0 along with other required packages on your Rocky Linux 9 system.

Step 3 - Start MySQL Service

Once installed, we need to start the MySQL service for initial setup:

sudo systemctl start mysqld

This will start the mysqld daemon process in the background.

Step 4 - Secure the MySQL Installation

It is highly recommended to run the mysql_secure_installation script to improve the security of your MySQL installation:

sudo mysql_secure_installation
  • Set a strong root password
  • Remove anonymous user accounts
  • Disable remote root login
  • Remove test database
  • Reload privileges

Answer Yes to all the questions asked by the script to perform the steps above.

Step 5 - Manage MySQL Service

The main MySQL commands to manage the service are:

sudo systemctl start mysqld    # Start MySQL
sudo systemctl stop mysqld     # Stop MySQL
sudo systemctl restart mysqld  # Restart MySQL
sudo systemctl status mysqld   # Check status

This allows you to start, stop, restart and check status of the MySQL service.

Step 6 - Create a Dedicated MySQL User

For improved security, we should create a dedicated MySQL user for applications instead of using the root user directly:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost';

Replace “myuser” and “password” with your desired username and password.

Step 7 - Test MySQL Connection

As a test, connect to the MySQL server using the new user account created:

mysql -u myuser -p
Enter password:

This will connect you to the MySQL shell to run queries.

Step 8 - Create a Sample Database and Table

Let’s create a sample database and table to test MySQL:

CREATE DATABASE mytestdb;
USE mytestdb;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(50)
);

We now have MySQL 8 installed and ready to be used!

Conclusion

In this detailed tutorial, we went through the steps to install MySQL community server 8 on Rocky Linux 9. We also looked at starting, stopping and checking status of the MySQL service. Finally, we tested creating a dedicated user, new database and tables to validate the installation.

You can now use this MySQL server for your web or server applications that require a relational database to store and manage data.

If you want to learn more about MySQL, please use our MySQL tutorials and MySQL Reference.