MySQL
is an open source database management system, commonly installed as part of the popular LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl). 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 CentOS 8 server.
Prerequisites
To complete this tutorial, you will need a server running CentOS 8. This server must 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 CentOS 8.
Step 1 — Installing
MySQL
On CentOS 8, MySQL version 8 is available from the default repositories
. Run the following command to install the mysql-server package and
several of its dependencies
:
- sudo dnf install mysql-server
When prompted, press y, and then press ENTER to confirm that you want to continue:
Output . . . Install 49 packages Total download size: 46 M Installed size: 252 M Okay [y/N]: And
With that, MySQL is installed on your server but not yet operational. The package you just installed configures MySQL to run as a systemd service named mysqld.service. To use MySQL, you will need to start it with the command
systemctl:
- sudo systemctl start mysqld.service
To verify that the service is running correctly, run the following command. Note that for many systemctl commands, including start and, as shown here, status, it is not necessary to include .service after the service name
:
- sudo systemctl status
mysqld If MySQL
started successfully, 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; preset by provider: disabled) Active: active (running) since Thu 2020-03-12 14:07:41 UTC; 1min ago 7s 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 cent-mysql-3 systemd[1]: Starting the MySQL 8.0 database server… Tue 12 14:07:32 cent-mysql-3 mysql-prepare-db-dir[15639]: Initializing MySQL database Mar 12 14:07:41 cent-mysql-3 systemd[1]: MySQL 8.0 database server started. Next, configure MySQL
to start each time the server boots with the following command:
- sudo systemctl enable mysqld
MySQL is now installed, running, and enabled on your server. Next, we’ll go over how to harden the security of your database using a shell script that comes pre-installed with your MySQL instance.
Step 2 — Protecting
MySQL MySQL includes a security script that allows you to
change some default configuration options to improve
MySQL security.
To use the security script, run the following command:
- sudo mysql_secure_installation
This will take you through a series of messages asking if you want to make certain changes to the security options of your MySQL installation. The first message will ask if you want to configure the Validate Password plugin, which you can use to test the strength of your MySQL password.
If you choose to
configure the Validate Password snap-in, the script prompts 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 combination of uppercase, lowercase, numeric, and special characters:
OutputSecure MySQL server deployment. Connect to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of the password and allows users to set only those passwords that are strong enough. Do you want to configure the VALIDATE PASSWORD component? Press y| And for Yes, any other key for No: AND There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case and special characters STRONG Length >= 8, numeric, uppercase and lowercase, special characters, and dictionary file Enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Regardless of whether you choose to configure the Validate Password plugin, the next message will be to set a password for the MySQL root user. Enter and then confirm a strong password of your choice:
OutputSet the password for root here. New password: Re-enter a new password:
If you used the Validate Password plugin, you will receive feedback on the strength of your new password. The script will then ask if you want to proceed with the password you just entered or if you want to enter a new one. Assuming you are satisfied with the strength of the password you just entered, enter Y
to proceed with the script: OutputEstimated password security: 100 Do you want to proceed with the password provided? (Press y| And for Yes, any other key for No) : And
After that, you can press Y and then ENTER to accept the defaults for all 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 honors the changes you’ve made.
With that, you have installed and secured MySQL on your CentOS 8 server. As a final step, we will test that the database is accessible and works as expected.
Step 3 — MySQL Test
You can verify your installation and get information about it by connecting to the mysqladmin tool, a client that allows you to run administrative commands. Use the following command to connect to MySQL as
root (-u root), request a password (-p), and return the installation version: mysqladmin –
- u root -p version
You will see output similar to the following:
mysqladmin Ver 8.0.17 for Linux on x86_64 (Source distribution) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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.17 Protocol version 10 Localhost connection via UNIX socket UNIX socket /var/lib/mysql/mysql.sock Uptime: 2 hours 52 min 37 sec Threads: 2 Questions: 20 Slow queries: 0 Openings: 131 Download tables: 3 Open tables: 48 Queries per second Average: 0.001
This indicates that the installation was successful.
If you want to connect to MySQL and start adding data to it, Run the following:
- mysql
-u root -p Like the previous mysqladmin command, this command includes the -u option, which allows you to specify the user you want to connect to (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 entering the password of your MySQL root user, You will see the MySQL prompt:
From there, you can start using your MySQL installation to create and load databases and start running queries
.
Conclusion
By following this tutorial, you have installed and secured MySQL on a CentOS 8 server. From here, you can install Nginx and PHP to have a fully operational LEMP stack on your server.
For more information on using MySQL, we recommend that you review the official documentation.