innodb_flush_log_at_trx_commit
主要针对innodb的redo从log_buffer到log file的同步而设置的
如果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(刷到磁盘)操作。
sync_binlog
设置二进制日志从binlog_buffer中刷新到磁盘的频率。对MySQL系统来说,它是至关重要的参数,不仅影响到binlog对MySQL带来的性能损耗,而且还影响到MySQL中数据的完整性。
sync_binlog 的默认值是0,像操作系统刷其他文件的机制一样,MySQL不会同步到磁盘中去而是依赖操作系统来刷新binary log。
当sync_binlog =N (N>0) ,MySQL 在每写 N次 二进制日志binary log时,会使用fdatasync()函数将它的写二进制日志binary log同步到磁盘中去。
压测语句:
sysbench --test=/root/sysbench-0.5/sysbench/tests/db/oltp.lua --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=180 --mysql-user=root --mysql-socket=/tmp/mysqld.sock --mysql-password='' --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex run
OLTP test statistics:
queries performed:
read: 480872
write: 137392
other: 68696
total: 686960
transactions: 34348 (
190.78 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 618264 (3434.07 per sec.)
other operations: 68696 (381.56 per sec.)
Test execution summary:
total time: 180.0381s
total number of events: 34348
total time taken by event execution: 2880.2442s
per-request statistics:
min: 5.07ms
avg: 83.85ms
max: 290.99ms
approx. 95 percentile: 108.72ms
Threads fairness:
events (avg/stddev): 2146.7500/6.87
execution time (avg/stddev): 180.0153/0.01
场景2:
sync_binlog=1 & innodb_flush_log_at_trx_commit=1
OLTP test statistics:
queries performed:
read: 455504
write: 130144
other: 65072
total: 650720
transactions: 32536 (
180.71 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 585648 (3252.79 per sec.)
other operations: 65072 (361.42 per sec.)
Test execution summary:
total time: 180.0447s
total number of events: 32536
total time taken by event execution: 2880.1853s
per-request statistics:
min: 34.42ms
avg: 88.52ms
max: 163.85ms
approx. 95 percentile: 115.39ms
Threads fairness:
events (avg/stddev): 2033.5000/6.78
execution time (avg/stddev): 180.0116/0.02
场景3:sync_binlog=1000 & innodb_flush_log_at_trx_commit=1
OLTP test statistics:
queries performed:
read: 455420
write: 130120
other: 65060
total: 650600
transactions: 32530 (
180.69 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 585540 (3252.45 per sec.)
other operations: 65060 (361.38 per sec.)
Test execution summary:
total time: 180.0306s
total number of events: 32530
total time taken by event execution: 2879.9498s
per-request statistics:
min: 24.78ms
avg: 88.53ms
max: 175.68ms
approx. 95 percentile: 113.50ms
Threads fairness:
events (avg/stddev): 2033.1250/6.57
execution time (avg/stddev): 179.9969/0.02
场景4:
sync_binlog=1000 & innodb_flush_log_at_trx_commit=2
OLTP test statistics:
queries performed:
read: 491624
write: 140464
other: 70232
total: 702320
transactions: 35116 (
195.04 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 632088 (3510.76 per sec.)
other operations: 70232 (390.08 per sec.)
Test execution summary:
total time: 180.0430s
total number of events: 35116
total time taken by event execution: 2880.1055s
per-request statistics:
min: 26.98ms
avg: 82.02ms
max: 166.06ms
approx. 95 percentile: 105.86ms
Threads fairness:
events (avg/stddev): 2194.7500/7.55
execution time (avg/stddev): 180.0066/0.03
结论:
sync_binlog等于1,innodb_flush_log_at_trx_commit=1时数据最安全,但性能最差,TPS最低
sync_binlog>1,innodb_flush_log_at_trx_commit=2 时性能最好。