MariaDB 10.2.10 Slave stops on duplicate key error

1k Views Asked by At

I've been setting up two Debian Stretch based MariaDB-Server in Master-Master-Replication.

Slave's replication config section is:

server-id       = 2226
auto_increment_increment = 1
auto_increment_offset   = 1
log_bin         = /var/tmp/mysql_binlog/mysql-bin.log
log_bin_index       = /var/tmp/mysql_binlog/mysql-bin.log.index
expire_logs_days    = 3
max_binlog_size         = 100M
relay_log       = /var/tmp/mysql_binlog/slave-relay.log
relay_log_index     = /var/tmp/mysql_binlog/slave-relay.log.index
log_slave_updates   = 1
replicate_annotate_row_events = 0
log_bin_trust_function_creators = 1

I am experiencing the following error:

show slave status\G;

          Relay_Log_File: slave-relay.032025
            Relay_Log_Pos: 14887746
    Relay_Master_Log_File: mysql-bin.001119
         Slave_IO_Running: Yes
               Last_Errno: 1062
               Last_Error: Error 'Duplicate entry '71899-single' for key 'PRIMARY'' on query. Default database: 'mydb'. Query: 'INSERT INTO document_reference
                        (document_reference_document_id, document_reference_type, document_reference_value)
                    VALUES (71899, "single", 0)'

but:

MariaDB [(none)]> select * from mydb.document_reference WHERE document_reference_document_id=71899;
Empty set (0.00 sec)

I've looked up the relay log file - there is only one insert statement.

Anyone has an idea what causes duplicate entry error on slave?

Added information:

Master settings:

auto_increment_increment     | 1     
auto_increment_offset        | 1     
binlog_format                | MIXED

Table definition:

CREATE TABLE "document_reference" (
"document_reference_document_id" int(10) unsigned NOT NULL,
"document_reference_type" enum('single,'multi') COLLATE utf8_unicode_ci     
NOT NULL DEFAULT 'single',
"document_reference_value" int(11) NOT NULL,
PRIMARY KEY ("document_reference_document_id","document_reference_type"))
1

There are 1 best solutions below

5
On

When having "dual Master", set

auto_increment_increment = 2  -- on both
auto_increment_offset = 1 on one Master, = 2 on the other

Also, server_id must be different on the two Masters (and on any Slaves). If they are not different, replication goes round and round between the Masters.

Otherwise, inserts on separate Masters can generate the same AUTO_INCREMENT values.

However, that does not answer the problem. (There is no auto_inc involved.) The only possible answer (I think) is that two of these rows: (71899, "single", ...) were inserted. The absence of evidence of such in the SELECT may have to do with DELETEs, deadlocks, etc. (So, I do not have a definite answer.)