How to Install MySQL on CentOS 7: Complete Guide Print

  • mysql, centos 7, database installation, lamp stack, mysql server, systemctl, linux database
  • 1

Installing MySQL on CentOS 7 requires adding the MySQL community repository first, then using yum to install the database server. This process takes about 5-10 minutes and gives you a fully functional MySQL installation ready for web applications and database management.

MySQL is one of the most popular open-source database systems used in web hosting environments. It's the "M" in LAMP stack (Linux, Apache, MySQL, PHP/Perl/Python) and powers countless websites and applications. Whether you're running a WordPress site, custom application, or need a robust database solution, MySQL on CentOS 7 provides excellent performance and reliability.

Installing the MySQL Repository

CentOS 7 doesn't include MySQL in its default repositories, so you need to add the official MySQL community repository first. Download and install the MySQL repo package using these commands:

Download the MySQL repository package:

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

Install the repository:

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

This adds the MySQL yum repository to your system, allowing you to install MySQL packages directly.

Installing MySQL Server

Once the repository is configured, install MySQL server and client tools with a single command:

sudo yum install mysql-community-server

The installation process downloads all necessary packages and dependencies. This typically takes a few minutes depending on your connection speed and server specifications.

Configuring MySQL to Start Automatically

After installation, configure MySQL to start automatically when your server boots. This ensures your databases are always available after system restarts.

Enable MySQL on Boot

sudo systemctl enable mysqld.service

You'll see output confirming the service has been enabled, showing symbolic links being created in the systemd configuration.

Disable MySQL on Boot (Optional)

If you need to prevent MySQL from starting automatically, use this command:

sudo systemctl disable mysqld.service

Managing MySQL Service

Use systemctl commands to control the MySQL service on your CentOS 7 server.

Start MySQL

sudo systemctl start mysqld.service

Stop MySQL

sudo systemctl stop mysqld.service

Restart MySQL

sudo systemctl restart mysqld.service

Restarting is useful when you've made configuration changes that require the service to reload.

Check MySQL Status

To verify if MySQL is currently running:

systemctl status mysqld.service

Or for a simple active/inactive response:

systemctl is-active mysqld.service

Initial MySQL Security Setup

After installation, MySQL creates a temporary root password. Find it in the MySQL log file:

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

Run the security script to set your root password and remove insecure defaults:

sudo mysql_secure_installation

This interactive script helps you configure password policies, remove anonymous users, disable remote root login, and remove test databases. Follow the prompts and choose settings appropriate for your security requirements.

Verifying Your Installation

Test your MySQL installation by logging in with the root account:

mysql -u root -p

Enter your root password when prompted. If you see the MySQL command prompt, your installation is successful and ready for creating databases and users.

Common MySQL Installation Issues

Repository Not Found

If the MySQL repository fails to download, verify your server has internet connectivity and the repository URL is current. Repository package versions change periodically, so check the official MySQL downloads page for the latest version.

Port Conflicts

MySQL uses port 3306 by default. If another service is using this port, MySQL won't start properly. Check for port conflicts and adjust your MySQL configuration if needed.

Permission Errors

Most MySQL installation and management commands require root or sudo privileges. Always use sudo when running installation and service management commands.

Frequently Asked Questions

What version of MySQL gets installed on CentOS 7?

By default, the MySQL community repository installs MySQL 8.0, the latest stable version. If you need a specific version like MySQL 5.7, you can configure the repository to use that release channel before installation by editing /etc/yum.repos.d/mysql-community.repo and enabling your preferred version.

Can I install MySQL without adding a repository?

CentOS 7's default repositories include MariaDB (a MySQL fork) instead of MySQL. While you could install MariaDB directly with yum install mariadb-server, installing MySQL specifically requires adding the MySQL community repository as described in this guide.

How do I find my MySQL root password after installation?

MySQL 5.7 and later generate a temporary root password during installation. Find it by running: sudo grep 'temporary password' /var/log/mysqld.log. You must change this password on first login using the mysql_secure_installation script or ALTER USER command.

Why won't MySQL start after installation?

Common causes include port 3306 already in use, insufficient disk space, or permission issues with MySQL data directories. Check the MySQL error log at /var/log/mysqld.log for specific error messages. Verify the service status with systemctl status mysqld.service for diagnostic information.

Need help with your MySQL installation or database configuration? The OBHost support team is available 24/7 to assist you. Visit our contact page or email support@obhost.org for technical assistance.


Was this answer helpful?

« Back