mysql binlog提取sql_(2) mysqlbinlog 精确提取sql语句

本文介绍了如何使用mysqlbinlog工具提取特定数据库、指定位置范围或时间范围内的SQL语句。通过示例展示了如何使用-d参数指定数据库,--start-position和--stop-position参数精确到SQL语句的位置,以及--start-datetime和--stop-datetime参数按时间筛选。内容包括创建数据库、表及插入数据等操作。
摘要由CSDN通过智能技术生成

命令: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).

举例: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') 这条语句的binlogmysqlbinlog --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的这个时间点 是截止线)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*/;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值