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
+