ITPub博客

首页 > Linux操作系统 > Linux操作系统 > mysql的binlog格式

mysql的binlog格式

原创 Linux操作系统 作者:myownstars 时间:2013-04-09 15:10:34 0 删除 编辑

日志格式

5.5默认为statement

可以在session级别修改,需要super权限

Set session binlog_format=’MIXED’;

Set session sql_log_bin=0; --0不记录log,默认为1

Master修改此参数不会影响到slave,但不停止复制就修改或只修改master可能会导致slave停止工作;

 

当master以statement/mixed运行且binlog有以row格式记录的事件时,

Slave可根据实际情况自动切换至row,处理完后再切换为原来的设置;

 

 

Statement风险

当某些sql为nondeterministic时,会导致主备库书记不一致;

http://dev.mysql.com/doc/refman/5.5/en/bugs.html

 

Create table … select

Create table部分以statement方式记录,select部分取决于binlog_format设置

  

 

Row风险

不会记录临时表操作,如果正在使用row且操作临时表,切换为statement会导致错误结果;

当使用InnoDB且事务隔离级别为read_commited/read_uncommited时,只能使用row,如果在运行时切换成statement会导致无法执行insert;

Row以块的形式记录进binlog,--binlog-row-event-max-size规定了其最大尺寸,默认为1024;

即便指定了row,某些sql仍会以statement形式记录,比如DDL;

slave不支持MyISAM的并发insert;

不使用checksums,在处理binlog时因网络/磁盘等导致的错误可能无法被检测,可在change master to中使用SSL用于checksuming;

当slave_exec_mode=idempotent(默认strict)时,因相应行不存在导致的错误无法被捕获,则master/slave有不同步的风险;

 

Mixed

当Mysql认定当前sql为unsafe时,以row格式记录

 

 

Unsafe sql

使用limit但不加order by的update/delete http://dev.mysql.com/doc/refman/5.5/en/replication-features-limit.html

调用load_file()/UUID()/USER()/RAND()/SLEEP()/SYSDATE()/master_pos_wait()等系统函数sql http://dev.mysql.com/doc/refman/5.5/en/replication-features-functions.html   

UDF

更新包含auto_increment列的表

引用日志表的sql—主备库的日志表可能不同

Load data infile—5.5.6起,该sql被认为不安全

Insert delayed

在事务中操作非事务表

引用系统变量

 

Auto_increment注意事项

基本上statement级别可以正确的复制auto_increment/last_insert_id()/timestamp,但要注意以下几点:

1

Slave/master上的表声明auto_increment的必须为同一列,5.5.30之前必须如此,BUG#12669186

2

由trigger/function引发的auto_increment更新被认定是不安全的,5.5 BUG#45677

3

使用alter table添加auto_increment列可能导致主备库数据顺序不一致,因为行序号依赖存储引擎和插入次序;

要消除此误差,必须重建表

Create tabe t2 like t1; --create … like会跳过原表的外键和data directory/index directory约束

Alter table t2 add id int auto_increment primary key;

Insert into t2 select * from t1 order by col1, col2; --order by必须列举出t1所有列

Drop t1;

Alter table t2 rename t1;

 

 

Statement与row格式的区别还体现在replicate-to-db/binlog-to-db上

前者应用于slave,后者作用于master;

这两个选项在statement和row级别复制时效果不同;

statement复制只检查use db条件,如果符合即复制;而row则检查实际表,即便use db不符合也可以复制;

案例

slave上设置replicate_do_db=sales;

master上执行

use prices;

update sales.january set amount=amount+1000;

如果binlog格式为statement不会执行,而row会执行;

 

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15480802/viewspace-758064/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: mysql常用函数
请登录后发表评论 登录
全部评论

注册时间:2010-03-18

  • 博文量
    375
  • 访问量
    3320795