• mysql 5.7多源复制(用于生产库多主库合并到一个查询从库)


      目前我们使用的是主从+分库分表的系统架构,主库有N个分库,从库为多个slave做负载均衡,所以数据库端的架构是下面这样的:

      因为差不多有一年半没有专门搞技术为主了,顺带回顾下。

      这就涉及到多个主库数据同步到不分库分表的从库共查询和管理类系统使用。在mysql 5.6以及之前的版本中,没有原生的解决方法,除非使用mariadb分支,在mysql 5.7之后支持多源复制,除了使用原生的多源复制之外,还有一个选择,就是使用案例开源的otter/canal。如果只是N个库合并到一个库的,我们使用mysql原生的复制,因为无论从稳定性还是运维成本、系统要求的角度,mysql复制都合理的多。对于需要特殊处理比较多的或者目标库为oracle的,我们使用otter/canal。文本讲述mysql多源的搭建。下一文中,我们会讲述完整的otter环境搭建并进行简单的性能测试。

      首先安装mysql 5.7,推荐使用percona server,相关参数优化推荐等请参考mysql安装以及配置参数优化。

      因为环境限制,两个主节点在同一台机器,从节点另外一台机器。

    172.28.1.97 3307 主1
    172.28.1.97 3308 主2
    10.20.24.89 3308 从

      同时172.28.1.97 3308 主2有三个database,ta_1,ta_2,ta_base,均同步到从库的ta库。

      和mysql一主一从复制相比,多源复制加入了一个叫做Channel的概念, 每一个Channel都是一个独立的Slave,都有一个IO_THREAD和SQL_THREAD。原理和普通复制一样。我们只需要对每一个Master执行Change Master 语句,只需要在每个语句最后使用For Channel来进行区分。多源复制和正常主从其他的配置都一样,基本上主库开下binlog、server-id不一样就可以了,只有下列额外限制:

    • master-info-repository必须为TABLE
    • relay-log-info-repository必须为TABLE
    • 以FOR CHANNEL 'CHANNEL_NAME'区分不同的master。

      首先参考mysql单机版安装mysql 5.7安装与参数优化,下列为slave直接相关的参数,在/etc/my.cnf中额外或者修改下列参数:

    master-info-repository=TABLE
    relay-log-info-repository=TABLE
    # replicate-rewrite-db 多库同步到单库,库名重写,其他的replicate-*会在replicate-rewrite-db评估后执行,多个映射的话,配置文件中包含多行即可,这个设计好傻,为啥不逗号或者分号分隔呢。如果同时有多个replicate*过滤器,先评估数据库级别的、然后表级别的;先评估do,后评估ignore(也就是在白名单或者不在黑名单的模式)。比如,主库多个分库合并到从库一个库
    replicate-rewrite-db=ta_base->ta
    replicate-rewrite-db=ta_1->ta
    replicate-rewrite-db=ta_2->ta
    sync_relay_log=1
    relay_log_recovery=1
    slave-parallel-type=LOGICAL_CLOCK
    slave-parallel-workers=16 #具体值多少合适需要性能测试得到,一般cpu数量即可
    server-id = 2
    replicate-do-db # 如果只要同步某些库
    replicate-ignore-db #如果只需要不同步某些库
    slave-skip-errors=ddl_exist_errors + 1022 #建议不要同步ddl(1007,1008,1050,1051,1054,1060,1061,1068,1094,1146,1022)
    log_slave_updates=ON(GTID模式必须开始log_slave_updates,对性能有一定影响,Mysql 5.7之后从节点可以不开启binlog)
    skip-slave-start=false #默认false,也就是server重启的时候会自动启动slave,不建议修改

    启动mysql服务器。

    MySQL [(none)]> SET GLOBAL master_info_repository = 'TABLE';
    Query OK, 0 rows affected (0.00 sec)
    
    MySQL [(none)]> SET GLOBAL relay_log_info_repository = 'TABLE';
    Query OK, 0 rows affected (0.00 sec)
    
    -- 注:不同于设置全局变量,所有这些通过change master修改的信息都有存储在performance_schema的replication相关表中,重启后不会失效,复制连接信息存储在performance_schema库的replication_connection_configuration表中,IO线程当前状态在replication_connection_status。SQL线程的配置和状态分别在replication_applier_configuration和replication_applier_status表。
    所有这些通过change/replication修改的信息都有存储在performance_schema的replication相关表中,重启后会失效,一定要同时保存到配置文件中
    MySQL [(none)]> CHANGE MASTER TO MASTER_HOST='172.18.1.97',MASTER_PORT=3307,MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=1834 FOR CHANNEL 'Master_3307';
    Query OK, 0 rows affected, 2 warnings (0.03 sec)
    
    MySQL [(none)]> CHANGE MASTER TO MASTER_HOST='172.18.1.97',MASTER_PORT=3308,MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=7484 FOR CHANNEL 'Master_3308';
    Query OK, 0 rows affected, 2 warnings (0.01 sec)
    
    MySQL [(none)]> start slave for channel 'Master_3307';
    Query OK, 0 rows affected (0.00 sec)
    
    MySQL [(none)]> start slave for channel 'Master_3308';
    Query OK, 0 rows affected (0.01 sec)
    
    MySQL [(none)]> show slave status for channel 'Master_3307'G;
    *************************** 1. row ***************************
                   Slave_IO_State: Connecting to master
                      Master_Host: 172.18.1.97
                      Master_User: repl
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 1834
                   Relay_Log_File: slave-relay-bin-master_3307.000001
                    Relay_Log_Pos: 4
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Connecting
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: ta
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 1834
                  Relay_Log_Space: 154
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: NULL
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 2003
                    Last_IO_Error: error connecting to master 'repl@172.18.1.97:3307' - retry-time: 60  retries: 1
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 0
                      Master_UUID: 
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 180703 08:23:54
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: (ta_base,ta),(ta_1,ta),(ta_2,ta)
                     Channel_Name: master_3307
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    
    ERROR: No query specified
    
    MySQL [(none)]> show slave status for channel 'Master_3308'G;
    *************************** 1. row ***************************
                   Slave_IO_State: Connecting to master
                      Master_Host: 172.18.1.97
                      Master_User: repl
                      Master_Port: 3308
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000002
              Read_Master_Log_Pos: 7484
                   Relay_Log_File: slave-relay-bin-master_3308.000001
                    Relay_Log_Pos: 4
            Relay_Master_Log_File: mysql-bin.000002
                 Slave_IO_Running: Connecting
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: ta
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 7484
                  Relay_Log_Space: 154
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: NULL
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 2003
                    Last_IO_Error: error connecting to master 'repl@172.18.1.97:3308' - retry-time: 60  retries: 1  #这里是因为后来网断了,前面忘了截图下来
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 0
                      Master_UUID: 
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 180703 08:23:58
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: (ta_base,ta),(ta_1,ta),(ta_2,ta)
                     Channel_Name: master_3308
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    
    ERROR: No query specified
    
    MySQL [(none)]> exit
    Bye

    其他注意点

    • mysql仅支持实例级别设置计数器的步长,通过auto_increment_increment参数控制,这样分库分表的时候,自增表的auto_increment就需要区分开从1还是2开始。
    • 在mysql 8.0之前,global参数在重启之后就会失效,所以对于可以动态修改的全局参数,需要同时修改my.cnf配置文件确保重启后保持一致。
    • 如果主库是双节点,还需要有个监控程序监控主库,便于宕机后自动切换到另外一个节点,具体实现根据基于日志点位、GTID的不同而不同(有需求可留站内信,可提供同时监控并自动重启rabbitmq、es、tomcat、spring boot、mysql复制、redis、zk、otter manager的守护程序)。

    参考

    https://blog.csdn.net/qustdjx/article/details/26937325/
    http://yoshinorimatsunobu.blogspot.com/2013/10/making-full-table-scan-10x-faster-in.html
    https://www.cnblogs.com/zhoujinyi/p/5704567.html
    MySQL 5.7并行复制实现原理与调优
    https://www.oschina.net/translate/showdown-mysql-8-vs-postgresql-10
    https://aws.amazon.com/cn/about-aws/whats-new/2017/12/amazon-aurora-with-mysql-compatibility-speeds-query-processing-with-hash-join-and-batched-scans/
    https://blog.csdn.net/chenhaifeng2016/article/details/77530569

  • 相关阅读:
    第二题:坦克游戏1.0(方法:动态规划)
    第一题:小鼠迷宫问题(方法:广搜)
    我的世界之电脑mod小乌龟 —— 方位上的操作 lua函数集
    NOIP 2011 提高组 选择客栈(vijos 1737)(方法:队列,数学)
    codeforces_1040_A Python练习
    codeforces_466_C Python练习
    codeforces_158_B Python练习
    三.Python_scrapy的Item对象 学习笔记
    二.Pyhon_scrapy终端(scrapy shell)学习笔记
    一.Python_srcrapy的命令行工具 学习笔记(Command line tool)
  • 原文地址:https://www.cnblogs.com/zhjh256/p/9260554.html
Copyright © 2020-2023  润新知