同一行 ID 后主主复制失败

master master replication failed after same Row ID

您好,我已经设置了 Master - Master mysql 复制。

Master A = Localsystem
Master B = Server

它工作正常。 但是当 Master A 无法从 Master B 访问时

我复制的 table 的 Id 字段直到 - 233

现在在同一个 table 的两个 master 上插入了一行。

都在 table 中获得了下一个 ID 234。

并且当 Master A 对 Master B 可达时。 两端复制失败。

**

on Master A;
show master status \G;

**

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 13.235.146.234
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000097
          Read_Master_Log_Pos: 33310336
               Relay_Log_File: ded18908-1-relay-bin.000003
                Relay_Log_Pos: 4445481
        Relay_Master_Log_File: mysql-bin.000097
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: school_testing.ai_student_attendance,school_testing.ai_staff_attendance
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1062
                   Last_Error: Error 'Duplicate entry '234' for key 'PRIMARY'' on query. Default database: 'school_testing'. Query: 'INSERT INTO `school_testing`.`ai_student_attendance` (`ids`, `student_id`, `atten_list`, `atten_cont`, `month`) VALUES (NULL, '124', 'sasas', 'sasas', 'sasas')'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 20766001
              Relay_Log_Space: 22735128
              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: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1062
               Last_SQL_Error: Error 'Duplicate entry '234' for key 'PRIMARY'' on query. Default database: 'school_testing'. Query: 'INSERT INTO `school_testing`.`ai_student_attendance` (`ids`, `student_id`, `atten_list`, `atten_cont`, `month`) VALUES (NULL, '124', 'sasas', 'sasas', 'sasas')'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 3
1 row in set (0.00 sec)

ERROR: 
No query specified

**

Master - B 
show slave status \G;

**

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 182.21.11.142
                  Master_User: replica
                  Master_Port: 22331
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 111212
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 43815
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: school_testing.ai_student_attendance,school_testing.ai_staff_attendance
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1062
                   Last_Error: Error 'Duplicate entry '234' for key 'PRIMARY'' on query. Default database: 'school_testing'. Query: 'INSERT INTO `school_testing`.`ai_student_attendance` (`ids`, `student_id`, `atten_list`, `atten_cont`, `month`) VALUES (NULL, '125', 'dsdsdsa', 'dsada', 'asdasdadasdadada')'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 83284
              Relay_Log_Space: 72201
              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: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1062
               Last_SQL_Error: Error 'Duplicate entry '234' for key 'PRIMARY'' on query. Default database: 'school_testing'. Query: 'INSERT INTO `school_testing`.`ai_student_attendance` (`ids`, `student_id`, `atten_list`, `atten_cont`, `month`) VALUES (NULL, '125', 'dsdsdsa', 'dsada', 'asdasdadasdadada')'
1 row in set (0.00 sec)

ERROR: 
No query specified

我不知道该如何解决这个问题。我需要专家帮助。

这种情况很少见。但是当我遇到这个问题时我的复制失败了

你需要做的是在每个master上使用不同的ID Offsets:

在您的 my.cnf 个文件中 -

在大师 1 上:

auto_increment_increment=2 
auto_increment_offset=1 

在大师 2 上:

auto_increment_increment=2 
auto_increment_offset=2 

这样每个人都会生成一组不同的 ID。

例如

大师 1 = 1, 3, 5

大师 2 = 2, 4, 6

这样您就不会出现重复输入错误(只要您使用 auto_increment 作为您的 ID)并且不会手动输入它们。