服務器生成的二進制日志文件寫成二進制格式。要想檢查這些文本格式的文件,應使用mysqlbinlog實用工具。
應這樣調用mysqlbinlog:
shell> mysqlbinlog [options] log-files...
例如,要想顯示二進制日志binlog.000003的內容,使用下面的命令:
shell> mysqlbinlog binlog.0000003
輸出包括在binlog.000003中包含的所有語句,以及其它信息例如每個語句花費的時間、客戶發出的線程ID、發出線程時的時間戳等等。
通常情況,可以使用mysqlbinlog直接讀取二進制日志文件并將它們用于本地MySQL服務器。也可以使用--read-from-remote-server選項從遠程服務器讀取二進制日志。
當讀取遠程二進制日志時,可以通過連接參數選項來指示如何連接服務器,但它們經常被忽略掉,除非你還指定了--read-from-remote-server選項。這些選項是--host、--password、--port、--protocol、--socket和--user。
還可以使用mysqlbinlog來讀取在復制過程中從服務器所寫的中繼日志文件。中繼日志格式與二進制日志文件相同。
在5.11.3節,“二進制日志”中詳細討論了二進制日志。
mysqlbinlog支持下面的選項:
· ---help,-?
顯示幫助消息并退出。
· ---database=db_name,-d db_name
只列出該數據庫的條目(只用本地日志)。
· --force-read,-f
使用該選項,如果mysqlbinlog讀它不能識別的二進制日志事件,它會打印警告,忽略該事件并繼續。沒有該選項,如果mysqlbinlog讀到此類事件則停止。
· --hexdump,-H
在注釋中顯示日志的十六進制轉儲。該輸出可以幫助復制過程中的調試。在MySQL 5.1.2中添加了該選項。
· --host=host_name,-h host_name
獲取給定主機上的MySQL服務器的二進制日志。
· --local-load=path,-l pat
為指定目錄中的LOAD DATA INFILE預處理本地臨時文件。
· --offset=N,-o N
跳過前N個條目。
· --password[=password],-p[password]
當連接服務器時使用的密碼。如果使用短選項形式(-p),選項和 密碼之間不能有空格。如果在命令行中--password或-p選項后面沒有 密碼值,則提示輸入一個密碼。
· --port=port_num,-P port_num
用于連接遠程服務器的TCP/IP端口號。
· --position=N,-j N
不贊成使用,應使用--start-position。
· --protocol={TCP | SOCKET | PIPE | -position
使用的連接協議。
· --read-from-remote-server,-R
從MySQL服務器讀二進制日志。如果未給出該選項,任何連接參數選項將被忽略。這些選項是--host、--password、--port、--protocol、--socket和--user。
· --result-file=name, -r name
將輸出指向給定的文件。
· --short-form,-s
只顯示日志中包含的語句,不顯示其它信息。
· --socket=path,-S path
用于連接的套接字文件。
· --start-datetime=datetime
從二進制日志中第1個日期時間等于或晚于datetime參量的事件開始讀取。datetime值相對于運行mysqlbinlog的機器上的本地時區。該值格式應符合DATETIME或TIMESTAMP數據類型。例如:
shell> mysqlbinlog --start-datetime="2004-12-25 11:25:56" binlog.000003
該選項可以幫助點對點恢復。
· --stop-datetime=datetime
從二進制日志中第1個日期時間等于或晚于datetime參量的事件起停止讀。關于datetime值的描述參見--start-datetime選項。該選項可以幫助及時恢復。
· --start-position=N
從二進制日志中第1個位置等于N參量時的事件開始讀。
· --stop-position=N
從二進制日志中第1個位置等于和大于N參量時的事件起停止讀。
· --to-last-logs,-t
在MySQL服務器中請求的二進制日志的結尾處不停止,而是繼續打印直到最后一個二進制日志的結尾。如果將輸出發送給同一臺MySQL服務器,會導致無限循環。該選項要求--read-from-remote-server。
· --disable-logs-bin,-D
禁用二進制日志。如果使用--to-last-logs選項將輸出發送給同一臺MySQL服務器,可以避免無限循環。該選項在崩潰恢復時也很有用,可以避免復制已經記錄的語句。注釋:該選項要求有SUPER權限。
· --user=user_name,-u user_name
連接遠程服務器時使用的MySQL用戶名。
· --version,-V
顯示版本信息并退出。
還可以使用--var_name=value選項設置下面的變量:
· open_files_limit
指定要保留的打開的文件描述符的數量。
可以將mysqlbinlog的輸出傳到mysql客戶端以執行包含在二進制日志中的語句。如果你有一個舊的備份,該選項在崩潰恢復時也很有用(參見5.9.1節,“數據庫備份”):
shell> mysqlbinlog hostname-bin.000001 | mysql
或:
shell> mysqlbinlog hostname-bin.[0-9]* | mysql
如果你需要先修改含語句的日志,還可以將mysqlbinlog的輸出重新指向一個文本文件。(例如,想刪除由于某種原因而不想執行的語句)。編輯好文件后,將它輸入到mysql程序并執行它包含的語句。
mysqlbinlog有一個--position選項,只打印那些在二進制日志中的偏移量大于或等于某個給定位置的語句(給出的位置必須匹配一個事件的開始)。它還有在看見給定日期和時間的事件后停止或啟動的選項。這樣可以使用--stop-datetime選項進行點對點恢復(例如,能夠說“將數據庫前滾動到今天10:30 AM的位置”)。
如果MySQL服務器上有多個要執行的二進制日志,安全的方法是在一個連接中處理它們。下面是一個說明什么是不安全的例子:
shell> mysqlbinlog hostname-bin.000001 | mysql # DANGER!!
shell> mysqlbinlog hostname-bin.000002 | mysql # DANGER!!
使用與服務器的不同連接來處理二進制日志時,如果第1個日志文件包含一個CREATE TEMPORARY TABLE語句,第2個日志包含一個使用該臨時表的語句,則會造成問題。當第1個mysql進程結束時,服務器撤銷臨時表。當第2個mysql進程想使用該表時,服務器報告 “不知道該表”。
要想避免此類問題,使用一個連接來執行想要處理的所有二進制日志中的內容。下面提供了一種方法:
shell> mysqlbinlog hostname-bin.000001 hostname-bin.000002 | mysql
另一個方法是:
shell> mysqlbinlog hostname-bin.000001 > /tmp/statements.sql
shell> mysqlbinlog hostname-bin.000002 >> /tmp/statements.sql
shell> mysql -e "source /tmp/statements.sql"
mysqlbinlog產生的輸出可以不需要原數據文件即可重新生成一個LOAD DATA INFILE操作。mysqlbinlog將數據復制到一個臨時文件并寫一個引用該文件的LOAD DATA LOCAL INFILE語句。由系統確定寫入這些文件的目錄的默認位置。要想顯式指定一個目錄,使用--local-load選項。
因為mysqlbinlog可以將LOAD
DATA INFILE語句轉換為LOAD
DATA LOCAL INFILE語句(也就是說,它添加了LOCAL),用于處理語句的客戶端和服務器必須配置為允許LOCAL操作。參見5.6.4節,“LOAD DATA LOCAL安全問題”。
警告:為LOAD DATA LOCAL語句創建的臨時文件不會自動刪除,因為在實際執行完那些語句前需要它們。不再需要語句日志后應自己刪除臨時文件。文件位于臨時文件目錄中,文件名類似original_file_name-#-#。
--hexdump選項可以在注釋中產生日志內容的十六進制轉儲:
shell> mysqlbinlog --hexdump master-bin.000001
上述命令的輸出應類似:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
# at 4
#051024 17:24:13 server id 1 end_log_pos 98
# Position Timestamp Type Master ID Size Master Pos Flags
# 00000004 9d fc 5c 43 0f 01 00 00 00 5e 00 00 00 62 00 00 00 00 00
# 00000017 04 00 35 2e 30 2e 31 35 2d 64 65 62 75 67 2d 6c |..5.0.15.debug.l|
# 00000027 6f 67 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |og..............|
# 00000037 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
# 00000047 00 00 00 00 9d fc 5c 43 13 38 0d 00 08 00 12 00 |.......C.8......|
# 00000057 04 04 04 04 12 00 00 4b 00 04 1a |.......K...|
# Start: binlog v 4, server v 5.0.15-debug-log created 051024 17:24:13
# at startup
ROLLBACK;
十六進制轉儲的輸出包含下面的元素:
· Position: The byte position within the log file.
· Timestamp: The event timestamp. In the example just shown, '9d fc 5c 43' is the representation of '051024 17:24:13' in hexadecimal.
· Type: The type of the log event. '0f' means that the example event is a FORMAT_DESCRIPTION_EVENT. The types are:
· 00 UNKNOWN_EVENT
· This event should never be present in the log.
· 01 START_EVENT_V3
· This indicates the start of a log file written by MySQL 4 or earlier.
· 02 QUERY_EVENT
· The most common type of events. These contain queries executed
· on the master.
· 03 STOP_EVENT
· Indicates that master has stopped.
· 04 ROTATE_EVENT
· Written when the master switches to a new log file.
· 05 INTVAR_EVENT
· Used mainly for AUTO_INCREMENT values and if the LAST_INSERT_ID()
· function is used in the statement.
· 06 LOAD_EVENT
· Used for LOAD DATA INFILE in MySQL 3.23.
· 07 SLAVE_EVENT
· Reserved for future use.
· 08 CREATE_FILE_EVENT
· Used for LOAD DATA INFILE statements. This indicates the start
· of execution of such a statement. A temporary file is created
· on the slave. Used in MySQL 4 only.
· 09 APPEND_BLOCK_EVENT
· Contains data for use in a LOAD DATA INFILE statement. The
· data is stored in the temporary file on the slave.
· 0a EXEC_LOAD_EVENT
· Used for LOAD DATA INFILE statements. The contents of the
· temporary file is stored in the table on the slave.
· Used in MySQL 4 only.
· 0b DELETE_FILE_EVENT
· Rollback of LOAD DATA INFILE statement. The temporary file
· should be deleted on slave.
· 0c NEW_LOAD_EVENT
· Used for LOAD DATA INFILE in MySQL 4 and earlier.
· 0d RAND_EVENT
· Used to send information about random values if the RAND()
· function is used in the query.
· 0e USER_VAR_EVENT
· Used to replicate user variables.
· 0f FORMAT_DESCRIPTION_EVENT
· This indicates the start of a log file written by MySQL 5 or later.
· 10 XID_EVENT
· Event indicating commit of XA transaction
· 11 BEGIN_LOAD_QUERY_EVENT
· Used for LOAD DATA statements in MySQL 5 and later.
· 12 EXECUTE_LOAD_QUERY_EVENT
· Used for LOAD DATA statements in MySQL 5 and later.
· 13 TABLE_MAP_EVENT
· Reserved for future use
· 14 WRITE_ROWS_EVENT
· Reserved for future use
· 15 UPDATE_ROWS_EVENT
· Reserved for future use
· 16 DELETE_ROWS_EVENT
· Reserved for future use
· Master ID: The server id of the master that created the event.
· Size: The size in bytes of the event.
· Master Pos: The position of the event in the original master log file.
· Flags: 16 flags.
· 01 LOG_EVENT_BINLOG_IN_USE_F
· Log file correctly closed (Used only in FORMAT_DESCRIPTION_EVENT)
· If this flag is set (if the flags are e.g. '01 00') in an
· FORMAT_DESCRIPTION_EVENT, then the log file has not been
· properly closed. Most probably because of a master crash (for
· example, due to power failure).
· 02 Reserved for future use.
· 04 LOG_EVENT_THREAD_SPECIFIC_F
· Set if the event is dependent on the connection it was
· executed in (example '04 00'), e.g. if the event uses
· temporary tables.
· 08 LOG_EVENT_SUPPRESS_USE_F
· Set in some circumstances when the event is not dependent on
· the current database
其它標志保留用于將來使用。
在以后的版本中十六進制轉儲輸出的格式可能會改變。