mysql主从切换

发布时间 2023-12-15 14:50:24作者: 紫系流月

需求: 停止 db03机器上的主库,将 db05 从节点作为主库,将其他从库的主库指向 db05

前置准备,打通业务机器和 db05 的防火墙,保证db03 db05 mysql实例上的账户信息一致

1、让业务停止写入 db03 上的mysql主库

2、登录mysql db03 查看位点

show master status;

+------------------+-----------+--------------+------------------+-------------------------------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+-----------+--------------+------------------+-------------------------------------------+
| mysql_bin.003295 | 108167999 |              |                  | 23edd83c-988f-11ee-a990-9c52f8ecc7c5:1-10 |
+------------------+-----------+--------------+------------------+-------------------------------------------+

如果5-10分钟一直不变就是没有写入;

show processlist;
查看是否还有服务连接;

3、检查没有问题,将 db03 老主库设置为只读库。

## 在 db03 上有个mysql 的主库登录执行 设置为只读:
set global super_read_only = on;
set global read_only = on;
  1. 在新主库上 db05 执行:
SHOW VARIABLES LIKE '%read%';
## 如果 super_read_only  read_only 为 OFF 因为业务需要写数据到主库
set global super_read_only = on;
set global read_only = on;

show slave status\G;

stop slave;
reset slave all;

## 记录下 File Position
show master status;
  1. 把服务的配置改到新的主库 db05 上。

  2. 将其他从库指向新主库

stop slave;
reset slave all;

set global super_read_only = on;
set global read_only = on;

CHANGE MASTER TO
    MASTER_HOST='db05地址',
    MASTER_USER='user',
    MASTER_PASSWORD='passwd',
    MASTER_LOG_FILE='db05 file',
    MASTER_LOG_POS=db05 Position;

start slave;