(2) mysqlbinlog 精确提取sql语句

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

命令:

1
2
3
mysqlbinlog -d oldgirl mysql-bin.000046 
mysqlbinlog --start-position=442 --stop-position=549 mysql-bin.000046 
mysqlbinlog --start-datetime= "2016-04-11 21:42:45"  --stop-datetime= "2016-04-11 21:43:22"  mysql-bin.000046

1 mysqlbinlog 记录的是 所有库的log日志

如:

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#160411 21:00:35 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 160411 21:00:35 at startup

# Warning: this binlog is either in use or was not closed properly.

ROLLBACK/*!*/;

BINLOG '

858LVw8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAADznwtXEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==

'/*!*/;

# at 107

#160411 21:01:16 server id 1  end_log_pos 194   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379676/*!*/;

SET @@session.pseudo_thread_id=2/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=0/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

/*!\C latin1 *//*!*/;

SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

create database oldboy

/*!*/;

# at 194

#160411 21:03:34 server id 1  end_log_pos 344   Query   thread_id=2     exec_time=0     error_code=0

use oldboy/*!*/;

SET TIMESTAMP=1460379814/*!*/;

create table student(id int(4) auto_increment primary key, name varchar(16) not null)

/*!*/;

# at 344

#160411 21:04:53 server id 1  end_log_pos 414   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379893/*!*/;

BEGIN

/*!*/;

# at 414

#160411 21:04:53 server id 1  end_log_pos 442   Intvar

SET INSERT_ID=1/*!*/;

# at 442

#160411 21:04:53 server id 1  end_log_pos 549   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379893/*!*/;

insert into student (name) values ('andy')

/*!*/;

# at 549

#160411 21:04:53 server id 1  end_log_pos 576   Xid = 13

COMMIT/*!*/;

# at 576

#160411 21:04:57 server id 1  end_log_pos 646   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379897/*!*/;

BEGIN

/*!*/;

# at 646

#160411 21:04:57 server id 1  end_log_pos 674   Intvar

SET INSERT_ID=2/*!*/;

# at 674

#160411 21:04:57 server id 1  end_log_pos 781   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379897/*!*/;

insert into student (name) values ('jack')

/*!*/;

# at 781

#160411 21:04:57 server id 1  end_log_pos 808   Xid = 14

COMMIT/*!*/;

# at 808

#160411 21:42:02 server id 1  end_log_pos 897   Query   thread_id=3     exec_time=0     error_code=0

SET TIMESTAMP=1460382122/*!*/;

create database oldgirl

/*!*/;

# at 897

#160411 21:42:45 server id 1  end_log_pos 1048  Query   thread_id=3     exec_time=0     error_code=0

use oldboy/*!*/;

SET TIMESTAMP=1460382165/*!*/;

create table new_student(id int auto_increment primary key, name varchar(16) not null)

/*!*/;

# at 1048

#160411 21:43:22 server id 1  end_log_pos 1118  Query   thread_id=3     exec_time=0     error_code=0

SET TIMESTAMP=1460382202/*!*/;

BEGIN

/*!*/;

# at 1118

#160411 21:43:22 server id 1  end_log_pos 1146  Intvar

SET INSERT_ID=1/*!*/;

# at 1146

#160411 21:43:22 server id 1  end_log_pos 1257  Query   thread_id=3     exec_time=0     error_code=0

SET TIMESTAMP=1460382202/*!*/;

insert into new_student (name) values ('lili')

/*!*/;

# at 1257

#160411 21:43:22 server id 1  end_log_pos 1284  Xid = 22

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;





命令1:指定库

  -d, --database=name List entries for just this database (local log only).


举例:

1
mysqlbinlog -d oldgirl mysql-bin.000046 > oldgirl.sql

[root@master mysql]# cat oldgirl.sql 

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#160411 21:00:35 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 160411 21:00:35 at startup

# Warning: this binlog is either in use or was not closed properly.

ROLLBACK/*!*/;

BINLOG '

858LVw8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAADznwtXEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==

'/*!*/;

# at 107

# at 194

# at 344

#160411 21:04:53 server id 1  end_log_pos 414   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379893/*!*/;

SET @@session.pseudo_thread_id=2/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=0/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

/*!\C latin1 *//*!*/;

SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 414

#160411 21:04:53 server id 1  end_log_pos 442   Intvar

SET INSERT_ID=1/*!*/;

# at 442

# at 549

#160411 21:04:53 server id 1  end_log_pos 576   Xid = 13

COMMIT/*!*/;

# at 576

#160411 21:04:57 server id 1  end_log_pos 646   Query   thread_id=2     exec_time=0     error_code=0

SET TIMESTAMP=1460379897/*!*/;

BEGIN

/*!*/;

# at 646

#160411 21:04:57 server id 1  end_log_pos 674   Intvar

SET INSERT_ID=2/*!*/;

# at 674

# at 781

#160411 21:04:57 server id 1  end_log_pos 808   Xid = 14

COMMIT/*!*/;

# at 808

#160411 21:42:02 server id 1  end_log_pos 897   Query   thread_id=3     exec_time=0     error_code=0

SET TIMESTAMP=1460382122/*!*/;

create database oldgirl

/*!*/;

# at 897

# at 1048

#160411 21:43:22 server id 1  end_log_pos 1118  Query   thread_id=3     exec_time=0     error_code=0

SET TIMESTAMP=1460382202/*!*/;

BEGIN

/*!*/;

# at 1118

#160411 21:43:22 server id 1  end_log_pos 1146  Intvar

SET INSERT_ID=1/*!*/;

# at 1146

# at 1257

#160411 21:43:22 server id 1  end_log_pos 1284  Xid = 22

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;




命令2:指定位置范围

--start-position

--stop-position


我只想提取含有 insert into student (name) values ('andy') 这条语句的binlog

1
mysqlbinlog --start-position=442 --stop-position=549 mysql-bin.000046

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#160411 21:00:35 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 160411 21:00:35 at startup

# Warning: this binlog is either in use or was not closed properly.

ROLLBACK/*!*/;

BINLOG '

858LVw8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAADznwtXEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==

'/*!*/;

# at 442

#160411 21:04:53 server id 1  end_log_pos 549   Query   thread_id=2     exec_time=0     error_code=0

use oldboy/*!*/;

SET TIMESTAMP=1460379893/*!*/;

SET @@session.pseudo_thread_id=2/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=0/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

/*!\C latin1 *//*!*/;

SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

insert into student (name) values ('andy')

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;



命令三:用时间来指定(但是时间不准确 1s钟可很多语句)

--start-datetime

--stop-datetime  (语句不会包括在stop的这个时间点 是截止线)

1
mysqlbinlog --start-datetime= "2016-04-11 21:42:45"  --stop-datetime= "2016-04-11 21:43:22"  mysql-bin.000046

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#160411 21:00:35 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 160411 21:00:35 at startup

# Warning: this binlog is either in use or was not closed properly.

ROLLBACK/*!*/;

BINLOG '

858LVw8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAADznwtXEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==

'/*!*/;

# at 897

#160411 21:42:45 server id 1  end_log_pos 1048  Query   thread_id=3     exec_time=0     error_code=0

use oldboy/*!*/;

SET TIMESTAMP=1460382165/*!*/;

SET @@session.pseudo_thread_id=3/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

SET @@session.sql_mode=0/*!*/;

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

/*!\C latin1 *//*!*/;

SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

create table new_student(id int auto_increment primary key, name varchar(16) not null)

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;


本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1762780如需转载请自行联系原作者


cuizhiliang

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
SQL XML Oracle
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
538 0
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
|
6月前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
79 0
|
SQL 存储 缓存
一文搞懂MySQL中一条SQL语句是如何执行的
一文搞懂MySQL中一条SQL语句是如何执行的
|
SQL 存储 关系型数据库
MySQL的第一篇文章——了解数据库、简单的SQL语句
MySQL的第一篇文章——了解数据库、简单的SQL语句
|
SQL Python
Pandas与SQL的数据操作语句对照
Pandas与SQL的数据操作语句对照
169 0
Pandas与SQL的数据操作语句对照
|
SQL 数据库 Python
Python 数据库Insert语句脚本生成工具(SQL Server)
Python 数据库Insert语句脚本生成工具(SQL Server)
388 0
Python 数据库Insert语句脚本生成工具(SQL Server)
|
SQL 程序员 开发工具
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
545 0
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
|
SQL 数据库 数据安全/隐私保护
使用SQL语句 查询电话号码 加密显示
使用SQL语句 查询电话号码 加密显示
338 0
使用SQL语句 查询电话号码 加密显示
|
SQL 存储 安全
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率