部署MySQL主从+orchestrator+keepalived

下面给你一套 CentOS 8、3节点、二进制 MySQL 8.0 + Orchestrator(Raft集群)+ Keepalived 的完整部署方案,无任何额外机器、全在3台搞定、CentOS 8 完全兼容、可直接上线


一、架构(3台 CentOS 8)

db1(192.168.1.10):MySQL Master + Orchestrator + Keepalived
db2(192.168.1.11):MySQL Slave  + Orchestrator + Keepalived
db3(192.168.1.12):MySQL Slave  + Orchestrator + Keepalived
VIP:192.168.1.100
  • Orchestrator 3台组成 Raft 集群(高可用、无单点)
  • Keepalived 3台部署,VIP 始终漂在当前 MySQL 主库
  • MySQL 主从 GTID 复制,Orchestrator 自动故障切换

二、所有节点初始化(3台都执行)

# 关闭防火墙/SELinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 主机名与hosts
hostnamectl set-hostname db1  # db2/db3对应修改
echo -e "192.168.1.10 db1\n192.168.1.11 db2\n192.168.1.12 db3" >> /etc/hosts

# 免密ssh(Orchestrator依赖)
ssh-keygen -t rsa -N ''
ssh-copy-id root@db1
ssh-copy-id root@db2
ssh-copy-id root@db3

# 依赖
dnf install -y wget tar perl-DBD-MySQL nc

三、二进制安装 MySQL 8.0.25(3台都执行)

cd /usr/local
wget https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
tar xf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.25-linux-glibc2.12-x86_64 mysql

useradd -r -s /sbin/nologin mysql
mkdir -p /data/mysql /data/binlog
chown -R mysql:mysql /data/mysql /data/binlog /usr/local/mysql

/etc/my.cnf(3台,改 server-id)

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
port=3306
user=mysql

server-id=10          # db1=10 db2=11 db3=12
log-bin=/data/binlog/mysql-bin
binlog_format=ROW
gtid_mode=ON
enforce_gtid_consistency=ON
relay_log=relay-bin
log_slave_updates=1
read_only=1
super_read_only=1

初始化与启动

# 初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql

# 服务脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld

# 改密码
grep password /var/log/mysqld.log
/usr/local/mysql/bin/mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';

创建复制/管理账号(3台)

CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'Repl@123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

CREATE USER 'orc'@'%' IDENTIFIED WITH mysql_native_password BY 'Orc@123456';
GRANT ALL ON *.* TO 'orc'@'%';
FLUSH PRIVILEGES;

搭建主从(db2、db3)

CHANGE MASTER TO
MASTER_HOST='db1',
MASTER_USER='repl',
MASTER_PASSWORD='Repl@123456',
MASTER_AUTO_POSITION=1;

START SLAVE;
SHOW SLAVE STATUS\G

四、部署 Orchestrator 3.2.6(3台都装,Raft集群)

1)安装

cd /usr/local
wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-linux-amd64-v3.2.6.tar.gz
tar xf orchestrator-linux-amd64-v3.2.6.tar.gz
mv orchestrator-linux-amd64-v3.2.6 orchestrator
mkdir -p /etc/orchestrator /var/log/orchestrator

2)配置 /etc/orchestrator/orchestrator.conf.json(3台,改 RaftBind)

{
  // 1. 基础运行配置
  "Debug": false,  // 关闭调试模式,生产环境必须为 false
  "ListenAddress": "0.0.0.0:3000",  // Web UI 监听地址,允许所有IP访问

  // 2. 管控 MySQL 集群的账号(需要在所有 MySQL 库上创建)
  "MySQLTopologyUser": "orc",  // 连接 MySQL 的监控账号
  "MySQLTopologyPassword": "Orc@123456",  // 监控账号密码
  "DefaultInstancePort": 3306,  // MySQL 默认端口

  // 3. 【核心】生产防抖 & 探测配置
  "InstancePollSeconds": 2,  // 每2秒检查一次MySQL状态,故障发现快
  "MySQLConnectTimeoutSeconds": 3,  // 连接MySQL超时3秒才判定异常,防止瞬时抖动
  "FailoverMasterLostAfterSeconds": 5,  // 主库连续失联5秒才确认宕机(防抖动)
  "FailoverGracePeriodSeconds": 5,  // 切换前等待5秒,让从库尽量追日志
  "RecoveryPeriodBlockSeconds": 30,  // 切换后30秒内禁止再次切换(冷却时间,默认1小时)

  // 4. 自动切换开关(必须开启)
  "AutoMasterFailover": true,  // 开启【自动故障切换】,主挂了自动切
  "FailoverOnMasterFailure": true,  // 主库故障时执行切换
  "ApplyMySQLPromotionAfterMasterFailover": true,  // 切换后自动关闭新主的只读模式(可写)
  "DiscoverByShowSlaveHosts": true,  // 自动发现主从拓扑,自动添加节点

  // 5. Web 界面登录认证
  "AuthenticationMethod": "basic",  // 开启网页简单认证
  "HTTPAuthUser": "admin",  // Web 登录用户名
  "HTTPAuthPassword": "Admin@123",  // Web 登录密码

  // 6. Raft 集群配置(Orchestrator自身高可用)
  "RaftEnabled": true,  // 开启 Raft 集群模式,解决单点故障
  "RaftDataDir": "/usr/local/orchestrator/raftdata",  // Raft 数据存放目录(自带SQLite,无需外部数据库)
  "RaftBind": "192.168.1.10:10008",  // 【本机IP】每台机器必须改自己的IP
  "DefaultRaftPort": 10008,  // Raft 集群通信端口
  "RaftNodes": [  // 集群所有节点列表(3台都写一样)
    "192.168.1.10:10008",
    "192.168.1.11:10008",
    "192.168.1.12:10008"
  ]
}

3)systemd 服务 /etc/systemd/system/orchestrator.service

[Unit]
Description=Orchestrator
After=network.target mysqld.service

[Service]
ExecStart=/usr/local/orchestrator/orchestrator -config /etc/orchestrator/orchestrator.conf.json
Restart=always
User=root

[Install]
WantedBy=multi-user.target

4)启动

systemctl daemon-reload
systemctl start orchestrator
systemctl enable orchestrator

5)添加集群节点(任意一台执行)

/usr/local/orchestrator/orchestrator -register -instance db1:3306
/usr/local/orchestrator/orchestrator -register -instance db2:3306
/usr/local/orchestrator/orchestrator -register -instance db3:3306

五、Keepalived 配置(3台,VIP 随主库漂移)

1)安装

dnf install -y keepalived
systemctl enable keepalived

2)检测脚本 /etc/keepalived/check_mysql.sh(3台相同)

#!/bin/bash
ORC_USER="orc"
ORC_PASS="Orc@123456"
SOCK="/tmp/mysql.sock"

# 检查MySQL存活
mysql -u$ORC_USER -p$ORC_PASS -S $SOCK -e "select 1" >/dev/null 2>&1
if [ $? -ne 0 ]; then exit 1; fi

# 检查是否是主库(super_read_only=0)
SRO=$(mysql -u$ORC_USER -p$ORC_PASS -S $SOCK -N -e "SELECT @@super_read_only")
if [ "$SRO" = "0" ]; then exit 0; else exit 1; fi
chmod +x /etc/keepalived/check_mysql.sh

3)keepalived.conf(3台,priority 不同)

  • db1(主库):priority 100
  • db2:priority 90
  • db3:priority 80
global_defs {
  router_id db1  # db2/db3对应改
}

vrrp_script chk_mysql {
  script "/etc/keepalived/check_mysql.sh"
  interval 2
  weight -30
}

vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 100  # 90/80
  advert_int 1
  nopreempt
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    192.168.1.100/24
  }
  track_script {
    chk_mysql
  }
}

4)启动

systemctl start keepalived

六、验证

  1. Orchestrator 集群http://192.168.1.10:3000 查看拓扑
  2. VIP 状态ip a 看 VIP 在 db1
  3. 故障切换systemctl stop mysqld(db1),观察 Orchestrator 自动选主、Keepalived VIP 漂移

关键优势

  • CentOS 8 完全兼容(无 MHA 依赖坑)
  • 3 台全搞定(无额外 Manager 机器)
  • Raft 高可用(Orchestrator 无单点)
  • 自动选主 + VIP 漂移(业务无感知)
posted @ 2026-05-09 10:17  wuyingchun1987  阅读(32)  评论(0)    收藏  举报