mysqlbinlog 工具

mysqlbinlog 工具说明:它可以审查binlog文件及中继日志文件的内容。除了在本地去读binlog文件,mysqlbinlog同样可以从其他服务器上远程读取binlog文件

基本用法:
启动一个客户端连接到master,执行下面的命令查看它们在二进制日志中如何结束的。

mysql> reset master;
Query OK, 0 rows affected (0.10 sec)

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      120 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

mysql> create table test(
    -> id int auto_increment,
    -> name char(100) not null,
    -> email char(100),
    -> password char(100),
    -> primary key(id)
    -> );
Query OK, 0 rows affected (0.07 sec)


mysql> set @password=PASSWORD('test');
Query OK, 0 rows affected (0.00 sec)


mysql> insert into test (name,email,password)
    -> values ('test','test@test.com',@password);
Query OK, 1 row affected (0.03 sec)


mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      697 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

使用mysqlbinlog装载binlog文件 master-bin.000001的内容

不知道 master-bin.000001文件位置可以执行下面命令查找 master-bin.000001文件位置
find / -name master-bin.000001

[root@root ~]# sudo mysqlbinlog --short-form  --force-if-open --base64-output=never /var/lib/mysql/master-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
ROLLBACK/*!*/;
use `test`/*!*/;
SET TIMESTAMP=1431574509/*!*/;
SET @@session.pseudo_thread_id=999999999/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table test(
id int auto_increment,
name char(100) not null,
email char(100),
password char(100),
primary key(id)
)
/*!*/;
SET TIMESTAMP=1431574594/*!*/;
BEGIN
/*!*/;
SET INSERT_ID=1/*!*/;
SET @`password`:=_utf8 0x2A39344244434542453139303833434532413146393539464430324639363443374146344346433239 COLLATE `utf8_general_ci`/*!*/;
SET TIMESTAMP=1431574594/*!*/;
insert into test (name,email,password)
values ('test','test@test.com',@password)
/*!*/;
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

--short-form:mysqlbinlog只打印被执行的SQL语句信息,忽略关于二进制日志中的事件的注释信息。当mysqlbinlog只用来回放事件到一个服务器时,这个选项时非常有用的。如果你想为查找问题而审计binlog文件,就需要这些注释而不能使用该选项。

--force-if-open:如果binlog没有被正确关闭,无论因为binlog文件仍在被写入或因为服务器系统崩溃,mysqlbinlog都将打印一条警告说这个binlog文件没有被正确关闭。这个选项防止打印警告

--base64-output=never:阻止mysqlbinlog打印base64-encoded事件。如果mysqlbinlog必须打印 base64-encoded事件,他将打印二进制日志的format description事件以显示其使用的编码。

mysqlbinlog 可以接受多个文件,如果给定多个binlog文件,它们将被按顺序处理。

由于binlog文件通常都很大,可以通过限制选项限定一定范围内的事件被打印。

--start--position=bytepos:转储的第一个事件的字节位置。如果几个binlog文件提供给mysqlbinlog的,这个位置将被解释为在序列中的第一个文件的位置。
                                     如果一个事件不在给定位置开始。mysqlbinlog仍然会尝试解释在那个位置开始的子节作为一个事件,这通常会导致垃圾输出。

--stop-position=bytepos:最后打印的事件的子节位置。如果没有事件在那个位置结束,最后打印的事件的位置将是在bytepos位置之前的事件。给定了多个binlog文件,该位置将是序列中最后一个文件的位置。

--start-datetime=datetime:只打印那些有时间戳或datetime后的事件。

--stop-datetime=datetime:只打印那些有时间戳或datetime前的事件。

读取远程文件

为处理远程读取binlog文件,包含有连接到服务器的主机和用户的read-from-remote-server选项,以及可选的端口和密码,用于replication slave权限的用户
$sudo mysqlbinlog --read-from-remote-server --host=master.example.com --base64-output=never --user=rple_user --password --start-position=xxx --stop-position=xxx  master-bin.000001

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28282660/viewspace-1654361/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28282660/viewspace-1654361/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值