(2) mysqlbinlog 精确提取sql语句

命令:

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') 这条语句的binlog

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的这个时间点 是截止线)

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*/;


你可能感兴趣的:(mysql,binlog)