Linux, Web Hosting, and Everything Else in Between
Linux, Web Hosting, and Everything Else in Between

How to Install MySQL on CentOS (7, 8)

How to Install MySQL on CentOS (7, 8)

In this tutorial, we’re going to show you how to install MySQL on CentOS. Step-by-step instructions on how to configure and install MySQL.

Alternative recommended read: How to Install MySQL on Ubuntu.

Prerequisites

Before we begin, this is what you’ll need:

  • A CentOS server. You can get one from Linode or Vultr. If you want a managed provider, get a server from SolaDrive. They will install MySQL for you.
  • Root access/sudo privileges. All commands in this tutorial were executed with the root user
  • By default, in CentOS 7, MySQL isn’t included in the repos at all. In CentOS 8 (and newer releases) it is. We’ll include instructions for both use cases. MariaDB is included in all versions of CentOS.

How to Install MySQL on CentOS 7

Since MySQL isn’t included in the CentOS 7 repositories by default, we’ll have to add it to the repos first, and then install MySQL.

Step 1: Update the system

Update the repositories/system by running the following command:

yum update

Step 2: Download and add the repo

Next, we need to download the MySQL repo. You can get the download link from this page.

Once you get the download link, use wget to download the file:

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

And now you can install the downloaded package:

rpm -ivh mysql80-community-release-el7-5.noarch.rpm

Step 3: Install MySQL

And finally, you can install MySQL by running the following command:

yum install mysql-server

You can now start and check the status of the MySQL daemon with the following commands:

systemctl start mysqld
systemctl status mysqld

And that’s it. MySQL is installed on your CentOS 7 server. The next steps are to secure and configure MySQL. The instructions for that are below.

How to install MySQL on CentOS 8 (and later releases)

Starting with CentOS 8, MySQL is included by default in the repositories. So the installation process is somewhat easier.

Step 1: Update the system

Run the following command to update CentOS:

yum update

Step 2: Install MySQL

And install MySQL with the following command:

yum install mysql-server

Start and check the status of the MySQL daemon with the following commands:

systemctl start mysqld
systemctl status mysqld

How to Optimize and Secure MySQL on CentOS

This is by no means an extensive tutorial on how to secure MySQL. There are a lot more configurations and settings you should do to properly secure MySQL on CentOS, as well as secure the server itself.

But, a quick and easy way to get a more secure MySQL server is to run the security script, which will guide you through easy-to-follow prompts.

Run the script with the following command:

mysql_secure_installation

And follow the self-explanatory prompts.

You can also optimize MySQL with MySQLTuner.

To repair and optimize all your databases at once, run the following command:

mysqlcheck -A --auto-repair -u root -p

And that’s all. You’ve now installed and configured MySQL on CentOS.

Leave a comment

Your email address will not be published. Required fields are marked *