Introduction

MySQL is a popular free and open source database management application. It forms part of the LAMP Stack (Linux, Apache, MySQL, PHP), a software collection used for servers.

With the release of CentOS 7, MySQL was dropped from the standard repositories in favor of MariaDB. MariaDB is fully compatible with MySQL and can be substituted almost seamlessly.

This guide will walk you through installing MySQL on CentOS 7 from third-party software repositories.

Prerequisites

Tools/Software

  • A terminal window (Menu > Applications > Utilities > Terminal)
  • A web browser
  • The Yum package manager (included by default)
  • (optional) The wget command (if you don't have it, you can install it from a terminal with this command: sudo yum install wget)

NOTE:
Use Additional MySQL Security Plugins and Libraries

MySQL includes a number of security plugins to authenticate connections to the MySQL server, password verification and securing storage for sensitive data. All of these are available in the free version.

Guide To Installing MySQL on CentOS 7

Step 1: Download the repository packages

Open a browser window, and go to the following address:

https://dev.mysql.com/downloads/repo/yum/

This page will list details on the Yum repository that holds the MySQL files. Scroll down to find the Red Hat Enterprise Linux version that you want to download. (At the time of writing, the website offers Linux 7 and Linux 6 – you’ll probably want Linux 7). You can click the Download button, which takes you to a registration page. You can sign up if you’d like, or look a little lower for the “No thanks, just start my download” link.

Alternately, you can open a terminal and use the wget command to save the file. On the first webpage that lists the release versions, you’ll see a gray subtext that shows something like “(mysql80-community-release-el7-1.noarch.rpm)”. Make note of this, then open a terminal window, then enter the following command:

sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm.noarch.rpm

The system should reach out and download the files. (Make sure you check the website and copy the exact release ID – use that in your Terminal command.) Leave the browser window open for the next step.

Step 2: Add the Software Repositories

The files we just downloaded provide access to the MySQL software repositories.

Before adding them, check the MD5 value to authenticate the software.

Enter the following in the terminal window:

sudo md5sum mysql80-community-release-el7-1.noarch.rpm

The system should respond with a long string of letters and numbers.

Switch back to the MySQL web page and look just below the Download link to find a gray string of numbers labeled MD5.

Compare the MD5 value on the web page to the MD5 value you generated in the terminal window. If they match, proceed to the next step. (If they don’t match, it’s possible that your download was corrupted in transit. Or, it’s possible that the download has been compromised. Repeat Steps 1 and 2 and overwrite the downloaded file. If the MD5 values still don’t match, stop the procedure.)

Generally, the values will match, and we can load the repositories.

In the terminal, enter the following:

sudo rpm –ivh mysql80-community-release-el7-1.noarch.rpm

Make sure you’ve entered the release version from Step 1. This will add 2 new Yum repositories that we can get MySQL from.

Step 3: Install MySQL Database

In the terminal window, enter the following:

sudo yum install mysql-server

The system will ask for confirmation, press Y to confirm. It should also request that you accept a GPG (Gnu Privacy Guard) key. This is another security confirmation because we just added two new software sources. Press Y again, and the system should download and install the software.

Step 4: Managing MySQL Service

You’ll need to start the MySQL service before you can work with it.

In a terminal window, enter the following to start the MySQL service:

sudo systemctl start mysqld

To check the status of the MySQL service with the following Terminal command:

sudo systemctl status mysqld

The system will display several lines of information about the mysqld service. In the Active line, it should display active: (running). You may also see a line below that shows a timestamp of when the service was started.

By default, the MySQL service is set to launch at startup.

To disable it, you can use the disable command:

sudo systemctl disable mysqld

Likewise, if you need to stop the MySQL service, use the stop command:

sudo systemctl stop mysqld

Step 5: Find Temporary Password

The MySQL installation routine sets up a default temporary password.

Use the grep command to find the password by entering the following into a terminal window:

sudo grep ‘temporary password’ /var/log/mysqld.log

Make a note of the password.

Step: 6 Configuring MySQL

Your new MySQL installation comes with a security script to make security configurations easier.

Launch the script with the following terminal command:

sudo mysql_secure_installation

The system will prompt you to enter the default root password. Enter the password you recovered from Step 5.

Next, the system will tell you that password has expired, and prompt you to enter a new one. Enter a new password, make a note of it, then press Enter.

Note: the password should be 12 characters long, and contain at least 1 uppercase, 1 lowercase, 1 number, and 1 unique character.

The system will rate your password strength, and ask if you want to enter a new, stronger password. If you’re satisfied with your password strength, hit the spacebar if you want to revise your password, press Y.

The Secure Installation script will continue, and you can safely reply Y to the rest of the prompts, which include:

  • Remove anonymous users
  • Disallow remote root login
  • Remove test database
  • Reload privilege tables

Step 7: Log in to MySQL

To launch MySQL from the command line, use the following terminal command:

mysql –u root –p

The system will prompt you to enter your password. Enter the password you set in Step 6, and the system should respond by displaying the MySQL shell.

It lists necessary information about the MySQL software, and your command prompt will change to mysql> to indicate that you’re logged into the MySQL software.

Once you’re done, you can log out with:

exit

The command prompt will change, indicating that you’ve logged out of MySQL.

Conclusion

Installing MySQL on CentOS 7 is straightforward.

Once you know where to add the software repositories you should be up and running with your new MySQL installation.