为什么mysqlbinlog显示[替换]行事件如下所示

时间:2016-10-19 07:50:43

标签: mysql mysqlbinlog

我们从

了解到
  

REPLACE的工作原理与INSERT完全相同,只是如果在一个旧行中   table与PRIMARY KEY或UNIQUE的新行具有相同的值   index,在插入新行之前删除旧行

当我使用没有删除权限的帐户执行替换时,它提醒我删除权限。

然而,当我使用mysqlbinlog查看binlog中的内容时,它让我很困惑。

一个例子如下:

binlog_format =行

root:test> show create table t3\G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) DEFAULT NULL,
  `c2` varchar(20) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 15 |    5 | bb   | NULL |
+----+------+------+------+
5 rows in set (0.01 sec)

案例1:

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 15 |    5 | bb   | NULL |
+----+------+------+------+
5 rows in set (0.01 sec)

root:test> 
root:test> replace into t3 (c1,c2,c3) values(5,'bb',3);
Query OK, 2 rows affected (0.01 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 16 |    5 | bb   |    3 |
+----+------+------+------+
5 rows in set (0.00 sec)

mysqlbinlog显示如下:

# at 15126
#161019 15:04:14 server id 22100  end_log_pos 15190 CRC32 0x9bab83d9    Update_rows: table id 234 flags: STMT_END_F
### UPDATE `test`.`t3`
### WHERE
###   @1=15
###   @2=5
###   @3='bb'
###   @4=NULL
### SET
###   @1=16
###   @2=5
###   @3='bb'
###   @4=3
# at 15190
#161019 15:04:14 server id 22100  end_log_pos 15221 CRC32 0x70a88be3    Xid = 177
COMMIT/*!*/;

mysqlbinlog不显示删除和插入。

案例2:auto_increment = 17

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 16 |    5 | bb   |    3 |
+----+------+------+------+
5 rows in set (0.00 sec)

root:test> replace into t3 (id,c1,c2,c3) values(17,5,'bb',3);
Query OK, 2 rows affected (0.01 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 17 |    5 | bb   |    3 |
+----+------+------+------+
5 rows in set (0.00 sec)

mysqlbinlog显示如下:

# at 15408
#161019 15:13:18 server id 22100  end_log_pos 15476 CRC32 0xe200a811    Update_rows: table id 234 flags: STMT_END_F
### UPDATE `test`.`t3`
### WHERE
###   @1=16
###   @2=5
###   @3='bb'
###   @4=3
### SET
###   @1=17
###   @2=5
###   @3='bb'
###   @4=3
# at 15476
#161019 15:13:18 server id 22100  end_log_pos 15507 CRC32 0xe02d7749    Xid = 179
COMMIT/*!*/;

mysqlbinlog不显示删除和插入。

案例3:auto_increment = 18

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 17 |    5 | bb   |    3 |
+----+------+------+------+
5 rows in set (0.00 sec)

root:test> replace into t3 (id,c1,c2,c3) values(17,5,'cc',4);  
Query OK, 2 rows affected (0.01 sec)

root:test> select * from t3;
+----+------+------+------+
| id | c1   | c2   | c3   |
+----+------+------+------+
|  1 |    1 | a    |    1 |
|  2 |    2 | a    |    1 |
|  8 | NULL | NULL |    1 |
| 14 |    4 | bb   | NULL |
| 17 |    5 | cc   |    4 |
+----+------+------+------+
5 rows in set (0.00 sec)

mysqlbinlog显示如下:

# at 15745
#161019 15:30:49 server id 22100  end_log_pos 15796 CRC32 0x258b633a    Write_rows: table id 234 flags: STMT_END_F
### DELETE FROM `test`.`t3`
### WHERE
###   @1=17
###   @2=5
###   @3='bb'
###   @4=3
### INSERT INTO `test`.`t3`
### SET
###   @1=17
###   @2=5
###   @3='cc'
###   @4=4
# at 15796
#161019 15:30:49 server id 22100  end_log_pos 15827 CRC32 0xcc4e1740    Xid = 183
COMMIT/*!*/;

mysqlbinlog显示删除和插入。

因此,似乎当主键值相同时,binlog使用delete + insert。如果主键值不相同,则binlog使用update。

为什么?

1 个答案:

答案 0 :(得分:0)

看起来您正在使用基于行的复制,而不是基于语句。

使用基于行的复制,您只能看到在主服务器上执行的更改效果。如果您运行update t3 set c3 = 1;来更改所有5行,则一条语句可能会导致多行更改。