MySQL
is an open source database management system, commonly installed as part of the popular LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl). It uses a relational database and SQL (Structured Query Language) to manage its data.
CentOS 7 prefers MariaDB, a fork of MySQL managed by the original MySQL developers and designed as a replacement for MySQL. If you run yum install mysql on CentOS 7, it is MariaDB that is installed instead of MySQL. If you’re wondering about MySQL vs. MariaDB, MariaDB will usually work smoothly instead of MySQL, so unless you have a specific use case for MySQL, check out the How to Install MariaDB on Centos 7 guide.
This tutorial will explain how to install MySQL version 8 on a CentOS 7 server.
Prerequisites
To follow this tutorial, you will need:
- A CentOS 7 with a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in the Initial Server Setup with CentOS 7 guide.
Step 1 — Installing
MySQL
As mentioned in the introduction, the Yum command to install MySQL actually installs MariaDB. To install MySQL, we will need to visit the MySQL Yum Repository community that provides packages for MySQL.
In a web browser, visit:
https://dev.mysql.com/downloads/repo/yum/
Note that prominent download links do not lead directly to files. Instead, they lead to a later page where you are invited to log in or sign up for an account. If you don’t want to create an account, you can locate the text “No thanks, just start my download”, then right-click and copy the link location, or you can edit the version number in the commands below.
Locate the desired version and update it as needed at the following link
:
- curl -sSLO https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
Once the rpm file is saved, we will verify the integrity of the download by running
md5sum and comparing it with the corresponding MD5 value that appears on the site: md5sum mysql80-community-release-el7-5.noarch.rpm outpute2bd920ba15cd3d651c1547661c60c7c
- mysql80-community-release-el7-5.noarch.rpm
Compare this output with the appropriate MD5 value on the site
:
Now that we
have verified that the file was not corrupted or changed, we will install the package
: sudo
- rpm -ivh mysql57-community-release-el7-9.noarch.rpm
This adds two new MySQL yum repositories, and now we can use them to install MySQL server:
- sudo yum install mysql-server
Press y to confirm that you want to continue. Since we just added the package, we will also be asked to accept your GPG key. Press y to download it and complete the installation.
Step 2 — Starting
MySQL We will start the
daemon with
the following command: sudo systemctl start mysqld systemctl
does not show the result of all service management commands, so to make sure we succeeded, we will use the following command
:
- sudo systemctl status mysqld
If MySQL started successfully, the output should contain Active: active and the end line should look similar to the following
:
- Dec 01 19:02:20 centos-512mb-sfo2-02 systemd[1]: MySQL Server started
.
During the installation process, a temporary password is generated for the MySQL root user. Locate
it in mysqld.log with this command:
- sudo grep ‘temporary password’ /var/log/mysqld.log
Output2022-01-24T19:54:46.313728Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: mqRfBU_3Xk>r
Make a note of the password, which you will need in the next step to secure the installation and where you will be forced to change it. The default password policy requires 12 characters, with at least one uppercase letter, one lowercase letter, one number, and one special character.
Step 3 —
MySQL Setup MySQL includes a security script to
change some of the less secure default options for things like remote root logins and sample users
.
Use this command to run the security script
.
- sudo mysql_secure_installation
This will prompt you for the default root password. As soon as you enter it, you will be asked to change it.
OutputThe existing password for the root of the user account has expired. Set a new password. New password:
Enter a new 12-character password that contains at least one uppercase letter, one lowercase letter, one number, and one special character. Re-enter it when prompted.
You will receive feedback on the strength of your new password and then be immediately prompted to change it again. Since you just did, you can confidently say No
: OutputEstimated password strength: 100 Change password for root? (Press y| And for Yes, any other key for No): After
rejecting the prompt to change the password again, we will press Y and then ENTER for all subsequent questions to remove anonymous users, disallow remote root login, delete the test database and access to it, and reload the privilege tables.
Now that we have secured the installation, Let’s try it.
Step 4 — MySQL Test
We can verify our installation and obtain information about it by connecting with the mysqladmin tool, a client that allows you to execute administrative commands. Use the following command to connect to MySQL as root (-u root), request a password (-p), and roll back the version.
- mysqladmin -u root -p version You should
see output similar to the following:
mysqladmin Ver 8.0.28 for Linux on x86_64 (MySQL Community Server – GPL) 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 Localhost connection via UNIX socket UNIX socket /var/lib/mysql/mysql.sock Uptime: 3 min 2 sec Threads: 2 Questions: 14 Slow queries: 0 Apertures: 133 Emptying tables: 3 Open tables: 49 Queries per second Average: 0.076
This indicates that the installation was successful
.
Conclusion
In this tutorial, we have installed and secured MySQL on a CentOS 7 server. To learn more about using MySQL, this guide to learn more about MySQL commands can help. You can also consider implementing some additional security measures.