• MySQL增强半同步的搭建实验,和一些参数的个人理解


     

    关于参数理解,已补充实验,可以查看: rpl_semi_sync_master_wait_no_slave 参数研究实验

     

    环境信息

    role

    ip

    port

    hostname

    master

    192.168.188.101

    4306

    mysqlvm1

     

     

     

     

    slave

    192.168.188.201

    4306

    mysqlvm1-1

     

     

    5306

     

     

     

    6306

     

     

     

    7306

     

     

    MySQL版本

      5.7.26

     

    前置条件

      已配置好主从复制。

     

    配置增强半同步

      1.加载lib,所有主从节点都要配置。

        主库:install plugin rpl_semi_sync_master soname 'semisync_master.so';

        从库:install plugin rpl_semi_sync_slave soname 'semisync_slave.so';         

        ps:可以一起装。建议一起装,因为会有主从切换的情景。

    2.查看,确保所有节点都成功加载。

      mysql> show plugins;

        | rpl_semi_sync_master       | ACTIVE   | REPLICATION        | semisync_master.so | GPL     |

        | rpl_semi_sync_slave        | ACTIVE   | REPLICATION        | semisync_slave.so  | GPL     |

     

    3.启用半同步

      1.先启用从库上的参数,最后启用主库的参数。

        从库:set global rpl_semi_sync_slave_enabled = {0|1};   # 1:启用,0:禁止

        主库:

          set global rpl_semi_sync_master_enabled = {0|1};   # 1:启用,0:禁止

          set global rpl_semi_sync_master_timeout = N;       # 单位为ms

     

        PS:配置文件加载库: #官方手册建议这样写入配置文件,但是——

          风险!:endbled写入配置文件的话,会使实例启动后立即进入半同步模式,如果发生长时间断连的实例重新运行启动,有可能导致主库被拖垮。

          建议!:长时间断开的从库,重新连接后,要等待追完全部事务后,手动开启半同步模式,而不是启动后直接切换,防止冲击主库。

     

            [mysqld]

            rpl_semi_sync_master_enabled=1

            rpl_semi_sync_master_timeout=1000

     

            [mysqld]

            rpl_semi_sync_slave_enabled=1

     

      2.从库重启io_thread

        stop slave io_thread;

        start slave io_thread;

        此时在主库会发现

          mysql> show global status like "%sync%";

     

    | Variable_name                              | Value |

     

    | Innodb_data_fsyncs                         | 141   |

     

    | Innodb_data_pending_fsyncs                 | 0     |

     

    | Innodb_os_log_fsyncs                       | 87    |

     

    | Innodb_os_log_pending_fsyncs               | 0     |

     

    | Rpl_semi_sync_master_clients               | 2     |

    #支持和注册半同步复制的已连Slave数。

    | Rpl_semi_sync_master_net_avg_wait_time     | 0     |

    # master等待slave回复的平均等待时间,单位毫秒。

    | Rpl_semi_sync_master_net_wait_time         | 0     |

    # master总的等待时间。

    | Rpl_semi_sync_master_net_waits             | 0     |

    # master等待slave回复的的总的等待次数,即半同步复制的总次数,不管失败还是成功,不算半同步失败后的异步复制。

    | Rpl_semi_sync_master_no_times              | 1     |

    # master关闭半同步复制的次数。

    | Rpl_semi_sync_master_no_tx                 | 8     |

    # master没有收到slave的回复而提交的次数,可以理解为master等待超时的次数,即半同步模式不成功提交数量。

    | Rpl_semi_sync_master_status                | ON    |

    # ON是活动状态(半同步),OFF是非活动状态(异步),用于表示主服务器使用的是异步复制模式,还是半同步复制模式。

    | Rpl_semi_sync_master_timefunc_failures     | 0     |

     

    | Rpl_semi_sync_master_tx_avg_wait_time      | 0     |

    # master花在每个事务上的平均等待时间。

    | Rpl_semi_sync_master_tx_wait_time          | 0     |

    # master总的等待时间。

    | Rpl_semi_sync_master_tx_waits              | 0     |

    # master等待成功的次数,即master没有等待超时的次数,也就是成功提交的次数

    | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |

    # master提交后来的先到了,而先来的还没有到的次数。

    | Rpl_semi_sync_master_wait_sessions         | 0     |

    # 前有多少个session因为slave的回复而造成等待。

    | Rpl_semi_sync_master_yes_tx                | 0     |

    # master成功接收到slave的回复的次数,即半同步模式成功提交数量。

    | Rpl_semi_sync_slave_status                 | OFF   |

    # Slave上的半同步复制状态,ON表示已经被启用,OFF表示非活动状态。

     

      3.主库参数详解 

        mysql> show global variables like '%sync%';

    | Variable_name                             | Value      |

     

    | binlog_group_commit_sync_delay            | 100        |

     

    | binlog_group_commit_sync_no_delay_count   | 10         |

     

    | innodb_flush_sync                         | ON         |

     

    | innodb_sync_array_size                    | 1          |

     

    | innodb_sync_spin_loops                    | 30         |

     

    | rpl_semi_sync_master_enabled              | ON         |

    #(主库)是否启动半同步

    | rpl_semi_sync_master_timeout              | 100000     |

    #为了防止半同步复制中主在没有收到S发出的确认发生堵塞,用来设置超时,超过这个时间值没有收到信息,则切换到异步复制,执行操作。默认为10000毫秒,等于10秒,这个参数动态可调,表示主库在某次事务中,如果等待时间超过10秒,那么则降级为异步复制模式,不再等待SLAVE从库。如果主库再次探测到SLAVE从库恢复了,则会自动再次回到半同步复制模式。

    | rpl_semi_sync_master_trace_level          | 32         |

     

    | rpl_semi_sync_master_wait_for_slave_count | 1          |

     #For example:if rpl_semi_sync_master_wait_for_slave_count is 2, then 2 slaves must acknowledge receipt of the transaction before the timeout period configured by rpl_semi_sync_master_timeout for semisynchronous replication to proceed. If less slaves acknowledge receipt of the transaction during the timeout period, the master reverts to normal replication.

    超时前至少x个来自slave对的ack应答。如果超时前的应答未及该值,则master转为异步复制。

    在slave存活大于等于该值时,master提交不会有任何等待发生,一旦slave存活低于该值,则master会进行等待,直到超时。

    | rpl_semi_sync_master_wait_no_slave        | ON         |

     # 这个参数,我在中文网络上就没见到说对的搞懂了~ 20200117:

    这个值影响master根据slave数量决定什么时候转为异步复制。

    【在完全无事务进行的空业务环境下进行实验】。

    原理:

      master会根据一定时间获取连接的slave数量(rpl_semi_sync_master_clients) ,同时用获取到的值与参数(rpl_semi_sync_master_wait_for_slave_count)进行对比,来决定使用怎样的复制方式。

    实验:

      令存活的slave数量小于参数(rpl_semi_sync_master_wait_for_slave_count)值, 过一定时间后,master检测到真实slave数量(rpl_semi_sync_master_clients)小于参数(rpl_semi_sync_master_wait_for_slave_count)值——

    如果该值为ON:

      master不会立即将半同步复制转为异步复制(Rpl_semi_sync_master_status=ON)。

      此时开启事务,只要在超时时间范围内(rpl_semi_sync_master_timeout)应答该事物的slave达到参数(rpl_semi_sync_master_wait_for_slave_count)值,master就会一直保持半同步。

      也就是说,直到事务提交超时前,master都不会根据检测到的slave数量变更复制模式。

    如果该值为OFF:

      master会立即转为异步复制(Rpl_semi_sync_master_status=OFF)。

      也就是说,只要master检测到的slave数量低于count参数的设定,便立即变更复制模式。

       off的话, 如果master根据检测slave数量自行降为异步, 那么开始事务的时候就不用等超时(直接异步了,事务不用等待)。 如果是on的话,master会超时后再降为异步,事务会等待。

      但是询问发现,生产环境都是ON,且默认值就是ON,为什么呢?这个我还不知道。

    【8.0文档】

    Controls whether the master waits for the timeout period configured by rpl_semi_sync_master_timeout to expire, even if the slave count drops to less than the number of slaves configured by rpl_semi_sync_master_wait_for_slave_count during the timeout period.

     

    When the value of rpl_semi_sync_master_wait_no_slave is ON (the default), it is permissible for the slave count to drop to less than rpl_semi_sync_master_wait_for_slave_count during the timeout period. As long as enough slaves acknowledge the transaction before the timeout period expires, semisynchronous replication continues.

     

    When the value of rpl_semi_sync_master_wait_no_slave is OFF, if the slave count drops to less than the number configured in rpl_semi_sync_master_wait_for_slave_count at any time during the timeout period configured by rpl_semi_sync_master_timeout, the master reverts to normal replication.

     

    This variable is available only if the master-side semisynchronous replication plugin is installed.

    | rpl_semi_sync_master_wait_point           | AFTER_SYNC |

     #5.7默认AFTER_SYNC,在得到slave的应答后再commit,可选值AFTER_COMMIT。

    | rpl_semi_sync_slave_enabled               | OFF        |

    #(从库)是否启动半同步

    | rpl_semi_sync_slave_trace_level           | 32         |

     

    | sync_binlog                               | 1          |

     

    | sync_frm                                  | ON         |

     

    | sync_master_info                          | 10000      |

     

    | sync_relay_log                            | 10000      |

     

    | sync_relay_log_info                       | 10000      |

     

              

      4.维护

      •   从上面得知,如果slave无法连接,那么master会等待至超时后,将该slave转为异步模式;当从库重新连接后,在rpl_semi_sync_master_wait_no_slave=ON时(默认),从库会自动转换为半同步模式。
      •   长时间断开的从库,重新连接后,要等待追完全部事务后,手动开启半同步模式,而不是启动后直接切换。

     

        从库断开后的重连维护:

          start slave io_thread;

          show slave status G        #查看 Retrieved_Gtid_Set、Executed_Gtid_Set,前者应该接收到master的gtid,后者没有

          start slave sql_thread;

          show slave status G        #查看 Retrieved_Gtid_Set、Executed_Gtid_Set,此时二者相同,追到master的GTID。

          set rpl_semi_sync_slave_enabled = 1;     #开启半同步

          stop io_thread;

          start io_thread;

  • 相关阅读:
    爬取1907条『课程学习』数据,分析哪类学习资源最受大学生青睐
    以『B站』为实战案例!手把手教你掌握爬虫必备框架『Scrapy』
    python爬取各类基金数据,以『动图可视化』方式展示基金的涨跌情况
    详细实战教程!部署Flask网站+域名访问+免费https证书
    王者荣耀白晶晶皮肤1小时销量突破千万!分析网友评论我发现了原因
    python爬取『大年初一』热映电影,以『可视化及词云秀』方式带你了解热映电影...
    python爬取44130条用户观影数据,分析挖掘用户与电影之间的隐藏信息!
    Nanopore sequencing and assembly of a human genome with ultra-long reads
    Genome Sequencing and Assembly by Long Reads in Plants
    SiLiCO: A Simulator of Long Read Sequencing in PacBio and Oxford Nanopore
  • 原文地址:https://www.cnblogs.com/konggg/p/12204874.html
Copyright © 2020-2023  润新知