mysqlbinlog — Utility for Processing Binary Log Files

本文以 [email protected] 經典版本爲基礎,官方文檔 地址

binary logging format

  • Statement-based logging
  • Row-based logging

常用選項

–verbose, -v

Reconstruct row events and display them as commented SQL statements. If this option is given twice (by passing in either “-vv” or “–verbose --verbose”), the output includes comments to indicate column data types and some metadata, and row query log events if so configured

注:這應該是程序員看 binlog 最得力的選項啦,The combination of --base64-output=DECODE-ROWS and --verbose provides a convenient way to see row events only as SQL statements,更多請看 mysqlbinlog Row Event Display

–database=db_name, -d db_name

The effects of this option depend on whether the statement-based or row-based logging format is in use

注:按數據庫名篩選 binlog,僅支持指定一個,請確保 binlog_format=ROW,通過 show variables like 'binlog_format' 查看,否則你需要詳細看官方文檔,進而清楚你在做什麼

–start-datetime=datetime

Start reading the binary log at the first event having a timestamp equal to or later than the datetime argument

shell> mysqlbinlog --start-datetime="2005-12-25 11:25:56" binlog.000003

–start-position=N, -j N

Start reading the binary log at the first event having a position equal to or greater than N. This option applies to the first log file named on the command line

–stop-datetime=datetime

Stop reading the binary log at the first event having a timestamp equal to or later than the datetime argument

–stop-position=N

Stop reading the binary log at the first event having a position equal to or greater than N. This option applies to the last log file named on the command line

最好知道選項

–base64-output=value

  • AUTO
  • NEVER
  • DECODE-ROWS

注:慎用此選項,just used to suppress the BINLOG statements for row events,更多請看 mysqlbinlog Row Event Display

–raw

–read-from-remote-server

–result-file=name, -r name

–rewrite-db=‘from_name->to_name’

–stop-never

–to-last-log, -t

應用

You can pipe the output of mysqlbinlog into the mysql client to execute the events contained in the binary log. This technique is used to recover from a crash when you have an old backup

shell> mysqlbinlog binlog.000001 | mysql -u root -p
or
shell> mysqlbinlog binlog.[0-9]* | mysql -u root -p

If the statements produced by mysqlbinlog may contain BLOB values, these may cause problems when mysql processes them. In this case, invoke mysql with the --binary-mode option

You can also redirect the output of mysqlbinlog to a text file instead, if you need to modify the statement log first (for example, to remove statements that you do not want to execute for some reason). After editing the file, execute the statements that it contains by using it as input to the mysql program

shell> mysqlbinlog binlog.000001 > tmpfile
shell> ... edit tmpfile ...
shell> mysql -u root -p < tmpfile
shell> mysqlbinlog binlog.000001 binlog.000002 | mysql -u root -p

equal

shell> mysqlbinlog binlog.000001 >  /tmp/statements.sql
shell> mysqlbinlog binlog.000002 >> /tmp/statements.sql
shell> mysql -u root -p -e "source /tmp/statements.sql"

use the SHOW BINARY LOGS statement to see the current binary log names

Static and Live Backups

mysqlbinlog --read-from-remote-server --host=host_name --raw binlog.000130 binlog.000131 binlog.000132

mysqlbinlog --read-from-remote-server --host=host_name --raw --to-last-log binlog.000130
mysqlbinlog --read-from-remote-server --host=host_name --raw --stop-never binlog.000130

mysqldump + mysqlbinlog for Backup and Restore

強烈建議去官方文檔查看整個備份恢復流程

TODO

Example Backup and Recovery Strategy

Point-in-Time (Incremental) Recovery Using the Binary Log

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章