查看binlog文件的2种方式

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1.使用show binlog events

a.获取binlog文件列表 
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000005 |      1288 | 
| mysql-bin.000006 |       120 | 
+------------------+-----------+
mysql>

b.查看当前正在写入的binlog文件
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000006
         Position: 120


c.查看指定binlog文件的内容语法:
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]

1.演示查看一个二进制文件中全部事务
注:binlog文件实际上是由一连串的event组成的一个组,即事务组。
mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000005' \G
*************************** 1. row ***************************  一个row代表一个事务组
   Log_name: mysql-bin.000005
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 120
       Info: Server ver: 5.6.29-log, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql-bin.000005
        Pos: 120
 Event_type: Query
  Server_id: 1
End_log_pos: 194
       Info: BEGIN

2.演示对一个二进制文件的事务做过滤查询
mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000005' FROM 194 LIMIT 2 \G;
*************************** 1. row ***************************
   Log_name: mysql-bin.000005
        Pos: 194
 Event_type: Query
  Server_id: 1
End_log_pos: 319
       Info: use `tpcc`; UPDATE warehouse SET w_ytd = w_ytd + 3232 WHERE w_id = 1
*************************** 2. row ***************************
   Log_name: mysql-bin.000005
        Pos: 319
 Event_type: Query
  Server_id: 1
End_log_pos: 458
       Info: use `tpcc`; UPDATE district SET d_ytd = d_ytd + 3232 WHERE d_w_id = 1 AND d_id = 3
rows in set (0.00 sec)
mysql>


2.使用mysqlbinlog查看binlog(转:http://blog.csdn.net/leshami/article/details/41962243)

a、提取指定的binlog日志  
# mysqlbinlog /opt/data/APP01bin.000001  
# mysqlbinlog /opt/data/APP01bin.000001|grep insert  
/*!40019 SET @@session.max_insert_delayed_threads=0*/;  
insert into tb values(2,'jack')  
  
b、提取指定position位置的binlog日志  
# mysqlbinlog --start-position="120" --stop-position="332" /opt/data/APP01bin.000001  
  
c、提取指定position位置的binlog日志并输出到压缩文件  
# mysqlbinlog --start-position="120" --stop-position="332" /opt/data/APP01bin.000001 |gzip >extra_01.sql.gz  
  
d、提取指定position位置的binlog日志导入数据库  
# mysqlbinlog --start-position="120" --stop-position="332" /opt/data/APP01bin.000001 | mysql -uroot -p  
  
e、提取指定开始时间的binlog并输出到日志文件  
# mysqlbinlog --start-datetime="2014-12-15 20:15:23" /opt/data/APP01bin.000002 --result-file=extra02.sql  
  
f、提取指定位置的多个binlog日志文件  
# mysqlbinlog --start-position="120" --stop-position="332" /opt/data/APP01bin.000001 /opt/data/APP01bin.000002|more  
  
g、提取指定数据库binlog并转换字符集到UTF8  
# mysqlbinlog --database=test --set-charset=utf8 /opt/data/APP01bin.000001 /opt/data/APP01bin.000002 >test.sql  
  
h、远程提取日志,指定结束时间   
# mysqlbinlog -urobin -p -h192.168.1.116 -P3306 --stop-datetime="2014-12-15 20:30:23" --read-from-remote-server mysql-bin.000033 |more  
  
i、远程提取使用row格式的binlog日志并输出到本地文件  
# mysqlbinlog -urobin -p -P3606 -h192.168.1.177 --read-from-remote-server -vv inst3606bin.000005 >row.sql


3.mysqlbinlog 查看row模式的binlog 
当bin-log的模式设置为row时(binlog_format=row),查看执行的sql时也稍微麻烦一点,默认解析出来的结果是这样的

-bash-4.1$ /opt/mysql/bin/mysqlbinlog    /opt/mysql/var/mysql-bin.000004|more
/*!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 /*!*/;
# at 4
#160614 23:18:20 server id 1  end_log_pos 120 CRC32 0xbb07feb7  Start: binlog v 4, server v 5.6.14-log created 160614 23:18:20 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
PCBgVw8BAAAAdAAAAHgAAAABAAQANS42LjE0LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA8IGBXEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAbf+
B7s=
'/*!*/;

完全看不懂,这时需要添加参数(--base64-output=decode-rows -v)对输出结果解码。


-bash-4.1$ /opt/mysql/bin/mysqlbinlog  --base64-output=decode-rows -v  /opt/mysql/var/mysql-bin.000004|more
/*!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 /*!*/;
# at 4
#160614 23:18:20 server id 1  end_log_pos 120 CRC32 0xbb07feb7  Start: binlog v 4, server v 5.6.14-log created 160614 23:18:20 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 120
#160615 10:46:11 server id 1  end_log_pos 195 CRC32 0x9579c284  Query   thread_id=5     exec_time=0     error_code=0
SET TIMESTAMP=1465958771/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
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=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 195
#160615 10:46:11 server id 1  end_log_pos 300 CRC32 0xfadf2614  Table_map: `dentist`.`ds_case_no_type` mapped to number 93
# at 300
#160615 10:46:11 server id 1  end_log_pos 690 CRC32 0x210669fe  Update_rows: table id 93 flags: STMT_END_F
### UPDATE `dentist`.`ds_case_no_type`
### WHERE
###   @1='736666419b3a40d9b384a03311baf2be'
###   @2='5'
###   @3=NULL
###   @4=NULL
###   @5='88'
###   @6='66'
###   @7='1500000000'
###   @8='1500000006'
###   @9=NULL
###   @10='0'
###   @11=NULL
###   @12='c9c8d05d4d3b44379598dde5d02f1552'
###   @13='2016-06-07 17:03:57'
###   @14='c9c8d05d4d3b44379598dde5d02f1552'
###   @15='2016-06-12 13:17:33'
###   @16='67e05761a61243b68e4b259386dc0dc8'
### SET
###   @1='736666419b3a40d9b384a03311baf2be'
###   @2='5'
###   @3=NULL
###   @4=NULL
###   @5='88'
###   @6='66'
###   @7='1500000000'
###   @8='1500000007'
###   @9=NULL
###   @10='0'
###   @11=NULL
###   @12='c9c8d05d4d3b44379598dde5d02f1552'
###   @13='2016-06-07 17:03:57'
###   @14='c9c8d05d4d3b44379598dde5d02f1552'
###   @15='2016-06-12 13:17:33'
###   @16='67e05761a61243b68e4b259386dc0dc8'
# at 690
#160615 10:46:11 server id 1  end_log_pos 721 CRC32 0x70d0f695  Xid = 699
COMMIT/*!*/;
# at 721



本文转自 sunrisenan 51CTO博客,原文链接:http://blog.51cto.com/sunrisenan/1909609

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11月前
|
SQL 安全 关系型数据库
需要binlog的场景下,“暴力”干掉历史binlog文件,尽情释放磁盘空间
需要binlog的场景下,“暴力”干掉历史binlog文件,尽情释放磁盘空间
|
关系型数据库 MySQL Shell
MySQL笔记 | 2.Docker下搭建MySQL&查看BinLog文件
这篇文章主要是在Docker下搭建一个MySQL环节,为了后续的数据库学习做准备。
|
SQL 存储 关系型数据库
MySQL 技术篇- linux下mysql数据库利用binlog文件进行数据回滚实例演示,binlog恢复数据库的两种方式
MySQL 技术篇- linux下mysql数据库利用binlog文件进行数据回滚实例演示,binlog恢复数据库的两种方式
411 0
MySQL 技术篇- linux下mysql数据库利用binlog文件进行数据回滚实例演示,binlog恢复数据库的两种方式
|
SQL MySQL 关系型数据库
手动注册binlog文件造成主从异常
一、问题来源 有一个朋友@水米田 问我,基于POSITION的主从。他做了如下的操作 将备份的一些binlog文件加入到了目录中 修改index文件,加入了这些binlog文件 flush binary logs 然后整个主从环境大量延迟。
1142 0
|
SQL NoSQL 关系型数据库
MYSQL 什么时候同步binlog文件以及它的临时文件在哪里?
原创:转载请注明出处 本文参考5.7官方文档以及对MYSQL进行trace和GDB,使用自制binlog解析工具 infobin 获取可以通过百度云盘 http://pan.
1227 0