main logo

How to Install MySQL on CentOS 7

How to Install MySQL on CentOS 7

Note: CentOS 7 reached its End Of Life (EOL) on June 30, 2024. While this guide provides installation instructions, it is strongly recommended to migrate to a supported operating system like Rocky Linux or AlmaLinux for security and ongoing updates. For assistance with migration or managing your existing servers, consider exploring Linux Server Support options.

MySQL is a cornerstone of many web applications and services, valued for its reliability and performance. This tutorial will guide you through installing MySQL Community Server on a CentOS 7 server. We will cover adding the MySQL Yum repository, installing the MySQL packages, securing your installation, and managing the MySQL service.

Before proceeding, ensure you have a CentOS 7 server and a non-root user with sudo privileges.

Prerequisites

  • A CentOS 7 server.
  • A non-root user with sudo privileges.
  • Access to a terminal or command line interface.

If you need to set up a user with sudo privileges, you can follow a guide on initial server setup for CentOS 7.

Step 1 — Adding the MySQL Yum Repository

MySQL provides an official Yum repository that allows you to install the latest versions of MySQL. First, download the repository setup package from the MySQL website. As of this writing, the package for EL7 (Enterprise Linux 7, which CentOS 7 is based on) is mysql80-community-release-el7-x.noarch.rpm. You can find the latest version on the MySQL Yum Repository page.

Download the package using wget:

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

Next, install the downloaded package:

sudo yum localinstall mysql80-community-release-el7-11.noarch.rpm

This command will add the MySQL Yum repository to your system’s repository list and download the GPG key to verify package signatures.

By default, the repository for the latest MySQL series (currently MySQL 8.0) is enabled. If you need to install a different version, such as MySQL 5.7 (which also has an EOL approaching), you would use the yum-config-manager tool to disable the 8.0 repository and enable the 5.7 repository. However, for new installations, using the latest stable version is recommended.

Step 2 — Installing MySQL

Now that the repository is added and enabled, you can install the MySQL server package using yum:

sudo yum install mysql-community-server

This command will install the mysql-community-server package and all its dependencies. Press y and then Enter when prompted to confirm the installation.

Once the installation is complete, start the MySQL service:

sudo systemctl start mysqld

Verify that the MySQL service is running:

sudo systemctl status mysqld

You should see output indicating that the service is active (running).

MySQL also creates a temporary root password during installation. You can find this password in the MySQL error log file:

sudo grep 'temporary password' /var/log/mysqld.log

Make a note of this temporary password, as you will need it in the next step to secure your MySQL installation.

Step 3 — Securing MySQL

MySQL includes a security script, mysql_secure_installation, which helps you perform several important security-related operations, such as setting the root password, removing anonymous users, disallowing remote root login, and removing the test database.

Run the script with sudo:

sudo mysql_secure_installation

You will be prompted to enter the temporary root password you noted earlier.

Next, you’ll be asked if you want to set up the VALIDATE PASSWORD component, which can enforce strong password policies. If you enable it, you’ll choose a password validation policy level (LOW, MEDIUM, or STRONG). It’s generally recommended to enable this for production servers.

Then, you will be prompted to set a new root password. Choose a strong, unique password.

The script will then ask you a series of yes/no questions:

  • Remove anonymous users? (Recommended: Yes)
  • Disallow root login remotely? (Recommended: Yes, unless you specifically need remote root access, which is generally discouraged for security reasons.)
  • Remove test database and access to it? (Recommended: Yes)
  • Reload privilege tables now? (Recommended: Yes, to apply the changes immediately.)

Answering yes to these prompts will significantly improve the security of your MySQL installation. If you require specialized assistance with securing your database or need ongoing MySQL Server Support, professional services can provide tailored solutions.

Step 4 — Testing MySQL

After securing your installation, you can test if you can log in to the MySQL server using the new root password:

mysql -u root -p

Enter the root password you set during the mysql_secure_installation process. If successful, you will be greeted with the MySQL monitor prompt:

mysql>

You can run a simple query to check the version:

SELECT VERSION();

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

Now that MySQL is installed and running, you can manage it using systemctl commands.

To start the MySQL service:

sudo systemctl start mysqld

To stop the MySQL service:

sudo systemctl stop mysqld

To restart the MySQL service:

sudo systemctl restart mysqld

To check the status of the MySQL service:

sudo systemctl status mysqld

To enable MySQL to start automatically at boot:

sudo systemctl enable mysqld

To disable MySQL from starting automatically at boot:

sudo systemctl disable mysqld

Conclusion

You have successfully installed and secured MySQL on your CentOS 7 server. You now have a robust database system ready for your applications. Remember to keep your system and MySQL updated, especially since CentOS 7 has reached its end of life.

For further reading and other related tutorials, you might be interested in:

Managing a database server involves ongoing maintenance, security monitoring, and performance tuning. If your organization requires assistance with these tasks or is looking for comprehensive Managed IT Services, Medha Cloud offers solutions to help you focus on your core business. For Managed Service Providers (MSPs), Medha Cloud also provides white-label managed IT services, enabling you to expand your service portfolio.

Medha Cloud
Medha Cloud
Medha Cloud, a leading multi-cloud service provider, focuses on cloud computing and digital transformation. Every article published under the Medha Cloud brand is a collaborative effort by our team. We are committed to sharing insights, best practices, how-tos, reviews, and real-world examples to help organizations stay ahead of the curve and thrive in the digital age
Share
Contents

Related Articles

medhacloud logo
USA:
Medha Cloud Solutions LLC
30 N Gould St Ste R, Sheridan, WY 82801,
Phone: +1 646 775 2855

India:
Medha Cloud Solutions Private Limited
#74, 7th Cross, Krishna Garden InCity Layout. Chikka Kammanahalli, Banneraghatta Road, Bangalore 560083
Phone:+91 93536 44646

E-Mail: sales@medhahosting.com
©Medha Cloud 2024. All rights reserved.