MySQL兩階段交易認可

來源:互聯網
上載者:User

標籤:

參數介紹

innodb_flush_log_at_trx_commit

0: 每隔1s,系統後台線程刷log buffer,也就是把redo日誌刷盤,這裡會調用fsync,所以可能丟失最後1s的事務。

1: 每次commit時,刷redo日誌,確定fsync刷盤

2: 每次提交時,刷redo日誌到檔案系統,不調用fsync刷盤,5.6.6之前是每隔1s刷盤,之後的版本是通過參數innodb_flush_log_at_timeout設定,預設也是1s。所以也可能丟最後一秒的事務。如果有掉電保護群組件的話,可以開啟。

sync_binlog

表示每多少個sync事件觸發一次真正的binlog fsync刷盤,預設是1,表示每次commit時binlog都會fsync。

兩階段交易認可

這個兩階段交易認可不是分散式交易的兩階段交易認可,而是在開啟binlog之後,redo與binlog的兩階段交易認可。 兩階段交易認可,首先redo log prepare,然後寫binlog,最後redo log commit.

  • 如果redo log prepare之後,binlog之前宕機,則復原事務,日誌如下:
2015-07-29 17:03:18 21957 [Note] Starting crash recovery...2015-07-29 17:03:18 7ffff7fe4780  InnoDB: Starting recovery for XA transactions...2015-07-29 17:03:18 7ffff7fe4780  InnoDB: Transaction 35077 in prepared state after recovery2015-07-29 17:03:18 7ffff7fe4780  InnoDB: Transaction contains changes to 1 rows2015-07-29 17:03:18 7ffff7fe4780  InnoDB: 1 transactions in prepared state after recovery2015-07-29 17:03:18 21957 [Note] Found 1 prepared transaction(s) in InnoDB2015-07-29 17:03:18 21957 [Note] rollback xid ‘MySQLXid\1\0\0\0\0\0\0\0\6\0\0\0\0\0\0\0‘
  • 如果binlog寫入之後宕機,則recover事務。
2015-07-29 17:06:23 7ffff7fe4780  InnoDB: Starting recovery for XA transactions...2015-07-29 17:06:23 7ffff7fe4780  InnoDB: Transaction 35590 in prepared state after recovery2015-07-29 17:06:23 7ffff7fe4780  InnoDB: Transaction contains changes to 1 rows2015-07-29 17:06:23 7ffff7fe4780  InnoDB: 1 transactions in prepared state after recovery2015-07-29 17:06:23 22040 [Note] Found 1 prepared transaction(s) in InnoDB2015-07-29 17:06:23 22040 [Note] commit xid ‘MySQLXid\1\0\0\0\0\0\0\0\6\0\0\0\0\0\0\0‘2015-07-29 17:06:23 22040 [Note] Crash recovery finished.
什麼情況下會出現binlog寫入了,但是實際這條資料不存在庫中?

innodb_flush_log_at_trx_commit 為0, sync_binlog=1,此時redo log沒有刷盤,binlog刷盤了,recover的時候不會根據binlog恢複。

所以強烈建議這兩個參數都設定成1.

MySQL兩階段交易認可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.