如果innodb_flush_log_at_trx_commit设置为0,log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行.该模式下,在事务提交的时候,不会主动触发写入磁盘的操作。
如果innodb_flush_log_at_trx_commit设置为1,每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去.
如果innodb_flush_log_at_trx_commit设置为2,每次事务提交时MySQL都会把log buffer的数据写入log file.但是flush(刷到磁盘)操作并不会同时进行。该模式下,MySQL会每秒执行一次 flush(刷到磁盘)操作。
二 性能
两个参数在不同值时对db的纯写入的影响表现如下:
测试场景1
innodb_flush_log_at_trx_commit=2
sync_binlog=1000
测试场景2
innodb_flush_log_at_trx_commit=1
sync_binlog=1000
测试场景3
innodb_flush_log_at_trx_commit=1
sync_binlog=1
测试场景4
innodb_flush_log_at_trx_commit=1
sync_binlog=1000
测试场景5
innodb_flush_log_at_trx_commit=2
sync_binlog=1000
| 场景 | TPS |
| 场景1 | 41000 |
| 场景2 | 33000 |
| 场景3 | 26000 |
| 场景4 | 33000 |
三 安全 当innodb_flush_log_at_trx_commit和sync_binlog 都为 1 时是最安全的,在mysqld 服务崩溃或者服务器主机crash的情况下,binary log 只有可能丢失最多一个语句或者一个事务。但是鱼与熊掌不可兼得,双11 会导致频繁的io操作,因此该模式也是最慢的一种方式。 当innodb_flush_log_at_trx_commit设置为0,mysqld进程的崩溃会导致上一秒钟所有事务数据的丢失。
当innodb_flush_log_at_trx_commit设置为2,只有在操作系统崩溃或者系统掉电的情况下,上一秒钟所有事务数据才可能丢失。 双1适合数据安全性要求非常高,而且磁盘IO写能力足够支持业务,比如订单,交易,充值,支付消费系统。双1模式下,当磁盘IO无法满足业务需求时 比如11.11 活动的压力。推荐的做法是 innodb_flush_log_at_trx_commit=2 ,sync_binlog=N (N为500 或1000) 且使用带蓄电池后备电源的缓存cache,防止系统断电异常。 四 小结 系统性能和数据安全是业务系统高可用稳定的必要因素。我们对系统的优化需要寻找一个平衡点,合适的才是最好的,根据不同的业务场景需求,可以将两个参数做组合调整,以便是db系统的性能达到最优化。 来自:http://blog.itpub.net/22664653/viewspace-1063134/
sync_binlog innodb_flush_log_at_trx_commit 浅析
标签: