MySQL is a free, open-source, and extremely popular relational database management system. It is a simple, fast, and scalable SQL-based database system that can be installed as part of a LAMP or LEMP stack. Due to its speed and efficiency, it is the best choice for e-commerce and data warehousing applications. MySQL is readily available for all Linux distributions as well as other operating systems.

In this post, we will explain how to install MySQL 8 on Rocky Linux 8.

Install MySQL 8.0 on Rocky Linux 8

By default, MySQL 8.0 is included in the Rocky Linux AppStream repository. You can verify the available MySQL versions with the following command:

dnf module list mysql

You will get the following output:

Rocky Linux 8 - AppStream -13 MB/s | 9.7 MB     00:00    
Rocky Linux 8 - BaseOS -6.4 MB/s | 6.8 MB     00:01    
Rocky Linux 8 - Extras - 22 kB/s |  12 kB     00:00    
Rocky Linux 8 - AppStream - mysql- 8.0 [d]- client, server [d]- MySQL Module                        

Next, enable the MySQL repository with the following command:

dnf module enable mysql:8.0

You will get the following output:

Last metadata expiration check: 0:00:15 ago on Tuesday 05 April 2022 03:20:47 PM UTC.
Dependencies resolved.
==============================================================================================================================================
 Package                           Architecture                     Version                           Repository                         Size
==============================================================================================================================================
Enabling module streams:
 mysql                                                              8.0                                                                      

Transaction Summary
==============================================================================================================================================

Is this ok [y/N]: y

Now, install MySQL 8.0 by running the following command:

dnf install @mysql -y

Once the MySQL is installed, you can verify the installed version of MySQL using the following command:

mysql --version

You will get the following output:

mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)

Also Read

How to Delete or Remove Databases in MySQL

Manage MySQL 8.0 Service

By default, the MySQL service is managed by systemd. You can use the systemctl command to manage it from the command line.

To start the MySQL service, run the following command:

systemctl start mysqld

Enable the MySQL service to start at system reboot with the following command:

systemctl enable mysqld

To check the status of the MySQL service, run the following command:

systemctl status mysqld

You will get the following output:

● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-04-05 15:42:24 UTC; 13s ago
  Process: 26108 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 25984 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 25960 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 26064 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 11412)
   Memory: 464.0M
   CGroup: /system.slice/mysqld.service
           └─26064 /usr/libexec/mysqld --basedir=/usr

Apr 05 15:42:16 rockylinux systemd[1]: Starting MySQL 8.0 database server...
Apr 05 15:42:16 rockylinux mysql-prepare-db-dir[25984]: Initializing MySQL database
Apr 05 15:42:24 rockylinux systemd[1]: Started MySQL 8.0 database server.

Secure MySQL Installation

By default, MySQL is not secured and the root password is not set. You can secure it by running the following command:

mysql_secure_installation

You will be asked to set up the VALIDATE PASSWORD component:

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

Type Y and press the Enter key. You will be asked to choose the level of password validation policy:

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: 1

Type your preferred option and press the Enter key. You will be asked to set a root password:

Please set the password for root here.

New password: 

Re-enter new password: 

Set your root password and press the Enter key. You will be asked to continue with the provided password:

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

Type Y and press the Enter key. You will be asked to remove anonymous users:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y

Type Y and press the Enter key. You will be asked to remove the test database:

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y

Type Y and press the Enter key. You will be asked to reload the privilege tables:

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

Type Y and press the Enter key to finish the process.

Also Read

How to Allow Remote Connection to MySQL Database Server

Connect to the MySQL Server

After securing the MySQL, you can connect to the MySQL shell using the following command:

mysql -u root -p

You will be asked to provide your root password:

Enter password: 

Provide your root password and press the Enter key. Once you are logged in, you should see the MySQL shell in the following output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, 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> 

You can check the MySQL version with the following command:

mysql> SELECT VERSION ();

You will get the following output:

+------------+
| VERSION () |
+------------+
| 8.0.26     |
+------------+

Create a Database and User in MySQL

To create a database named testdb, run the following command:

mysql> CREATE DATABASE testdb;

To create a user named testuser, run the following command:

mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'securepassword';

To grant all the privileges to the testdb database, run the following command:

mysql> GRANT ALL ON testdb.* TO 'testuser'@'localhost';

Now, flush the privileges to apply the changes:

mysql> FLUSH PRIVILEGES;

To list all MySQL users, run the following command:

mysql> select user from mysql.user;

You will get the following output:

+------------------+
| user             |
+------------------+
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| testuser         |
+------------------+

To list all databases, run the following command:

mysql> SHOW DATABASES;

You should see the following output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+

Now, exit from MySQL using the following command:

mysql> EXIT;

Remove or Uninstall MySQL 8

If you don’t need to use MySQL and want to remove it from your system, run the following command:

dnf remove @mysql

After removing MySQL, remove the MySQL data directory with the following command:

rm -rf /var/lib/mysql*

Conclusion

In the above post, we showed you how to install MySQL 8.0 on Rocky Linux 8. We also explained how to create a database and user in MySQL. Try it on a dedicated server from Atlantic.Net!