mysql作为常用的关系型数据库,主从复制是其实现高可用和数据冗余的基础架构,但传统手动主从切换流程繁琐,故障发生时响应速度慢,容易影响业务连续性。Orchestrator是一款开源的mysql复制拓扑管理工具,能够自动发现数据库复制拓扑、实时监控节点状态,在主库故障时自动完成主从切换,是快速实现mysql高可用主从切换的优质选择。

环境准备
本次部署采用3台服务器,系统均为CentOS 7,配置如下:
- 服务器1:IP 192.168.0.10,部署mysql 8.0作为主库,同时部署Orchestrator服务
- 服务器2:IP 192.168.0.11,部署mysql 8.0作为从库1
- 服务器3:IP 192.168.0.12,部署mysql 8.0作为从库2
首先需要在所有mysql节点完成基础主从复制配置,确保主从同步正常,同时需要为Orchestrator准备一个有权限管理所有mysql实例的账号,执行以下sql创建账号:
-- 创建Orchestrator管理账号,允许从任意IP访问 CREATE USER 'orchestrator'@'%' IDENTIFIED BY 'orch_password'; -- 授予所有权限,实际生产可根据需求缩小权限范围 GRANT ALL PRIVILEGES ON *.* TO 'orchestrator'@'%'; FLUSH PRIVILEGES;
Orchestrator安装与配置
安装Orchestrator
在192.168.0.10服务器上安装Orchestrator,首先下载对应版本的二进制包,解压后移动到系统路径:
# 下载Orchestrator二进制包 wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-3.2.6-linux-amd64.tar.gz # 解压 tar -zxvf orchestrator-3.2.6-linux-amd64.tar.gz # 移动到系统路径 mv orchestrator /usr/local/ # 添加环境变量 echo 'export PATH=$PATH:/usr/local/orchestrator' >> /etc/profile source /etc/profile
配置Orchestrator
创建Orchestrator配置文件/etc/orchestrator.conf.json,核心配置内容如下:
{
"MySQLTopologyUser": "orchestrator",
"MySQLTopologyPassword": "orch_password",
"MySQLOrchestratorHost": "127.0.0.1",
"MySQLOrchestratorPort": 3306,
"MySQLOrchestratorUser": "orchestrator",
"MySQLOrchestratorPassword": "orch_password",
"MySQLOrchestratorDatabase": "orchestrator",
"DiscoverByShowSlaveHosts": true,
"InstancePollSeconds": 5,
"FailureDetectionPeriodBlockMinutes": 1,
"RecoveryPeriodBlockMinutes": 5,
"AutoFailover": true
}
配置说明:MySQLTopologyUser是连接所有mysql拓扑节点的账号,MySQLOrchestrator相关配置是Orchestrator自身存储元数据的mysql实例信息,AutoFailover设置为true开启自动故障切换,InstancePollSeconds是节点状态检测间隔。
启动Orchestrator并发现拓扑
首先初始化Orchestrator的元数据库,执行以下命令:
# 初始化元数据库 orchestrator -c redeploy -c discover -i 192.168.0.10:3306
启动Orchestrator服务:
# 后台启动Orchestrator nohup orchestrator -config /etc/orchestrator.conf.json http > /var/log/orchestrator.log 2>&1 &
启动后可以通过命令行查看发现的拓扑结构:
# 查看所有发现的实例 orchestrator -c topology -i 192.168.0.10:3306
也可以通过Orchestrator的Web界面查看拓扑,默认访问地址为http://192.168.0.10:3000,界面中会清晰展示主从关系、节点状态等信息。
验证主从自动切换
模拟主库故障,在192.168.0.10服务器上停止mysql服务:
systemctl stop mysqld
等待1分钟左右,Orchestrator会检测到主库故障,自动从两个从库中选择一个数据最新的作为新主库,完成主从切换。可以通过以下命令查看切换后的拓扑:
orchestrator -c topology -i 192.168.0.11:3306
切换完成后,原主库恢复服务时,Orchestrator会自动将其作为新主库的从库加入拓扑,无需手动操作。
注意事项
- 所有mysql实例需要开启
log_bin和gtid_mode,建议开启GTID复制,提升切换的可靠性 - Orchestrator的元数据库建议做高可用,避免Orchestrator自身单点故障
- 自动切换前建议先关闭AutoFailover参数,手动测试切换流程符合预期后再开启自动切换
- 切换阈值和恢复阻塞时间需要根据业务实际容忍度调整,避免误切换
Orchestrator还支持手动切换、拓扑调整、主从复制修复等更多功能,用户可以根据实际需求进一步探索配置。
mysqlOrchestrator主从切换高可用数据库运维修改时间:2026-07-17 00:39:28