DROP TABLE IF EXISTS may brake replication if slave has replication filters
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
MySQL Server |
Unknown
|
Unknown
|
|||
Percona Server moved to https://jira.percona.com/projects/PS |
Fix Released
|
Medium
|
Yura Sorokin | ||
5.5 |
Invalid
|
Undecided
|
Unassigned | ||
5.6 |
Fix Released
|
Medium
|
Yura Sorokin |
Bug Description
Copied from original bug description in mysql bug #77684 reported by FernandoL
Description:
If a replica has replication filters on a given database and DROP TABLE IF EXISTS is issued on a table that holds a key being used as Foreign Key by another table the statement will fail as expected with error:
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
however this will break replication on the replica with:
Last_SQL_Error: Query caused different errors on master and slave. Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217 ; Error on slave: actual message='no error', error code=0. Default database: 'db1'. Query: 'DROP TABLE IF EXISTS `table1` /* generated by server */'
How to repeat:
Setup master with:
binlog_
or
binlog_format=ROW
Setup replica with:
replicate-ignore-db = db1
replicate-
Then run on the master the following statements:
CREATE DATABASE `db1`;
USE `db1`;
CREATE TABLE `table1` (`ID` bigint(20) primary key) ENGINE=InnoDB;
CREATE TABLE `table2` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `DIVISION_ID` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`), KEY `FK_TABLE1_
DROP TABLE IF EXISTS `db1`.`table1`;
Suggested fix:
The problem seems to be related to the "USE" above as the following works as expected:
CREATE DATABASE `db1`;
CREATE TABLE `db1`.`table1` (`ID` bigint(20) primary key) ENGINE=InnoDB;
CREATE TABLE `db1`.`table2` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `DIVISION_ID` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`), KEY `FK_TABLE1_
DROP TABLE IF EXISTS `db1`.`table1`;
however if you add an USE `db1` after the CREATE DATABASE statement the replication error will follow.
tags: | added: upstream |
Verified.
mysql> show databases; ------- ------- + ------- ------- + ------- ------- +
+------
| Database |
+------
| information_schema |
| ardi |
| mysql |
| performance_schema |
| test |
+------
5 rows in set (0.00 sec)
mysql> show master status \g ------- -----+- ------- --+---- ------- ---+--- ------- ------- -+----- ------- ------- + ------- -----+- ------- --+---- ------- ---+--- ------- ------- -+----- ------- ------- + ------- -----+- ------- --+---- ------- ---+--- ------- ------- -+----- ------- ------- +
+------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------
| mysql-bin.000003 | 120 | | | |
+------
1 row in set (0.00 sec)
mysql> CREATE DATABASE `db1`;
Query OK, 1 row affected (0.01 sec)
mysql> USE `db1`;
Database changed
mysql> CREATE TABLE `table1` (`ID` bigint(20) primary key) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE `table2` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `DIVISION_ID` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`), KEY `FK_TABLE1_ DIVISION_ 1` (`DIVISION_ID`), CONSTRAINT `FK_TABLE1_ DIVISION_ 1` FOREIGN KEY (`DIVISION_ID`) REFERENCES `table1` (`ID`) ON DELETE CASCADE ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> DROP TABLE IF EXISTS `db1`.`table1`;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql>
mysql>