mysql - 同一行 ID 后主主复制失败

标签 mysql linux database replication database-replication

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

Master A = Localsystem
Master B = Server

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

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

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

两者都在表中获得了下一个 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

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

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

最佳答案

你需要做的是在每个母版上使用不同的 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

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

关于mysql - 同一行 ID 后主主复制失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28169690/

相关文章:

MySQL Workbench 导出权限

MySQL 连接查询构造

php - 打开和关闭多个查询的 mysqli 连接

python - 如何在 Mountain Lion 上安装 MySQLdb

mysql - MySQL中使用游标更新表中的id值

linux - 将文本行通过管道传输到 linux 命令中

Linux监控——Nagios和nrpe

c++ - Epoll_wait 在关闭的文件描述符上返回事件

mysql - 数据仓库和生产数据库

database - 大型网站如何做分页?