欢迎阅读!

潇湘夜雨

当前位置: 主页 > 数据库 >

mysql5.7多线程gtid主从复制

时间:2016-10-26 21:31来源:潇湘夜雨 作者:华嵩阳 点击:
环境准备: mysql5.7.16 centos6.7 master:172.18.109.235 salve1:172.18.109.236 salve2:172.18.109.236 注意:如果数据库服务器是通过虚拟机克隆或复制主库的源代码的方式安装,要删除auto.conf再启动m
环境准备:
mysql5.7.16
centos6.7
master:172.18.109.235
salve1:172.18.109.236
salve2:172.18.109.236
注意:如果数据库服务器是通过虚拟机克隆或复制主库的源代码的方式安装,要删除auto.conf再启动mysql
否则会使几个数据库的gtid号相同,导致后面的主从同步失败。
GTID的概述:
1、全局事物标识:global transaction identifieds。
2、GTID事物是全局唯一性的,且一个事务对应一个GTID。
3、一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
4、GTID用来代替classic的复制方法,不在使用binlog+pos开启复制。而是使用master_auto_postion=1的方式自动匹配GTID断点进行复制。
5、MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。
6、在传统的slave端,binlog是不用开启的,但是在GTID中,slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。
 
GTID的工作原理:
1、master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
2、slave端的i/o 线程将变更的binlog,写入到本地的relay log中。
3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。
4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。
6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
 
一、主库配置
1. 主库上必须启用两个参数
 
gtid-mode = on #开启gtid
enforce-gtid-consistency = 1 
skip_slave_start=1 #数据库起来后不能直接开启复制
log_bin = mysql-bin
server-id = 1
binlog_format = row 
binlog-ignore-db=mysql #忽略mysql数据库
修改完参数重启数据库
 
2.授权从库
 
mysql>GRANT REPLICATION SLAVE ON *.* to 'lzh'@'172.18.109.%' identified by '123456';
 
二、从库配置
 
这里从库slave1 和slave2,配置参数除了id号不同,其余都是相同的配置。
1. slave端的参数文件
 
#sync_master_info = 1 
#sync_relay_log_info = 1
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16 
master-info-repository  = TABLE 
relay-log-info-repository = TABLE 
relay_log_recovery=ON
gtid-mode = on
enforce-gtid-consistency = 1
skip_slave_start=1
server-id = 11 #salve1为11,salve2为12,保证id号不重复。
 
注意;master info与relay info还有3个参数控制刷新:
sync_relay_log:默认为10000,即每10000次sync_relay_log事件会刷新到磁盘。为0则表示不刷新,交由OS的cache控制。
sync_master_info:若master-info-repository为FILE,当设置为0,则每次sync_master_info事件都会刷新到磁盘,默认
为10000次刷新到磁盘;
若master-info-repository为TABLE,当设置为0,则表不做任何更新,设置为1,则每次事件会更新表 #默认为10000
sync_relay_log_info:若relay_log_info_repository为FILE,当设置为0,交由OS刷新磁盘,
默认为10000次刷新到磁盘;若relay_log_info_repository为TABLE,且为INNODB存储,则无论为任何值,则都每次evnet都会更新表。
若slave的binlog和redo也实时刷新的话,会带来很严重的IO性能瓶颈。根据实际需求设置,保证了数据一致,就很难保证性能。
 
配置完参数重新启动mysql服务
/etc/init.d/mysql restart
 
2.执行同步
 
登录从库执行如下操作
设置主库源
mysql> change master to master_host='172.18.109.235', master_port=3306, master_user='lzh',master_password='123456', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
注意:这里和非gtid的主从复制不同,不用设置mysql.bin日志的position,更加方便。
启动同步进程
mysql> start slave;
Query OK, 0 rows affected (0.08 sec)
mysql> show salve status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.109.235
                  Master_User: lzh
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: hadoop2-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          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: 154
              Relay_Log_Space: 576
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: fec611e4-99a2-11e6-b35d-000c29f8c231
             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: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 1 (责任编辑:liangzh)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
推荐内容