
How to Install MySQL on latest CentOS
Installing MySQL on the latest CentOS distribution is a fundamental skill for anyone managing Linux servers, whether you’re setting up development environments, production databases, or learning database administration. While CentOS has evolved significantly with recent releases, the MySQL installation process has both simplified in some ways and become more nuanced in others, especially with package management changes and security enhancements. This guide will walk you through multiple installation methods, troubleshoot common issues, and share configuration best practices that’ll save you headaches down the road.
Understanding MySQL Installation Options on CentOS
CentOS offers several pathways for MySQL installation, each with distinct advantages. The most common approaches include using the default package manager (dnf/yum), installing from MySQL’s official repository, or compiling from source. The choice often depends on your specific requirements for version control, customization needs, and long-term maintenance preferences.
The default repositories typically provide MariaDB instead of Oracle MySQL, which is a MySQL-compatible fork. While functionally similar for most use cases, there are subtle differences in performance optimization, storage engines, and enterprise features that might influence your decision.
Installation Method | Pros | Cons | Best For |
---|---|---|---|
Default Repository (MariaDB) | Easy installation, automatic updates, CentOS tested | Not Oracle MySQL, limited version options | Development, small to medium applications |
MySQL Official Repository | Latest MySQL versions, Oracle support, full feature set | Additional setup steps, potential conflicts | Production environments, MySQL-specific features |
Source Compilation | Complete customization, optimized builds | Time-intensive, complex dependencies, manual updates | High-performance requirements, custom configurations |
Step-by-Step Installation Guide
Let’s start with the most straightforward approach using MySQL’s official repository, which gives you genuine Oracle MySQL with the latest features and security updates.
First, update your system and install the MySQL repository configuration:
sudo dnf update -y
sudo dnf install wget -y
wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
sudo dnf localinstall mysql80-community-release-el9-1.noarch.rpm -y
Verify the repository was added correctly:
sudo dnf repolist enabled | grep "mysql.*-community.*"
You should see output similar to:
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server
Now install MySQL server:
sudo dnf install mysql-community-server -y
Start and enable the MySQL service:
sudo systemctl start mysqld
sudo systemctl enable mysqld
Check the service status to ensure it’s running properly:
sudo systemctl status mysqld
MySQL 8.0 generates a temporary root password during installation. Retrieve it with:
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 script will prompt you to:
- Change the root password (required)
- Remove anonymous users
- Disable root login remotely
- Remove test database
- Reload privilege tables
Alternative Installation: Using Default CentOS Repository
If you prefer MariaDB (which is functionally equivalent to MySQL for most applications), the installation is even simpler:
sudo dnf install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
Configuration and Initial Setup
After installation, you’ll want to optimize the MySQL configuration for your specific use case. The main configuration file is located at /etc/my.cnf
or /etc/mysql/my.cnf
.
Here’s a basic production-ready configuration example:
[mysqld]
# Basic settings
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/lib/mysql/mysql.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
# Performance tuning
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_flush_method = O_DIRECT
# Connection settings
max_connections = 200
connect_timeout = 60
wait_timeout = 28800
# Security
bind-address = 127.0.0.1
skip-name-resolve
# Logging
log-error = /var/log/mysqld.log
slow-query-log = 1
slow-query-log-file = /var/log/mysql-slow.log
long_query_time = 2
After making configuration changes, restart MySQL:
sudo systemctl restart mysqld
Firewall Configuration
If you need external connections to your MySQL server, configure the firewall:
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
For security, only open the MySQL port if absolutely necessary. Consider using SSH tunneling for remote connections:
ssh -L 3306:localhost:3306 user@your-server-ip
Common Issues and Troubleshooting
Issue 1: Service fails to start
Check the error log for specific issues:
sudo journalctl -u mysqld -f
sudo tail -f /var/log/mysqld.log
Common causes include incorrect permissions on data directory or configuration syntax errors.
Issue 2: Can’t connect to MySQL server
Verify the service is running and check socket permissions:
sudo systemctl status mysqld
ls -la /var/lib/mysql/mysql.sock
netstat -tlnp | grep 3306
Issue 3: Password validation errors
MySQL 8.0 has strict password validation by default. Check the current policy:
mysql -u root -p
SHOW VARIABLES LIKE 'validate_password%';
To temporarily lower requirements (not recommended for production):
SET GLOBAL validate_password.policy=LOW;
SET GLOBAL validate_password.length=6;
Issue 4: Repository conflicts
If you have conflicting repositories, clean up and reinstall:
sudo dnf remove mysql-community-release
sudo dnf clean all
sudo dnf install mysql80-community-release-el9-1.noarch.rpm
Performance Optimization and Best Practices
MySQL performance heavily depends on proper configuration. Here are key areas to focus on:
Memory allocation:
Set innodb_buffer_pool_size
to 70-80% of available RAM for dedicated database servers. For servers with other services, allocate conservatively.
Connection management:
Monitor active connections and adjust max_connections
accordingly. Too many connections can degrade performance.
mysql -u root -p -e "SHOW STATUS LIKE 'Threads_connected';"
mysql -u root -p -e "SHOW STATUS LIKE 'Max_used_connections';"
Query optimization:
Enable the slow query log to identify problematic queries:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
Regular maintenance:
Set up automated backups and table optimization:
#!/bin/bash
# Daily backup script
mysqldump -u root -p'your_password' --all-databases > /backup/mysql_$(date +%Y%m%d).sql
find /backup -name "mysql_*.sql" -mtime +7 -delete
Real-World Use Cases and Integration
Web Application Stack:
MySQL integrates seamlessly with popular web technologies. For a LAMP stack setup:
sudo dnf install httpd php php-mysqli -y
sudo systemctl start httpd
sudo systemctl enable httpd
Development Environment:
Create a dedicated development database and user:
mysql -u root -p
CREATE DATABASE devapp;
CREATE USER 'devuser'@'localhost' IDENTIFIED BY 'SecurePass123!';
GRANT ALL PRIVILEGES ON devapp.* TO 'devuser'@'localhost';
FLUSH PRIVILEGES;
High Availability Setup:
For production environments, consider MySQL replication or clustering. Master-slave replication provides read scaling and backup capabilities.
Security Considerations
Beyond the basic security script, implement these additional measures:
- Regular security updates:
sudo dnf update mysql-community-server
- Use SSL connections for remote access
- Implement proper user privilege management
- Enable binary logging for point-in-time recovery
- Monitor failed login attempts
Consider using MySQL’s built-in audit plugin for compliance requirements:
INSTALL PLUGIN audit_log SONAME 'audit_log.so';
SET GLOBAL audit_log_policy=ALL;
Comparison with Alternative Databases
While MySQL remains popular, consider these alternatives based on your requirements:
Database | Best For | Performance | Complexity |
---|---|---|---|
MySQL | Web applications, e-commerce | Good read/write balance | Medium |
PostgreSQL | Complex queries, data integrity | Excellent for complex operations | Higher |
MariaDB | MySQL replacement, open source | Similar to MySQL | Medium |
SQLite | Embedded applications, prototyping | Fast for small datasets | Low |
For more detailed MySQL documentation, visit the official MySQL documentation and the CentOS Wiki for system-specific guidance.
The installation process might seem straightforward, but proper configuration and maintenance separate functional installations from optimized, secure database servers. Take time to understand your specific requirements and adjust configurations accordingly. Regular monitoring and maintenance will ensure your MySQL installation serves your applications reliably for years to come.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.