据我们所知


  REPLACE的工作方式与INSERT完全相同,不同之处在于,如果
  表具有与PRIMARY KEY或UNIQUE的新行相同的值
  索引,在插入新行之前删除旧行


当我使用没有删除权限的帐户执行替换操作时,它使我想起了删除权限。

但是,当我使用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不显示delete和insert。

情况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不显示delete和insert。

情况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显示delete和insert。

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

为什么?

最佳答案

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

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

关于mysql - 为什么mysqlbinlog显示[替换为]行事件,如下所示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40125273/

10-12 12:52