MySQL installation on CentOS differs depending on the version. CentOS 7 doesn't include MySQL in default repositories, requiring manual repository setup. CentOS 8 and newer include MySQL directly, simplifying the process. This guide covers both scenarios plus essential security configuration.
For resellers managing cPanel servers or standalone Linux deployments, MySQL installation is foundational knowledge.
At a glance
- CentOS 7 requires adding the MySQL Yum repository before installation
- CentOS 8+ includes MySQL in default repositories for simpler installation
- MySQL installation commands use yum package manager
- Post-installation security hardening via mysql_secure_installation is essential
- Database optimization and repair uses mysqlcheck utility
Installing MySQL on CentOS 7
CentOS 7 doesn't ship with MySQL—only MariaDB (MySQL's open-source fork). To install MySQL, add the official repository first.
Download the repository package:
Wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
Install the package:
Rpm -ivh mysql80-community-release-el7-5.noarch.rpm
Now install MySQL server:
Yum install mysql-server
Start the MySQL daemon:
Systemctl start mysqld
Verify it's running:
Systemctl status mysqld
The service should respond showing active status.
Installing MySQL on CentOS 8+
CentOS 8 simplifies things. Update packages first:
Yum update
Install MySQL directly:
Yum install mysql-server
Start and verify:
Systemctl start mysqld
Systemctl status mysqld
The process is notably shorter—no repository configuration needed.
Securing MySQL
After installation, run the security script:
Mysql_secure_installation
This interactive tool prompts for:
- Setting the root password
- Removing anonymous users
- Disabling remote root login
- Removing test databases
- Reloading privilege tables
Running this is essential for any production server. The defaults leave security gaps.
Optimizing and Repairing Databases
Over time, databases develop inefficiencies. Fragmentation slows queries. Corrupted tables cause errors. The mysqlcheck utility addresses both:
Mysqlcheck -A –auto-repair -u root -p
This repairs and optimizes all databases. Schedule it during low-traffic periods. Some hosting control panels automate this via cron jobs.
Our analysis
MySQL installation is straightforward for anyone comfortable with command-line basics. The complexity comes after—in performance tuning, user management, and backup automation.
For resellers, learn these skills:
- User creation with appropriate privileges (never give apps root access)
- Database backup automation (mysqldump or alternatives)
- Log analysis for slow queries
- Configuration tuning for available RAM
These skills separate competent server managers from those who click through control panels without understanding what's happening.
Practical Example
A client reports their WordPress site suddenly can't connect to the database. Errors appear: "Error establishing database connection." You investigate:
SSH into the server. Check MySQL status: systemctl status mysqld. The service is stopped—crash or failed resource.
Start MySQL: systemctl start mysqld. The site reconnects immediately.
But why did it stop? Check logs: journalctl -u mysqld. Memory exhaustion—the VPS ran out of RAM during a traffic spike. MySQL got killed by the OOM killer.
You add swap space, tune MySQL's memory usage in /etc/my.cnf, and configure monit to restart MySQL automatically if it fails again. The root cause is addressed, not just the symptom.
What to watch next
Watch for CentOS Stream developments—it's becoming the RHEL upstream and may replace traditional CentOS. Also watch for MariaDB gaining features that narrow MySQL's advantages, potentially making the fork more attractive for new deployments.
Frequently asked questions
Why doesn't CentOS 7 include MySQL by default?
CentOS 7 ships with MariaDB, a MySQL fork, due to licensing considerations. Oracle's MySQL uses GPL licensing while MariaDB uses BSD. Installing MySQL requires adding Oracle's official repository.
What is mysql_secure_installation?
It's a security script that prompts for essential MySQL hardening steps: setting root password, removing anonymous users, disabling remote root access, and removing test databases. Always run on new installations.
How do I back up MySQL databases?
Use mysqldump for logical backups: mysqldump -u root -p –all-databases > backup.sql. For production, use automated tools like XtraBackup for hot backups or configure control panel backup systems.
How do I optimize MySQL tables?
Use mysqlcheck -o (optimize) or -A –auto-repair for all databases. Alternatively, run OPTIMIZE TABLE SQL command on specific tables through phpMyAdmin or command line.
What should I check if MySQL won't start?
Check service status with systemctl status mysqld, review logs with journalctl -u mysqld, verify port 3306 isn't blocked, confirm enough free RAM exists, and check configuration files for syntax errors.