Tutorial

How To Install MySQL on Rocky Linux 9

How To Install MySQL on Rocky Linux 9
Not using Rocky Linux 9?Choose a different version or distribution.
Rocky Linux 9

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LEMP (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. It implements the relational model and Structured Query Language (SQL) to manage and query data.

This tutorial explains how to install MySQL version 8 on a Rocky Linux 9 server.

Prerequisites

To complete this tutorial, you will need a server running Rocky Linux 9. This server should have a non-root user with administrative privileges and a firewall configured with firewalld. To set this up, see our Initial Server Setup guide for Rocky Linux 9.

Step 1 — Installing MySQL

On Rocky Linux 9, MySQL version 8 is available from the default repositories.

Run the following command to install the mysql-server package and a number of its dependencies:

  1. sudo dnf install mysql-server

When prompted, press y and then ENTER to confirm that you want to proceed:

Output
. . . Install 49 Packages Total download size: 46 M Installed size: 252 M Is this ok [y/N]: y

With that, MySQL is installed on your server but it isn’t yet operational. The package you just installed configures MySQL to run as a systemd service named mysqld.service. In order to use MySQL, you will need to start it with the systemctl command:

  1. sudo systemctl start mysqld.service

To check that the service is running correctly, run the following command. Note that for many systemctl commands — including start and, as shown here, status — you don’t need to include .service after the service name:

  1. sudo systemctl status mysqld

If MySQL was successfully started, the output will show that the MySQL service is active:

Output
● mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2020-03-12 14:07:41 UTC; 1min 7s ago Main PID: 15723 (mysqld) Status: "Server is operational" Tasks: 38 (limit: 5056) Memory: 474.2M CGroup: /system.slice/mysqld.service └─15723 /usr/libexec/mysqld --basedir=/usr Mar 12 14:07:32 rocky9-mysql systemd[1]: Starting MySQL 8.0 database server... Mar 12 14:07:32 rocky9-mysql mysql-prepare-db-dir[15639]: Initializing MySQL database Mar 12 14:07:41 rocky9-mysql systemd[1]: Started MySQL 8.0 database server.

Next, set MySQL to start whenever the server boots up with the following command:

  1. sudo systemctl enable mysqld

Note: If you ever want to change this behavior and disable MySQL from starting up at boot, you can do so by running:

  1. sudo systemctl disable mysqld

MySQL is now installed, running, and enabled on your server. Next, we’ll go over how to harden your database’s security using a shell script that came preinstalled with your MySQL instance.

Step 2 — Securing MySQL

MySQL includes a security script that allows you to change some default configuration options in order to improve MySQL’s security.

To use the security script, run the following command:

  1. sudo mysql_secure_installation

This will take you through a series of prompts asking if you want to make certain changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which you can use to test the strength of your MySQL password.

If you elect to set up the Validate Password Plugin, the script will ask you to choose a password validation level. The strongest level — which you select by entering 2 — will require your password to be at least eight characters long and include a mix of uppercase, lowercase, numeric, and special characters:

Output
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: Y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Regardless of whether you choose to set up the Validate Password Plugin, the next prompt will be to set a password for the MySQL root user. Enter and then confirm a secure password of your choice:

Output
Please set the password for root here. New password: Re-enter new password:

If you used the Validate Password Plugin, you’ll receive feedback on the strength of your new password. Then the script will ask if you want to continue with the password you just entered or if you want to enter a new one. Assuming you’re satisfied with the strength of the password you just entered, enter Y to continue the script:

Output
Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

Following that, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

With that, you’ve installed and secured MySQL on your Rocky Linux 9 server. As a final step, we will test that the database is accessible and working as expected.

Step 3 — Testing MySQL

You can verify your installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MySQL as root (-u root), prompt for a password (-p), and return the installation’s version:

  1. mysqladmin -u root -p version

You will see output similar to this:

Output
mysqladmin  Ver 8.0.28 for Linux on x86_64 (Source distribution)
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.

Server version        8.0.28
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/lib/mysql/mysql.sock
Uptime:            1 min 11 sec

Threads: 2  Questions: 16  Slow queries: 0  Opens: 133  Flush tables: 3  Open tables: 49  Queries per second avg: 0.225

This indicates your installation was successful.

If you’d like to connect to MySQL and begin adding data to it, run the following:

  1. mysql -u root -p

Like the previous mysqladmin command, this command includes the -u option, which allows you to specify the user you’d like to connect as (root in this case), and the -p option, which tells the command to prompt you for the user password you set in the previous step.

After you enter your root MySQL user’s password, you will see the MySQL prompt:

From there, you can begin using your MySQL installation to create and load databases and start running queries.

Conclusion

By following this tutorial, you’ve installed and secured MySQL on a Rocky Linux 9 server. From here, you could install Nginx and PHP to have a fully operational LEMP stack on your server.

To learn more about using MySQL, we encourage you to review the official documentation.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Tony Tran

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.