05-日志的读取
由于日志以二进制方式存储,不能直接读取,需要用mysqlbinlog工具来查看,语法如下:
shell> mysqlbinlog log-file;
mysqlbinlog 的用法在上一章中已经详细介绍过,这里不再赘述。下面以 STATEMENT 格式为例演示了二进制日志的读取过程。
(1)往测试表emp中插入两条测试记录。
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> insert into emp values(1,'z1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(1,'z2');
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
(2)使用mysqlbinlog工具进行日志查看,粗体字显示了步骤(1)中所做的操作。
[root@localhost mysql]# mysqlbinlog localhost-bin.000007
…
use test/!/;
SET TIMESTAMP=1186691584/!/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/!/;
SET @@session.sql_mode=0/!/;
/!\C gbk //!/;
SET @@session.character_set_client=28,@@session.collation_connection=28, @@session. collation_server=28/!/;
insert into emp values(1,'z1')/!/;
at 191
070810 4:33:07 server id 1 end_log_pos 284 Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1186691587/!/;
insert into emp values(1,'z2')/!/;
DELIMITER ;
End of log file
ROLLBACK / added by mysqlbinlog /;
/!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE/;
…
如果日志是ROW格式,则mysqlbinlog解析后是一堆无法读懂的字符,如下例所示:
…
BINLOG '
EBq/ShMBAAAAPwAAAK4EAAAAADoAAAAAAAAABm1lbWJlcgA CdDIACgMPDw/+CgsPAQwKJAAoAEAA
/gJAAAAA
EBq/ShgBAAAAtAAAAGIFAAAQADoAAAAAAAEACv////8A/A EAAAALYWxleDk5ODh5b3UEOXlvdSA3
AGMSenE8EgAA
此时可以加上-v或者-vv参数进行读取,读者可以自己尝试一下。