CentOS 7 上安装 MySQL 8.0 的完整教程
以下是基于 CentOS 7(或 RHEL 7)系统安装 MySQL 8.0 的完整步骤指南。假设你是 root 用户操作,系统是干净的(无旧 MySQL/MariaDB)。如果有问题,提供命令输出排查。参考官方文档:dev.mysql.com。
1. 检查和清理旧残留(避免冲突)
- 检查已安装包:
rpm -qa | grep -i mysql rpm -qa | grep -i mariadb - 移除残留(如 mariadb-libs):
yum remove mariadb-libs mysql* -y - 检查服务和目录:
systemctl status mysqld || systemctl status mysql ps aux | grep mysql ls -la /var/lib/mysql /etc/my.cnf /var/log/mysqld.log - 如果有残留,停止服务并删除(备份数据):
systemctl stop mysqld rm -rf /var/lib/mysql rm -f /etc/my.cnf /var/log/mysqld.log
2. 添加 MySQL YUM 仓库
- 安装最新仓库 RPM(如果已安装旧版,先移除):
yum remove mysql80-community-release* -y yum install https://repo.mysql.com/mysql80-community-release-el7-11.noarch.rpm -y - 验证仓库:
yum repolist | grep mysql - 清理缓存:
yum clean all yum makecache
3. 安装 MySQL Server
- 更新系统(可选):
yum update -y - 安装:
yum install mysql-community-server -y - 如果 YUM 失败,手动下载 RPM(最新版如 8.0.44):
安装(顺序重要):wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-common-8.0.44-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-libs-8.0.44-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-client-8.0.44-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-client-plugins-8.0.44-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-icu-data-files-8.0.44-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql-community-server-8.0.44-1.el7.x86_64.rpm
导入 GPG 密钥(如果提示):rpm -ivh mysql-community-common-*.rpm mysql-community-libs-*.rpm mysql-community-client-plugins-*.rpm mysql-community-icu-data-files-*.rpm mysql-community-client-*.rpm mysql-community-server-*.rpmrpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
4. 启动和配置服务
- 启动服务:
systemctl start mysqld systemctl enable mysqld systemctl status mysqld - 获取临时密码:
grep 'temporary password' /var/log/mysqld.log
5. 修改密码和安全配置
- 用临时密码登录(替换 <temp_password>):
mysql -u root -p<temp_password> - 在 mysql> 提示符下,降低密码策略(如果用弱密码如 ‘123456’):
SET GLOBAL validate_password.policy = LOW; SET GLOBAL validate_password.length = 4; ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; FLUSH PRIVILEGES; exit; - 非交互方式(shell 命令):
mysql -u root -p$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}') -e "SET GLOBAL validate_password.policy = LOW; SET GLOBAL validate_password.length = 4;" mysql -u root -p$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}') -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;"
6. 配置远程访问(外网访问)
- 编辑配置文件(用 sed 创建/修改):
echo '' | sed 'i\[mysqld]\nbind-address=0.0.0.0\nvalidate_password.policy=LOW\nvalidate_password.length=4' > /etc/my.cnf - 重启服务:
systemctl restart mysqld - 授予远程权限:
mysql -u root -p123456 -e "CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;"
7. 配置防火墙
- 启用防火墙(如果未启):
systemctl start firewalld systemctl enable firewalld - 放行 3306 端口:
firewall-cmd --permanent --add-port=3306/tcp firewall-cmd --reload
8. 安全加固(等效 mysql_secure_installation,非交互)
mysql -u root -p123456 -e "DELETE FROM mysql.user WHERE User=''; DROP DATABASE IF EXISTS test; DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'; FLUSH PRIVILEGES;"
9. 测试安装
- 本地登录:
mysql -u root -p123456 - 远程测试(从另一台机):
mysql -u root -p123456 -h <服务器IP> - 检查版本:
mysql -V
注意:弱密码 ‘123456’ 不安全,生产环境用强密码。云服务器需在控制台安全组放行 3306 端口。如果安装失败,检查网络、依赖(如 perl-DBI),或用 MariaDB 替代。