How to Install and Configure MySQL on CentOS: A Complete Guide

MySQL is one of the most widely used open-source relational database management systems (RDBMS). It powers many web applications and enterprise solutions due to its performance, reliability, and scalability. In this guide, we will walk you through the step-by-step process of installing and configuring MySQL on CentOS.

Whether you are using CentOS 7, 8, or Stream 9, this guide ensures a smooth MySQL installation with all necessary configurations for optimal performance and security.

Step 1: Update Your CentOS System

Before installing MySQL, it is essential to update your system to ensure all packages are current and dependencies are resolved.

sudo yum update -y

This command refreshes all system packages and prepares your CentOS system for MySQL installation.

Step 2: Add the MySQL Repository

CentOS does not include MySQL in its default repositories; instead, it ships with MariaDB. If you want the official MySQL version, you need to add the MySQL Yum repository.

For CentOS 7:

sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

For CentOS 8 and CentOS Stream 9:

sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpm

This command adds MySQL’s official repository to your CentOS system.

Step 3: Install MySQL Server

Once the repository is added, install MySQL using:

sudo yum install -y mysql-community-server

During the installation, the necessary dependencies will also be installed.

Step 4: Start and Enable MySQL Service

After installation, start the MySQL service and enable it to launch at boot time:

sudo systemctl start mysqld
sudo systemctl enable mysqld

Verify that MySQL is running:

sudo systemctl status mysqld

If MySQL is active, you should see an output indicating the service is running.

Step 5: Secure the MySQL Installation

After installation, MySQL generates a temporary root password. You must retrieve and reset this password before proceeding.

5.1 Find the Temporary Root Password

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

Copy the generated password for later use.

5.2 Run the MySQL Secure Installation Script

sudo mysql_secure_installation

This interactive script will ask several security-related questions:

  • Enter the temporary root password.
  • Set a new, strong root password.
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database? Y
  • Reload privilege tables? Y

After completing these steps, MySQL will be more secure and ready for use.

Step 6: Log into MySQL

To access the MySQL shell, use:

mysql -u root -p

Enter the root password when prompted.

Step 7: Create a Database and User

Once inside MySQL, you can create a database and a user with privileges:

CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace mydatabase, myuser, and mypassword with your preferred values.

Step 8: Enable Remote Access (Optional)

If you need to allow remote connections to MySQL:

8.1 Modify the MySQL Configuration File

sudo vi /etc/my.cnf

Find the line:

bind-address = 127.0.0.1

Change it to:

bind-address = 0.0.0.0

Save and exit the file.

8.2 Restart MySQL Service

sudo systemctl restart mysqld

8.3 Open MySQL Port in Firewall

sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --reload

Now, MySQL is accessible from remote hosts.

Step 9: Verify MySQL Installation

Check the MySQL version to confirm the installation:

mysqladmin -u root -p version

If you see MySQL version details, the installation is successful!

Step 10: Enable MySQL to Start on Boot

Ensure MySQL starts automatically when the system reboots:

sudo systemctl enable mysqld

Troubleshooting MySQL Installation Issues

GPG Key Check Failed?

If you encounter a GPG check failure, import the correct MySQL GPG key:

sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023

Then, retry installing MySQL.

MySQL Service Fails to Start?

If MySQL fails to start, check the logs:

sudo journalctl -xeu mysqld

To fix common issues, ensure:

  • No other service is using port 3306.
  • Sufficient disk space is available.
  • The /var/lib/mysql directory has proper permissions.

Conclusion

By following this guide, you have successfully installed and configured MySQL on CentOS. Your database server is now ready for production use, whether for web applications, enterprise systems, or personal projects.

To further optimize MySQL performance, consider tuning configuration parameters in /etc/my.cnf, setting up backups, and monitoring database health.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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