innodb_flush_log_at_trx_commit
-
當
innodb_flush_log_at_trx_commit=0
時, log buffer將每秒一次地寫入log file, 而且log file的flush(刷新到disk)操做同時進行. 此時, 事務提交是不會主動觸發寫入磁盤的操做.mysql -
當
innodb_flush_log_at_trx_commit=1
時(默認), 每次事務提交時, MySQL會把log buffer的數據寫入log file, 而且將log file flush(刷新到disk)中去.sql -
當
innodb_flush_log_at_trx_commit=2
時, 每次事務提交時, MySQL會把log buffer的數據寫入log file, 但不會主動觸發flush(刷新到disk)操做同時進行. 然而, MySQL會每秒執行一次flush(刷新到disk)操做安全
sync_binlog
-
當
sync_binlog=0
時(默認), 如os刷新其餘文件的機制同樣, MySQL不會刷新log buffer到disk中去, 而是依賴os機制刷新log buffer數據到binary log中.服務器 -
當
sync_binlog=1
時, MySQL在寫1次二進制日誌binary log時, 會使用fdatasync()函數將二進制binary log同步到disk中去.(安全性最高的配置)async -
當
sync_binlog=N(N>1)
時, MySQL在寫N次二進制日誌binary log時, 會使用fdatasync()函數將二進制binary log同步到disk中去.函數
結論
- 1)當innodb_flush_log_at_trx_commit和sync_binlog 都爲 1 時是最安全的,但也是最慢的一種方式。在mysqld 服務崩潰或者服務器主機crash的狀況下,binary log 只有可能丟失最多一個語句或者一個事務。
- 2)當innodb_flush_log_at_trx_commit設置爲0,該模式速度最快,但不太安全,mysqld進程的崩潰會致使上一秒鐘全部事務數據的丟失。
- 3)當innodb_flush_log_at_trx_commit設置爲2,該模式速度較快,也比0安全,只有在操做系統崩潰或者系統斷電的狀況下,上一秒鐘全部事務數據纔可能丟失。
對於訂單交易系統。推薦的作法是 innodb_flush_log_at_trx_commit=1 ,sync_binlog=1性能
對於非訂單交易系統。推薦的作法是 innodb_flush_log_at_trx_commit=2 ,sync_binlog=N (N爲500 或1000) spa
要求性能最快,推薦的作法是 innodb_flush_log_at_trx_commit=0 ,sync_binlog=0操作系統