Mysql基於gtid的主從複製錯誤:1062

說明:
環境
mysql-master:172.16.200.43
mysql-slave:172.16.200.44
系統:centos7
版本:MySQL5.6.35mysql

1、基於GTID複製環境的搭建前提

主從環境的搭建和5.5沒有什麼區別,惟一須要注意的是:開啓GTID須要啓用這三個參數:sql

#GTID
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates   = 1

gtid-mode用來設置是否開啓GTID功能,若是要開啓GTID功能,須要同時開啓log-bin和log_slave_updates功能,另外還須要開啓enforce_gtid_consistency功能。gtid_mode參數能夠設置爲on、off、upgrade_step_一、upgrade_step_2四種值,其中upgrade_step_1和upgrade_step_2是給未來mysql可能的新功能預留的,對當前的myql沒有任何意義。同時,mysql建議在mysql_upgrade的時候,關閉gtid_mode功能和enforce_gtid_consistency功能,由於Mysql在upgrade期間可能會操做非事務的MyISAM存儲引擎表,會引發報錯。centos

2、配置步驟

一、43上面受權44從庫的帳號

MySQL [(none)]> grant replication slave on *.* to  'slave'@'172.16.200.44' identified by '000000';
MySQL [(none)]> flush privileges;

二、配置主從同步

MySQL [(none)]> stop slave;
MySQL [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.200.43', MASTER_USER='slave', MASTER_PASSWORD='000000', MASTER_AUTO_POSITION=1;
MySQL [(none)]> start slave;

三、關閉防火牆

systemctl stop firewalld.service   #中止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

3、測試跳過一個事務

一、 43上面新建庫test

二、43上面新建表info_area

CREATE TABLE `info_area` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '地區ID',
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '名稱',
  `rel_id` varchar(50) NOT NULL DEFAULT '' COMMENT '關係ID',
  `pid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父ID',
  `level` int(11) NOT NULL DEFAULT '0' COMMENT '類別,一、省份 二、市 三、區 四、縣',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNQ_RID` (`rel_id`) USING BTREE,
  KEY `IDX_PID` (`rel_id`)
) ENGINE=InnoDB AUTO_INCREMENT=55185 DEFAULT CHARSET=utf8 COMMENT='地區表';

三、43插入3條語句,44徹底同步

INSERT INTO `test`.`info_area` (`id`, `name`, `rel_id`, `pid`, `level`) VALUES ('10000', '北京', '10000', '10000', '1');
INSERT INTO `test`.`info_area` (`id`, `name`, `rel_id`, `pid`, `level`) VALUES ('10002', '三環之內', '100001000110002', '10001', '3');
INSERT INTO `test`.`info_area` (`id`, `name`, `rel_id`, `pid`, `level`) VALUES ('10003', '三環到四環之間', '100001000110003', '10001', '3');

四、 43刪除id=10001

delete from info_area where id=10001;

五、44上面插入10001

INSERT INTO `test`.`info_area` (`id`, `name`, `rel_id`, `pid`, `level`) VALUES ('10001', '朝陽區', '1000010001', '10000', '2');

六、43上面插入10001

INSERT INTO `test`.`info_area` (`id`, `name`, `rel_id`, `pid`, `level`) VALUES ('10001', '朝陽區', '1000010001', '10000', '2');

因爲主從複製,44也會同步該條數據,可是44上面剛剛已經插入過id=10001,因此此時報錯ide

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.200.43
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 83009452
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 83009382
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1062
                   Last_Error: Could not execute Write_rows event on table test.info_area; Duplicate entry '10001' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000007, end_log_pos 83009421
           Retrieved_Gtid_Set: 0f869100-71d1-11e7-be5e-000c29f2e72e:1-14
            Executed_Gtid_Set: 0f869100-71d1-11e7-be5e-000c29f2e72e:1-13,
22fd263e-71d1-11e7-be5e-000c29fffc35:1
                Auto_Position: 1

七、解決gtid下1062錯誤

mysql> stop slave;
mysql> set GTID_NEXT='0f869100-71d1-11e7-be5e-000c29f2e72e:14';  
mysql> begin;
mysql> commit;
mysql> set GTID_NEXT='AUTOMATIC';
mysql> start slave;
注:
傳統方式
mysql> stop slave;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n;  
mysql> start slave;

八、恢復正常

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.200.43
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 83009452
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 448
        Relay_Master_Log_File: mysql-bin.000007
             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: 83009452
              Relay_Log_Space: 83010163
              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: 0f869100-71d1-11e7-be5e-000c29f2e72e
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 0f869100-71d1-11e7-be5e-000c29f2e72e:1-14
            Executed_Gtid_Set: 0f869100-71d1-11e7-be5e-000c29f2e72e:1-14,
22fd263e-71d1-11e7-be5e-000c29fffc35:1
                Auto_Position: 1
1 row in set (0.00 sec)
相關文章
相關標籤/搜索