美文网首页Mysql
九,主从复制高级进阶

九,主从复制高级进阶

作者: 会倒立的香飘飘 | 来源:发表于2021-05-30 14:02 被阅读0次

1,延时从库

介绍

是我们认为配置的一种特殊从库.人为配置从库和主库延时N小时.
SQL线程延时:数据已经写入relaylog中了,SQL线程"慢点"运行
一般企业建议3-6小时,具体看公司运维人员对于故障的反应时间

配置

mysql>stop slave;
mysql>CHANGE MASTER TO MASTER_DELAY = 300;
mysql>start slave;
mysql> show slave status \G
SQL_Delay: 300                    延时从库的设置时间
SQL_Remaining_Delay: NULL         延时从库剩余多少时间同步       

延时从库处理逻辑故障

恢复思路:
1,监控到数据库逻辑故障
2,停从库SQL线程,记录已经回放的位置点(截取日志起点)
    stop slave sql_thread ;
    show slave status \G
    Relay_Log_File: db01-relay-bin.000002
    Relay_Log_Pos: 320
3, 截取relaylog
    起点:     
    show slave status \G
    Relay_Log_File ,Relay_Log_Pos
    终点: drop之前的位置点
    show relaylog events in ''  
    进行截取
4,模拟SQL线程回访日志
    从库  source 
5,恢复业务
    情况一: 就一个库的话
    从库替代主库工作
    情况二: 
    从库导出故障库,还原到主库中
故障演练

主库:

create database delay charset utf8mb4;
use delay;
create table t1 (id int);
insert into t1 values(1),(2),(3);
commit;
drop database delay;

从库

停止从库sql线程
mysql> stop slave sql_thread;

获取从库relaylog的位置点
mysql> show slave status\G
Relay_Log_File: mister_f-relay-bin.000002
Relay_Log_Pos: 501

获取relaylog截取终点
mysql> show relaylog events in 'mister_f-relay-bin.000002';
+---------------------------+------+----------------+-----------+-------------+----------------------------------------+
| Log_name                  | Pos  | Event_type     | Server_id | End_log_pos | Info                                   |
+---------------------------+------+----------------+-----------+-------------+----------------------------------------+
| mister_f-relay-bin.000002 |    4 | Format_desc    |         8 |         123 | Server ver: 5.7.26-log, Binlog ver: 4  |
| mister_f-relay-bin.000002 |  123 | Previous_gtids |         8 |         154 |                                        |
| mister_f-relay-bin.000002 |  154 | Rotate         |         7 |           0 | mysql-bin.000004;pos=1790              |
| mister_f-relay-bin.000002 |  201 | Format_desc    |         7 |           0 | Server ver: 5.7.26-log, Binlog ver: 4  |
| mister_f-relay-bin.000002 |  320 | Anonymous_Gtid |         7 |        1855 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mister_f-relay-bin.000002 |  385 | Query          |         7 |        1971 | create database yanshi charset utf8mb4 |
| mister_f-relay-bin.000002 |  501 | Anonymous_Gtid |         7 |        2036 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mister_f-relay-bin.000002 |  566 | Query          |         7 |        2149 | create database delay charset utf8mb4  |
| mister_f-relay-bin.000002 |  679 | Anonymous_Gtid |         7 |        2214 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mister_f-relay-bin.000002 |  744 | Query          |         7 |        2314 | use `delay`; create table t1 (id int)  |
| mister_f-relay-bin.000002 |  844 | Anonymous_Gtid |         7 |        2379 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mister_f-relay-bin.000002 |  909 | Query          |         7 |        2452 | BEGIN                                  |
| mister_f-relay-bin.000002 |  982 | Table_map      |         7 |        2498 | table_id: 150 (delay.t1)               |
| mister_f-relay-bin.000002 | 1028 | Write_rows     |         7 |        2548 | table_id: 150 flags: STMT_END_F        |
| mister_f-relay-bin.000002 | 1078 | Xid            |         7 |        2579 | COMMIT /* xid=747 */                   |
| mister_f-relay-bin.000002 | 1109 | Anonymous_Gtid |         7 |        2644 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mister_f-relay-bin.000002 | 1174 | Query          |         7 |        2739 | drop database delay                    |
+---------------------------+------+----------------+-----------+-------------+----------------------------------------+
17 rows in set (0.00 sec)

截取relaylog日志
[root@mister_f ~]# mysqlbinlog --start-position=501 --stop-position=1174 /data/3308/data/mister_f-relay-bin.000002 >/tmp/relay.sql

恢复到从库

[root@db01 data]# mysql -uroot -p -S /data/3308/mysql.sock 
mysql> set sql_log_bin=0;
mysql> source /tmp/relay.sql
mysql> set sql_log_bin=1;

2,过滤复制

需求:假如数据库里有a,b,c这三个库但是只有a库是核心库其他两个是测试库,
为了尽可能减少主从复制的的压力,在主从复制的时候复制其中一个或者几个这样的需求,也就是有选择性的需求,默认是全部的。 

两种方法限制:
主库:限制binlog
通过show master status查看
binlog_do_db                 白名单 :只记录白名单里的库
binlog_ignore_db             黑名单:不记录黑名单里的库
在配置文件里添加后重启生效
例:
binlog_do_db=world

从库:relaylog
show slave status\G

Replicate_Do_DB: 
Replicate_Ignore_DB: 
Replicate_Do_Table: 
Replicate_Ignore_Table: 
Replicate_Wild_Do_Table: 
Replicate_Wild_Ignore_Table
和主库的一样

例子

[root@mister_f ~]# vim /data/3308/my.cnf 
replicate_do_db=repl
[root@mister_f ~]# systemctl restart mysqld3308.service 
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.21.0.8
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mister_f-relay-bin.000024
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: repl
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
测试:
主库:
mysql> create database aa charset utf8mb4;
Query OK, 1 row affected (0.01 sec)


从库:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.21.0.8
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 323
               Relay_Log_File: mister_f-relay-bin.000024
                Relay_Log_Pos: 489
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: repl
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| backup             |
| bin                |
| binlog             |
| gtid               |
| mysql              |
| performance_schema |
| sys                |
| test               |
| wordpress          |
| world              |
| yanshi             |
+--------------------+
12 rows in set (0.01 sec)

上面阔以看出日志已经过来但是没有同步,只会回放repl这个库
主库:
mysql> create database repl charset utf8mb4;
Query OK, 1 row affected (0.01 sec)

从库:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.21.0.8
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 498
               Relay_Log_File: mister_f-relay-bin.000024
                Relay_Log_Pos: 664
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: repl
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| backup             |
| bin                |
| binlog             |
| gtid               |
| mysql              |
| performance_schema |
| repl               |
| sys                |
| test               |
| wordpress          |
| world              |
| yanshi             |
+--------------------+
13 rows in set (0.00 sec)
可以看出上面的库已经同步过来了,

3,GTID复制

介绍

GTID(Global Transaction ID)是对于一个已提交事务的唯一编号,并且是一个全局(主从复制)唯一的编号。
它的官方定义如下:
GTID = source_id :transaction_id
7E11FA47-31CA-19E1-9E56-C43AA21293967:29
什么是sever_uuid,和Server-id 区别?
核心特性: 全局唯一,具备幂等性

准备环境

三台机器一主两从
主库:
10.0.0.51
从库  
10.0.0.52
10.0.0.53
(1)清理环境
pkill mysqld
创建目录:
mkdir -p /data/mysql/data
mkdir -p /data/binlog
mkdir -p /application 
上传软件到到application
(2) 准备配置文件
主库db01:

cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/application/mysql/
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=51
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db01 [\\d]>
EOF

slave1(db02):
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=52
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db02 [\\d]>
EOF

slave2(db03):
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=53
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db03 [\\d]>
EOF


(3) 初始化数据
mysqld --initialize-insecure --user=mysql --basedir=/application/mysql  --datadir=/data/mysql/data 

(4) 启动数据库
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start


(5) 构建主从:
master:51
slave:52,53

主库:
grant replication slave  on *.* to repl@'10.0.0.%' identified by '123';

从库:
change master to 
master_host='10.0.0.51',
master_user='repl',
master_password='123' ,
MASTER_AUTO_POSITION=1;
start slave;
show slave status\G

GTID核心参数

gtid-mode=on                                --启用gtid类型,否则就是普通的复制架构
enforce-gtid-consistency=true               --强制GTID的一致性
log-slave-updates=1                         --slave更新是否记入日志

GTID 复制和普通复制的区别

普通:
CHANGE MASTER TO
MASTER_HOST='10.0.0.51',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3307,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=444,
MASTER_CONNECT_RETRY=10;

GTID复制:
change master to 
master_host='10.0.0.51',
master_user='repl',
master_password='123' ,
MASTER_AUTO_POSITION=1;
start slave;


(0)在主从复制环境中,主库发生过的事务,在全局都是由唯一GTID记录的,更方便Failover
(1)额外功能参数(3个)
(2)change master to 的时候不再需要binlog 文件名和position号,MASTER_AUTO_POSITION=1;
(3)在复制过程中,从库不再依赖master.info文件,而是直接读取最后一个relaylog的 GTID号
(4) mysqldump备份时,默认会将备份中包含的事务操作,以以下方式
    SET @@GLOBAL.GTID_PURGED='8c49d7ec-7e78-11e8-9638-000c29ca725d:1-11';
    告诉从库,我的备份中已经有以上事务,你就不用运行了,直接从下一个GTID开始请求binlog就行

4,半同步复制

介绍

解决主从数据一致性问题

半同步工作原理

1. 主库执行新的事务,commit时,更新 show master  status\G ,触发一个信号给
2. binlog dump 接收到主库的 show master status\G信息,通知从库日志更新了
3. 从库IO线程请求新的二进制日志事件
4. 主库会通过dump线程传送新的日志事件,给从库IO线程
5. 从库IO线程接收到binlog日志,当日志写入到磁盘上的relaylog文件时,给主库ACK_receiver线程
6. ACK_receiver线程触发一个事件,告诉主库commit可以成功了
7. 如果ACK达到了我们预设值的超时时间,半同步复制会切换为原始的异步复制.

配置

加载插件
主:
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
从:
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
查看是否加载成功:
show plugins;
启动:
主:
SET GLOBAL rpl_semi_sync_master_enabled = 1;
从:
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
重启从库上的IO线程
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;
查看是否在运行
主:
show status like 'Rpl_semi_sync_master_status';
从:
show status like 'Rpl_semi_sync_slave_status';

相关文章

网友评论

    本文标题:九,主从复制高级进阶

    本文链接:https://www.haomeiwen.com/subject/mpcojltx.html