1. Check for Existing MySQL Installation
Before installing, check if MySQL is already installed on your system:
rpm -qa | grep mysql
If installed, remove it using:
rpm -e mysql # Standard removal
rpm -e --nodeps mysql # Force removal if dependencies exist
2. Download MySQL Yum Repository
Since MySQL is not included in CentOS 7’s default repositories, download the official Yum repository package:
wget http://repo.mysql.com/mysql-community-release-el7-2.noarch.rpm
Alternative:If wget
is unavailable, manually download the RPM from MySQL’s repositoryand transfer it to your server.
3. Install the Repository and MySQL Server
rpm -ivh mysql-community-release-el7-2.noarch.rpm
yum update
yum install mysql-server
4. Set Permissions and Initialize MySQL
chown -R mysql:mysql /var/lib/mysql/
mysqld --initialize
5. Start MySQL and Check Status
systemctl start mysqld
systemctl status mysqld
6. Retrieve Temporary Root Password
MySQL 8.0+ generates a temporary root password during initialization. Retrieve it with:
grep 'temporary password' /var/log/mysqld.log
7. Run Security Configuration Script
Execute mysql_secure_installation
to set a new root password, remove anonymous users, and disable remote root login. Follow the prompts to enhance security.
8. Log in to MySQL
mysql -u root -p
Enter your new password when prompted.
Troubleshooting Common Issues
Issue 1: Dependency Errors During RPM Installation
Error Example:rpm-lib(PayloadIsZstd) dependency missing
Solution: Ensure you download the correct RPM version for your OS (e.g., CentOS 7 vs. CentOS 10).
Issue 2: DNS/Network Failures During yum update
Symptoms:Unable to resolve mirrorlist.centos.org
.
Solutions:
- Update DNS settings in
/etc/resolv.conf
:
nameserver 8.8.8.8
nameserver 8.8.4.4
Verify network connectivity:
ping www.baidu.com
systemctl status network
Switch to a third-party repository (e.g., Alibaba Cloud for CentOS 7):
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
Temporarily disable the firewall:
systemctl stop firewalld
Issue 3: mysqld.service
Not Found
Error:Failed to start mysqld.service: Unit not found
Solution: Install the MySQL server package explicitly:
yum install -y mysql-community-server
systemctl start mysqld
Summary
This guide provides a reliable method for installing MySQL 8.4.6 on CentOS 7, with solutions to common pitfalls like dependency conflicts, network issues, and service errors. For optimal performance, ensure your environment meets MySQL’s requirements and follow security best practices.