MySQL Binlog--MIXED模式下数据更新
在 Mixed 模式下,MySQL 会根据执行的每一条具体的 SQL 语句来区分对待记录的日志形式,也就是在 statement 和 row 之间选择一种。
如果SQL语句为UPDATE/DELETE等修改语句时,并不是所有操作都以ROW模式记录变更操作。
测试Demo:
## 创建测试表 CREATE TABLE TB001(ID INT AUTO_INCREMENT PRIMARY KEY,C1 INT,C2 INT); ## 插入测试数据 INSERT INTO TB001(C1,C2)VALUES(1,1); ## 更新数据 UPDATE TB001 SET C2=2 WHERE C1=1;
查看生成的BINLOG事件:
SHOW BINLOG EVENTS IN 'mysql-bin.000002' FROM 9150 LIMIT 100;
+------------------+-------+------------+-----------+-------------+--------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-------+------------+-----------+-------------+--------------------------------------------------------------------+ | mysql-bin.000002 | 9520 | Gtid | 6322494 | 9585 | SET @@SESSION.GTID_NEXT= '4f268bbc-f79e-11e8-8d9d-b8ca3af84795:39' | | mysql-bin.000002 | 9585 | Query | 6322494 | 9666 | BEGIN | | mysql-bin.000002 | 9666 | Intvar | 6322494 | 9698 | INSERT_ID=1 | | mysql-bin.000002 | 9698 | Query | 6322494 | 9809 | use `db001`; INSERT INTO TB001(C1,C2)VALUES(1,1) | | mysql-bin.000002 | 9809 | Xid | 6322494 | 9840 | COMMIT /* xid=1903 */ | | mysql-bin.000002 | 9840 | Gtid | 6322494 | 9905 | SET @@SESSION.GTID_NEXT= '4f268bbc-f79e-11e8-8d9d-b8ca3af84795:40' | | mysql-bin.000002 | 9905 | Query | 6322494 | 9986 | BEGIN | | mysql-bin.000002 | 9986 | Query | 6322494 | 10094 | use `db001`; UPDATE TB001 SET C2=2 WHERE C1=1 | | mysql-bin.000002 | 10094 | Xid | 6322494 | 10125 | COMMIT /* xid=1904 */ | +------------------+-------+------------+-----------+-------------+--------------------------------------------------------------------+
可以发现UPDATE操作被以STATEMENT格式进行记录。
====================================================
运维建议:
建议配置线上生产环境binlog_format=ROW,保证主从数据一致。