控制 MySQL 磁盤寫入策略 以及 數據安全性 的兩個關鍵參數: innodb_flush_log_at_trx_commit 和 sync_binlog
參數:innodb_flush_log_at_trx_commit
①如果設置為0,log buffer將每秒一次地寫入log file中,並且同時進行log file的flush(刷新到磁盤中)操作。此模式下,在事務提交時,不主動觸發寫入磁盤的操作;
②如果設置為1,此模式下,在每次事務提交時,mysql都會把log buffer的數據寫入log file中,並且同時進行log file的flush(刷新到磁盤中)操作;
③如果設置為2,此模式下,在每次事務提交時,mysql都會把log buffer的數據寫入log file中,但是不會同時進行log file的flush(刷新到磁盤中)操作,會每秒執行一次log file的flush(刷新到磁盤中)操作。
注意:由於進程調度策略問題,不能保證"每秒執行一次flush(刷新到磁盤中)操作"100%的"每一秒執行一次"。
參數:sync_binlog
①如果設置N=0,像操作系統刷新其他文件的機制一樣,mysql不會同步到磁盤中去,而是依賴操作系統來刷新binary log。
②如果設置N>0,mysql在每寫N次二進制日志binary log時,會調用fdatasync()函數將二進制日志binary log同步到磁盤中去。
注意:如果啟用了autocommit,那么每一個語句statement就會有一次寫操作;否則每個事務對應一個寫操作。
如圖:
性能:
①測試場景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
innodb_flush_log_at_trx_commit | sync_binlog | TPS |
1000 | 2 | 41000 |
1000 | 1 | 33000 |
1 | 1 | 26000 |
1000 | 1 | 33000 |
由此可見:
Ⅰ、當innodb_flush_log_at_trx_commit=1和sync_binlog=1時,寫入操作性能最差;
Ⅱ、當innodb_flush_log_at_trx_commit=2和sync_binlog=2時,寫入操作達到最高性能;
安全:
①當innodb_flush_log_at_trx_commit設置為0時,mysqld進程崩潰會導致上一秒所有事務數據丟失。
②當innodb_flush_log_at_trx_commit和sync_binlog都為1時最為安全,mysqld進程崩潰或者服務器crash的情況下,binary log只有可能最多丟失一個事務。
③當innodb_flush_log_at_trx_commit設置為2時,只有在服務器崩潰或斷電情況下,上一秒所有事務數據才可能丟失。