说明

  • Orchestrator 可以实现 MySQL 高可用在主节点故障时自动切主。
  • 测试机器
    • 192.168.212.11(openEuler01):部署 MySQL 单个节点
    • 192.168.212.12(openEuler02):部署 MySQL 单个节点
    • 192.168.212.14(openEuler04):部署 orchestrator 和 orchestrator 的元数据库(MySQL)

部署

各节点配置 hosts

1
2
3
4
5
6
vi /etc/hosts
#################################
192.168.212.11 openEuler01
192.168.212.12 openEuler02
192.168.212.14 openEuler04
#################################

数据库部署主从

openEuler01/openEuler02分别部署 MySQL

配置异步主从复制

  • 所有节点配置主从延迟时间为一个较短的时间
1
set @@global.slave_net_timeout = 4;
  • 所有节点同步用户
1
GRANT SELECT, RELOAD, PROCESS, SHOW DATABASES, LOCK TABLES, REPLICATION CLIENT,REPLICATION SLAVE, EVENT ON *.* TO 'sync_user'@'%' identified by 'slaveBkPass';
  • 所有节点重置 binlog 位置
1
reset master;
  • openEuler02 执行 sql,连接主库
1
2
3
4
5
6
7
change master to MASTER_HOST='openEuler01', -- 设置主库 IP
  MASTER_PORT=3306,							-- 设置主库端口
  MASTER_USER='sync_user',					-- 配置同步用户
  MASTER_PASSWORD='slaveBkPass',    		-- 配置同步密码
  master_auto_position=1,					-- 开启自动GTID同步
  MASTER_CONNECT_RETRY=1,					-- 配置副本每1秒尝试重新连接一次
  MASTER_RETRY_COUNT=86400;					-- 配置累计重试次数
  • openEuler02 从库设置只读
1
2
set @@global.read_only=1;
set @@global.super_read_only=1;
  • 开启同步
1
start slave;
  • 查看主从状态
1
2
3
4
5
show slave status \G
-------------------------------------
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
-------------------------------------

部署 orchestrator

部署 orch 元数据库

  • 部署 MySQL:略
  • 元数据库创建库,配置权限
1
2
3
CREATE DATABASE IF NOT EXISTS `orchestrator` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
CREATE USER 'orc_server_user'@'%' IDENTIFIED BY 'orch_server_password';
GRANT ALL PRIVILEGES ON `orchestrator`.* TO 'orc_server_user'@'%';

部署 orch

  • 下载
1
2
3
4
5
mkdir -p /data/tools
cd /data/tools
wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-3.2.6-linux-amd64.tar.gz
mkdir -p orchestrator
tar -zxvf orchestrator-3.2.6-linux-amd64.tar.gz -C orchestrator
  • 更改配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
cd orchestrator/usr/local/orchestrator/
cp -rp orchestrator-sample.conf.json  orchestrator.conf.json
vi orchestrator.conf.json
#####################################
# 更改元数据相关配置
  "MySQLOrchestratorHost": "127.0.0.1",
  "MySQLOrchestratorPort": 3306,
  "MySQLOrchestratorDatabase": "orchestrator",
  "MySQLOrchestratorUser": "orc_server_user",
  "MySQLOrchestratorPassword": "orch_server_password",
# 如果集群不满足 RecoverMasterClusterFilters 和 RecoverIntermediateMasterClusterFilters
# 集群主节点故障将不会进行自动故障切换
  "RecoverMasterClusterFilters": ["*"], 
  "RecoverIntermediateMasterClusterFilters": ["*"], 
#####################################

orchestrator 管理 MySQL

  • MySQL 主节点创建 orch 用户(openEuler01)
1
2
3
4
CREATE USER 'orchestrator'@'%' IDENTIFIED BY 'orc_password';
GRANT SUPER, PROCESS, REPLICATION SLAVE, RELOAD ON *.* TO 'orchestrator'@'%';
GRANT SELECT ON mysql.slave_master_info TO 'orchestrator'@'%';
-- GRANT SELECT ON ndbinfo.processes TO 'orchestrator'@'%'; -- Only for NDB Cluster
  • orch 配置数据库连接信息
1
2
3
4
5
6
vi orchestrator.conf.json
#####################################
# 配置被管理库的用户、密码
  "MySQLTopologyUser": "orchestrator",
  "MySQLTopologyPassword": "orc_password",
#####################################
  • 配置 orch 同步管理 super_read_only
1
2
3
4
vi orchestrator.conf.json
#####################################
  "UseSuperReadOnly": true,
#####################################
  • 启动 orch
1
./orchestrator  --config=orchestrator.conf.json http
  • 发现节点
1
2
3
4
cd resources/bin
$ ./orchestrator-client -c discover -i openEuler01:3306
# 如果报错:orchestrator-client[3597]: cannot find jq,需安装 jq 包
# yum -y install jq
  • 查看当前架构
1
2
3
$ ./orchestrator-client -c topology -i openEuler01:3306
openEuler01:3306   [0s,ok,5.7.44-log,rw,ROW,>>,GTID]
+ openEuler02:3306 [0s,ok,5.7.44-log,ro,ROW,>>,GTID]

验证

手动触发主从切换

  • 手动触发 openEuler01 集群的主从切换,自动选主
1
./orchestrator-client  -c graceful-master-takeover-auto -i openEuler01
  • 查看集群状态
    • openEuler01 变成了维护状态,需手动 start slave 才可重新加入集群
1
2
3
./orchestrator-client -c topology -i openEuler01:3306
openEuler02:3306   [0s,ok,5.7.44-log,rw,ROW,>>,GTID]
+ openEuler01:3306 [0s,ok,5.7.44-log,ro,ROW,>>,GTID,downtimed]
  • openEuler01 开启主从同步
1
start slave
  • 查看架构状态
1
2
3
./orchestrator-client -c topology -i openEuler01:3306
openEuler02:3306   [0s,ok,5.7.44-log,rw,ROW,>>,GTID]
+ openEuler01:3306 [0s,ok,5.7.44-log,ro,ROW,>>,GTID]
  • 验证 openEuler01/openEuler02 只读状态
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
-- openEuler01
mysql> select @@global.read_only \G
@@global.read_only: 0
mysql> select @@global.super_read_only \G
@@global.super_read_only: 0

-- openEuler02
mysql> select @@global.read_only \G
@@global.read_only: 1
mysql> select @@global.super_read_only \G
@@global.super_read_only: 1

手动模拟主节点故障

  • 关停当前主节点(openEuler02)
  • 查看集群状态
1
2
3
4
$ ./orchestrator-client -c topology -i openEuler01
openEuler01:3306 [unknown,invalid,5.7.44-log,rw,ROW,>>,GTID,downtimed]
$ ./orchestrator-client -c topology -i openEuler02
openEuler02:3306 [0s,ok,5.7.44-log,rw,ROW,>>,GTID]
  • 补充说明
    • 如果主节点恢复后,要重新加入集群,需手动执行 change master to…… 与新选出来的主节点建立主从关系