Reference since the problem: www.zhihu.com/question/42… The answer is personally original
Set innodb_flush_log_at_trx_COMMIT to 1 and sync_binlog to 1. MySQL’s default configuration is double 1.
Innodb_flush_log_at_trx_commit is the configuration of innoDB engine and sync_binlog is the upper configuration of MySQL engine. Both control disk write policies.
MySQL innoDB engine after transaction commit:
- Binlog write memory
- Redo log Write memory
- The two configurations determine whether the two logs are flushed or not (call
fsync
) - Commit to complete
Innodb_flush_log_at_trx_commit: Flush policy of the redo log. The default value is 1
- if
innodb_flush_log_at_trx_commit
Set to 0:log buffer
Will write once per secondlog file
, andlog file
theflush
(Brush to disk) operation at the same time. In this mode, when a transaction commits,Write to disk is not actively triggeredThe operation; - if
innodb_flush_log_at_trx_commit
Set to 1: each time a transaction commitsMySQL
Will thelog buffer
Write data tolog file
And,flush
(swipe to a disk) - if
innodb_flush_log_at_trx_commit
Set to 2: each time a transaction commitsMySQL
Will write data to the log bufferlog file
, butflush
The (swipe to disk) operation is not simultaneous. In this mode,MySQL willExecute once per secondflush
(Brush to disk) operation.
Sync_binlog: indicates the disk flushing policy of binlog. The default value is 0
- If it is 0, MySQL does not sync to disk like the operating system does with other files, but depends on the operating system to flush
binary log
. sync_binlog = N (N>0)
MySQL inEvery N timeBinary logbinary log
, will be usedfdatasync()
The function writes its binary logbinary log
Synchronize to disk.