MySQL 5.7 多实例部署完整指南(基于二进制包)
MySQL 5.7 多实例部署完整指南(基于二进制包)
MySQL 5.7 多实例部署 的详细流程、关键说明及注意事项,确保操作逻辑清晰、可复现,同时解决潜在问题
一、部署前准备
1. 环境说明
- 操作系统:CentOS 7(或 RHEL 7,其他 Linux 发行版步骤类似,依赖安装命令需调整)
- MySQL 版本:5.7.22(二进制包,无需编译,部署效率高)
- 多实例规划:3 个实例,端口分别为
3306
、3307
、3308
(需确保端口未被占用)
2. 依赖检查与安装
配置yum仓库:(安装epel源)
[root@syf ~]# cd /etc/yum.repos.d/
[root@syf yum.repos.d]# ls
[root@syf yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2025-09-19 11:11:08-- https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 171.43.201.240, 111.124.193.164, 111.124.193.163
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|171.43.201.240|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’100%[=================================>] 2,523 --.-K/s in 0s 2025-09-19 11:11:08 (587 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523][root@syf yum.repos.d]# yum -y install epel-release
.....
二、详细部署步骤
步骤 1:上传 MySQL 二进制包
[root@syf ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@syf ~]# rz -E
rz waiting to receive.
[root@syf ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg Public
Desktop Music Templates
Documents mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz Videos
Downloads Pictures
安装依赖包:
[root@syf ~]# yum -y install libncurses*
....
步骤 2:创建 MySQL 用户与组(安全最佳实践)
为避免使用 root
运行 MySQL(存在安全风险),创建独立的 mysql
用户组和用户:
# 创建 mysql 组(-r 表示系统组,避免与普通用户组冲突)
[root@syf ~]# groupadd -r mysql
# 创建 mysql 用户(-M 不创建家目录,-s /sbin/nologin 禁止登录,-g 指定所属组)
[root@syf ~]# useradd -M -s /sbin/nologin -g mysql mysql
[root@syf ~]# id mysql
uid=1001(mysql) gid=982(mysql) groups=982(mysql)
步骤 3:解压并配置 MySQL 目录
3.1 解压二进制包至指定路径
# 解压到 /usr/local/(Linux 常用软件安装路径)
[root@syf ~]# tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# 创建软链接(简化路径,后续操作无需输入长版本号)
[root@syf local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@syf local]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
lrwxrwxrwx 1 root root 36 Sep 19 16:54 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 Sep 19 16:53 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Jul 23 11:29 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
3.2 修改目录权限
确保 mysql
用户拥有安装目录的权限,避免后续初始化报错:
[root@syf local]# chown -R mysql.mysql mysql
[root@syf local]# ls -ld mysql
lrwxrwxrwx 1 mysql mysql 36 Sep 19 16:54 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
[root@syf local]# chown -R mysql.mysql mysql/
[root@syf local]# ls -l mysql/
total 272
drwxr-xr-x 2 mysql mysql 4096 Sep 19 16:53 bin
drwxr-xr-x 2 mysql mysql 55 Sep 19 16:53 docs
drwxr-xr-x 3 mysql mysql 4096 Sep 19 16:52 include
drwxr-xr-x 5 mysql mysql 230 Sep 19 16:53 lib
-rw-r--r-- 1 mysql mysql 259253 Nov 30 2021 LICENSE
drwxr-xr-x 4 mysql mysql 30 Sep 19 16:53 man
-rw-r--r-- 1 mysql mysql 566 Nov 30 2021 README
drwxr-xr-x 28 mysql mysql 4096 Sep 19 16:53 share
drwxr-xr-x 2 mysql mysql 90 Sep 19 16:53 support-files
步骤 4:配置 MySQL 环境变量
[root@syf ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@syf ~]# source /etc/profile.d/mysql.sh
[root@syf ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
步骤 5:创建多实例数据目录
每个实例需独立的数据目录,避免数据冲突,按端口区分:
[root@syf ~]# cd /usr/local/
[root@syf local]# mkdir -p /opt/data/{3306..3308}
[root@syf local]# cd /opt/data/
[root@syf data]# ls
3306 3307 3308
[root@syf data]# chown -R mysql.mysql /opt/data/3306/
[root@syf data]# chown -R mysql.mysql /opt/data/3307/
[root@syf data]# chown -R mysql.mysql /opt/data/3308/
[root@syf data]# ls -ld /opt/data/
drwxr-xr-x 5 root root 42 Sep 21 19:12 /opt/data/
[root@syf data]# chown -R mysql.mysql /opt/data/
步骤 6:初始化多实例(核心步骤)
MySQL 5.7 需通过 mysqld --initialize
初始化实例,每个实例单独执行,生成临时密码(必须保存)
6.1 初始化 3306、3307、3308 实例
[root@syf data]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
.....
root@localhost: sado6BMzi+z%
[root@syf ~]# echo "sado6BMzi+z%" > 3306
[root@syf ~]# cat 3306
sado6BMzi+z%
[root@syf ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
.....
root@localhost: l=cj?cyl/7bY
[root@syf ~]# echo "l=cj?cyl/7bY" > 3307
[root@syf ~]# cat 3307
l=cj?cyl/7bY
[root@syf ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
......
root@localhost: gu%Gja&aQ6K0
[root@syf ~]# echo "gu%Gja&aQ6K0" > 3308
[root@syf ~]# cat 3308
gu%Gja&aQ6K0
- 注意:每个实例的临时密码不同,必须单独保存,后续重置密码需使用
步骤 7:配置多实例主配置文件(/etc/my.cnf)
依赖检查与安装
[root@syf ~]# yum -y install perl
编辑 /etc/my.cnf
:
[root@syf ~]# vim /etc/my.cnf
写入以下内容:
# mysqld_multi 工具配置(指定 mysqld_safe 和 mysqladmin 路径)
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin# 3306 实例配置([mysqld+端口] 为固定格式)
[mysqld3306]
datadir = /opt/data/3306 # 数据目录(与初始化一致)
port = 3306 # 端口(唯一)
socket = /tmp/mysql3306.sock # 本地通信socket(唯一,避免冲突)
pid-file = /opt/data/3306/mysql_3306.pid # 进程ID文件(唯一)
log-error = /var/log/3306.log # 错误日志(便于排查问题)# 3307 实例配置
[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error = /var/log/3307.log# 3308 实例配置
[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error = /var/log/3308.log
步骤 8:启动多实例
通过 mysqld_multi
工具启动实例,支持单独启动或批量启动:
# 单独启动 3306 实例
[root@syf ~]# mysqld_multi start 3306
# 单独启动 3307 实例
[root@syf ~]# mysqld_multi start 3307
# 单独启动 3308 实例
[root@syf ~]# mysqld_multi start 3308
验证启动结果
查看端口是否监听(确认实例已启动):
[root@syf ~]# ss -anlt
LISTEN 0 80 :::3306 :::*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*
步骤 9:重置多实例 root 密码(必须操作)
MySQL 5.7 初始化后,root 密码为临时密码,首次登录必须重置,否则无法执行其他操作。
9.1 重置 3306 实例密码(交互方式)
[root@syf ~]# cat 3306
sado6BMzi+z%
# 通过 socket 连接 3306 实例(-S 指定 socket 文件,-p 后接临时密码)
[root@syf ~]# mysql -uroot -p'sado6BMzi+z%' -S /tmp/mysql3306.sock
登录后执行密码重置:
# MySQL 5.7 重置密码命令(将 'redhat' 改为自定义密码)
mysql> set password=password('redhat');
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> exit
Bye
[root@syf ~]# mysql -uroot -predhat -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> exit
Bye
9.2 重置 3307、3308 实例密码(非交互方式)
通过 -e
参数直接执行重置命令,无需进入交互界面:
[root@syf ~]# cat 3307
l=cj?cyl/7bY
# 重置 3307 实例密码(--connect-expired-password 允许使用临时密码登录)
[root@syf ~]# mysql -uroot -p'l=cj?cyl/7bY' -S /tmp/mysql3307.sock -e 'set password=password("redhat");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@syf ~]# mysql -uroot -predhat -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
[root@syf ~]# cat 3308
gu%Gja&aQ6K0
# 重置 3308 实例密码
[root@syf ~]# mysql -uroot -p'gu%Gja&aQ6K0' -S /tmp/mysql3308.sock -e 'set password=password("redhat");' --connect-expired-password
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@syf ~]# mysql -uroot -predhat -S /tmp/mysql3308.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
- 注意:若临时密码包含特殊字符(如
+
、:
),需用单引号'
包裹,避免被 Shell 解析
常见问题排查
- 实例启动失败:查看对应实例的错误日志(如
/var/log/3306.log
),常见原因:- 数据目录权限不足(确保
mysql:mysql
所有) - 端口被占用(用
ss -antl | grep 3306
检查,杀死占用进程) - 临时密码丢失(删除数据目录重新初始化:
rm -rf /opt/data/3306/*
,再执行mysqld --initialize
)
- 数据目录权限不足(确保
- 密码重置报错 “Your password has expired”:登录时添加
--connect-expired-password
参数(如 3307 实例的非交互方式) - 本地连接实例报错 “Can’t connect to local MySQL server through socket”:确认
socket
文件路径与/etc/my.cnf
中配置一致,且实例已启动
多实例部署核心要点
- 资源隔离:每个实例必须使用独立的 端口、数据目录、socket、pid-file、日志文件,避免冲突
- 权限安全:始终使用
mysql
用户运行实例,禁止root
直接运行 - 密码管理:初始化时的临时密码必须保存,重置后的密码建议统一管理(如使用密码文件)