ablog

不器用で落着きのない技術者のメモ

MySQL のバイナリログとInnoDB ログ

MySQLのバイナリログはメディアリカバリに使うもので、ディスク障害などの際に mysqldump でエクスポートしておいたデータをインポートしてバイナリログでロールフォーワードする。Oracle Database で言うと、 インポートがリストアで、バイナリログでのロールフォーワードがアーカイブログとREDOログを使ったロールフォーワードに当たる。Oracle Database が物理的なブロックレベルで行う宇野に対して、MySQL は論理的にSQLベースで行う点が異なる。
InnoDBログはインスタンスダウンした時にクラッシュリカバリでロールフォーワードに使われる(ロールバックにはUNDOログが使われる)。Oracle Database がREDOログでロールフォーワードしてUNDO表領域のロールバックセグメントでロールバックするのと同じ。

  • バイナリログ


バイナリログを使っている場合、--innodb_support_xa を 1 に設定していると、InnoDBログとバイナリログの一貫性が保証される。sync_binlog が 1 の場合、クラッシュリカバリ時にバイナリログを走査して truncate して、マスターでロールバックしたトランザクションはバイナリログから削除するようだ。これが残っていると、マスターでDMLは発行されたけどロールバックされたトランザクションがスレーブに連携されて永遠にコミットされないトランザクションになるからではないかと思う。

For example, if you are using InnoDB tables and the MySQL server processes a COMMIT statement, it writes many prepared transactions to the binary log in sequence, synchronizes the binary log, and then commits this transaction into InnoDB. If the server crashes between those two operations, the transaction is rolled back by InnoDB at restart but still exists in the binary log. Such an issue is resolved assuming --innodb_support_xa is set to 1, the default. Although this option is related to the support of XA transactions in InnoDB, it also ensures that the binary log and InnoDB data files are synchronized. For this option to provide a greater degree of safety, the MySQL server should also be configured to synchronize the binary log and the InnoDB logs to disk before committing the transaction. The InnoDB logs are synchronized by default, and sync_binlog=1 can be used to synchronize the binary log. The effect of this option is that at restart after a crash, after doing a rollback of transactions, the MySQL server scans the latest binary log file to collect transaction xid values and calculate the last valid position in the binary log file. The MySQL server then tells InnoDB to complete any prepared transactions that were successfully written to the to the binary log, and truncates the binary log to the last valid position. This ensures that the binary log reflects the exact data of InnoDB tables, and therefore the slave remains in synchrony with the master because it does not receive a statement which has been rolled back.

MySQL :: MySQL 5.6 Reference Manual :: 5.4.4 The Binary Log

参考

Webエンジニアのための データベース技術[実践]入門 (Software Design plus)

Webエンジニアのための データベース技術[実践]入門 (Software Design plus)

P.155

バックアップ間隔とリカバリにかかる時間


UPDATE文であればデータファイル自体のサイズには大きな影響がないことが多いので、リストアにかかる時間は大差ありません。したがって、復旧時間の差はこのUPDATE文の実行時間の差に近くなるでしょう。1ヶ月分のUPDATE文を実行するとなると、膨大な時間がかかる可能性があることに注意が必要です。バックアップの間隔を空け過ぎていたため、バイナリログを全部当て終わるのに2日かかった、という失敗事例もありました。


http://h50146.www5.hpe.com/products/software/oe/linux/summary/reference/pdfs/MySQL-backup.pdf