mysql binlog_format 适时修改

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: --mysql允许在session或者global级别动态设置binlog_format的值,做在更新很多行时,可以设置 binlog_format = 'STATEMENT' 以加快数据应...
--mysql允许在session或者global级别动态设置binlog_format的值,做在更新很多行时,可以设置 binlog_format = 'STATEMENT' 以加快数据应用到备库上
 A session that makes many small changes to the database might want to use row-based logging.
 A session that performs updates that match many rows in the WHERE clause might want to use statement-based logging because it will be more efficient to log a few statements than many rows.
 Some statements require a lot of execution time on the master, but result in just a few rows being modified. It might therefore be beneficial to replicate them using row-based logging.

MariaDB [test]>  show variables like 'BINLOG_FORMAT';                        
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
MariaDB [test]> insert into t select seq,concat('rudy',seq) from seq_1_to_10;
Query OK, 10 rows affected (0.44 sec)
--此时binlog中的内容是sql形式的
[root@rudy_01 3307]# mysqlbinlog binlog.000010 
BEGIN
/*!*/;
# at 706
#160201 15:46:43 server id 11  end_log_pos 834  Query   thread_id=29    exec_time=0     error_code=0
SET TIMESTAMP=1454312803/*!*/;
insert into t select seq,concat('rudy',seq) from seq_1_to_10
/*!*/;
# at 834
#160201 15:46:43 server id 11  end_log_pos 861  Xid = 263
COMMIT/*!*/;

MariaDB [test]> set session binlog_format='ROW';
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]>  show variables like 'BINLOG_FORMAT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
MariaDB [test]> insert into t select seq,concat('rudy',seq) from seq_1_to_10;
Query OK, 10 rows affected (0.44 sec)

--修改 binlog_format = 'row' 后,其binlog的内容如下
[root@rudy_01 3307]# mysqlbinlog binlog.000010 
BEGIN
/*!*/;
# at 899
#160201 15:47:10 server id 11  end_log_pos 942  Table_map: `test`.`t` mapped to number 30
# at 942
#160201 15:47:10 server id 11  end_log_pos 1082         Write_rows: table id 30 flags: STMT_END_F
BINLOG '
fg2vVhMLAAAAKwAAAK4DAAAAAB4AAAAAAAEABHRlc3QAAXQAAgMPAh4AAw==
fg2vVhcLAAAAjAAAADoEAAAAAB4AAAAAAAEAAv/8AQAAAAVydWR5MfwCAAAABXJ1ZHky/AMAAAAF
cnVkeTP8BAAAAAVydWR5NPwFAAAABXJ1ZHk1/AYAAAAFcnVkeTb8BwAAAAVydWR5N/wIAAAABXJ1
ZHk4/AkAAAAFcnVkeTn8CgAAAAZydWR5MTA=
'/*!*/;
# at 1082
#160201 15:47:10 server id 11  end_log_pos 1109         Xid = 266
COMMIT/*!*/;


--注意在以下情况下就不允许动态修改binlog_format的值了
? From within a stored function or a trigger
? If the session is currently in row-based replication mode and has open temporary tables
--对于innodb的表,如果其事务隔离级别是READ COMMITTED or READ UNCOMMITTED,则binlog_format不能设置成STATEMENT
If you are using InnoDB tables and the transaction isolation level is READ COMMITTED or READ UNCOMMITTED, only row-based logging can be used. 
MariaDB [test]> set tx_isolation='READ-COMMITTED';
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]>  show variables like 'BINLOG_FORMAT';                        
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

MariaDB [test]> insert into t select seq,concat('rudy',seq) from seq_1_to_10;
ERROR 1665 (HY000): Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging. 
InnoDB is limited to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.

--在有临时表存在时,不建议改变binlog_format的值
Switching the replication format at runtime is not recommended when any temporary tables exist

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
17天前
|
存储 安全 关系型数据库
Mysql 的binlog日志的优缺点
MySQL的binlog(二进制日志)是一个记录数据库更改的日志文件,它包含了所有对数据库执行的更改操作,如INSERT、UPDATE和DELETE等。binlog的主要目的是复制和恢复。以下是binlog日志的优缺点: ### 优点: 1. **数据恢复**:当数据库出现意外故障或数据丢失时,可以利用binlog进行点恢复(point-in-time recovery),将数据恢复到某一特定时间点。 2. **主从复制**:binlog是实现MySQL主从复制功能的核心组件。主服务器将binlog中的事件发送到从服务器,从服务器再重放这些事件,从而实现数据的同步。 3. **审计**:b
|
26天前
|
SQL 关系型数据库 MySQL
mysql的binlog恢复数据
mysql的binlog恢复数据
27 0
|
2月前
|
存储 SQL 安全
浅谈MySQL Binlog
浅谈MySQL Binlog
45 0
|
2月前
|
SQL 存储 关系型数据库
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
1090 0
|
3月前
|
关系型数据库 MySQL 数据库
Mysql数据库redo log及binlog的写入
Mysql数据库redo log及binlog的写入
|
2月前
|
监控 关系型数据库 MySQL
MySQL Binlog实战:在生产环境中的应用与最佳实践【实战应用】
MySQL Binlog实战:在生产环境中的应用与最佳实践【实战应用】
35 0
|
2月前
|
SQL 监控 关系型数据库
MySQL Binlog深度解析:进阶应用与实战技巧【进阶应用】
MySQL Binlog深度解析:进阶应用与实战技巧【进阶应用】
44 0
|
2月前
|
存储 SQL 关系型数据库
Mysql专栏 - mysql、innodb存储引擎、binlog的工作流程
Mysql专栏 - mysql、innodb存储引擎、binlog的工作流程
79 0
|
3月前
|
缓存 安全 关系型数据库
MySQL sync_binlog配置
MySQL sync_binlog配置
|
3月前
|
SQL 存储 关系型数据库
MySQL-日志&二进制日志binlog初探
MySQL-日志&二进制日志binlog初探
55 0