mysqlbinlog用於處理二進制的日志文件,如果想要查看這些日志文件的文本內容,就需要使用mysqlbinlog工具。
1.mysqlbinlog
命令的語法
shell > mysqlbinlog [options] logfile1 logfile2 ...
2.mysqlbinlog
的選項
-d, --database=name 僅顯示指定數據庫的轉儲內容。 -o, --offset=# 跳過前N行的日志條目。 -r, --result-file=name 將輸入的文本格式的文件轉儲到指定的文件。 -s, --short-form 使用簡單格式。 --set-charset=name 在轉儲文件的開頭增加'SET NAMES character_set'語句。 --start-datetime=name 轉儲日志的起始時間。 --stop-datetime=name 轉儲日志的截止時間。 -j, --start-position=# 轉儲日志的起始位置。 --stop-position=# 轉儲日志的截止位置。
3.示例
查看二進制日志文件所在的位置,方法如下:
mysql> show variables like '%log%'; +-----------------------------------------+--------------------------------------------+ ....省略.... | log_bin | OFF | | log_bin_basename | | | log_bin_index | | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | ....省略.... +-----------------------------------------+--------------------------------------------+
由以上輸出可見,log-bin的值為OFF,所以必須首先打開log-bin,方法如下:
vi /etc/my.cnf [mysqld] ....省略.... log-bin=bin-log # 等號后為log-bin文件的名稱前綴,名稱可以自定義。
轉儲log-bin:
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> truncate table t1; Query OK, 0 rows affected (0.00 sec) mysql> insert into t1 values(214); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (215); Query OK, 1 row affected (0.00 sec)
---------------------------------------------------------------------------------------------------------------------
# mysqlbinlog bin-log.000002 ....省略.... use `test`/*!*/; ....省略.... truncate table t1 /*!*/; # at 211 #160830 7:28:36 server id 1 end_log_pos 290 CRC32 0x1f3368bb Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1472513316/*!*/; BEGIN /*!*/; # at 290 #160830 7:28:36 server id 1 end_log_pos 390 CRC32 0xcf40a797 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1472513316/*!*/; insert into t1 values(214) /*!*/; # at 390 #160830 7:28:36 server id 1 end_log_pos 421 CRC32 0x9d9f8407 Xid = 11 COMMIT/*!*/; # at 421 #160830 7:28:48 server id 1 end_log_pos 500 CRC32 0x722de8f9 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1472513328/*!*/; BEGIN /*!*/; # at 500 #160830 7:28:48 server id 1 end_log_pos 601 CRC32 0x03e2087d Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1472513328/*!*/; insert into t1 values (215) /*!*/; # at 601 #160830 7:28:48 server id 1 end_log_pos 632 CRC32 0x062775e4 Xid = 12 COMMIT/*!*/; DELIMITER ; # End of log file +