每天进步一点点——mysql——mysqlbinlog

一、        简介

mysqlbinlog:用于查看服务器生成的二进制日志的工具。

二、       命令格式

mysqlbinlog 选项日志文件1
日志文件2

三、       常用参数

-d--database=name:指定数据库名称,只列出指定数据库相关操作

-o--offset=#:忽略掉日志中前N行命令

-r--result-file=name:将输出的文本格式日志输出到指定文件

-s--short-form:显示简单格式忽略掉一些信息

--set-charset=char-name:在输出为文本格式时,在文件第一行加上set names char-name,这个选项在某些情况下装载数据时非常有用。

--start-datetime=date --stop-datetime=date指定日期间各内的所有日志

--start-position=# --stop-position=#:指定位置时间间隔内的所有日志

四、       常用例子

1.     查看当前正在使用的二进制日志文件

mysql>show master status;

+--------------+----------+--------------+------------------+-------------------+

| File         | Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set |

+--------------+----------+--------------+------------------+-------------------+

| mysql.000007 |      120 |              |                  |                   |

+--------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

2.     不加任何参数,显示所有日志

[[email protected]]# mysqlbinlog mysql.000007

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=1*/;

/*!40019 [email protected]@session.max_insert_delayed_threads=0*/;

/*!50003 [email protected][email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#150717 11:01:43 server id 1  end_log_pos 120 CRC32 0xc3bad8fd  Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43

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

BINLOG ‘

F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y

usM=

‘/*!*/;

# at 120

#150717 11:05:54 server id 1  end_log_pos 219 CRC32 0x79fabcbc  Query  thread_id=24910 exec_time=0    error_code=0

use`test2`/*!*/;

SET TIMESTAMP=1437102354/*!*/;

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

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

[email protected]@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;

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

[email protected]@session.collation_database=DEFAULT/*!*/;

createtable t6(id int)

/*!*/;

# at 219

#150717 11:06:09 server id 1  end_log_pos 300 CRC32 0xd883c511  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102369/*!*/;

BEGIN

/*!*/;

# at 300

#150717 11:06:09 server id 1  end_log_pos 399 CRC32 0x4845ae22  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102369/*!*/;

insertinto t6 value(1)

/*!*/;

# at 399

#150717 11:06:09 server id 1  end_log_pos 430 CRC32 0x254bd95b  Xid = 75156

COMMIT/*!*/;

# at 430

#150717 11:06:12 server id 1  end_log_pos 511 CRC32 0x400701db  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102372/*!*/;

BEGIN

/*!*/;

# at 511

#150717 11:06:12 server id 1  end_log_pos 610 CRC32 0x4d494d54  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102372/*!*/;

insertinto t6 value(2)

/*!*/;

# at 610

#150717 11:06:12 server id 1  end_log_pos 641 CRC32 0xc0160921  Xid = 75157

COMMIT/*!*/;

# at 641

#150717 11:06:14 server id 1  end_log_pos 722 CRC32 0x722e009d  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102374/*!*/;

BEGIN

/*!*/;

# at 722

#150717 11:06:14 server id 1  end_log_pos 821 CRC32 0x46f08ed1  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102374/*!*/;

insertinto t6 value(3)

/*!*/;

# at 821

#150717 11:06:14 server id 1  end_log_pos 852 CRC32 0x6eccabd7  Xid = 75158

COMMIT/*!*/;

# at 852

#150717 11:06:36 server id 1  end_log_pos 933 CRC32 0x1d97f072  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

BEGIN

/*!*/;

# at 933

#150717 11:06:36 server id 1  end_log_pos 1034 CRC32 0xca5d7197         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

deletefrom t6 where id=3

/*!*/;

# at 1034

#150717 11:06:36 server id 1  end_log_pos 1065 CRC32 0xc36e2552         Xid = 75159

COMMIT/*!*/;

# at 1065

#150717 11:21:20 server id 1  end_log_pos 1166 CRC32 0x937167ae         Query  thread_id=24910 exec_time=0    error_code=0

use `jiaowu`/*!*/;

SET TIMESTAMP=1437103280/*!*/;

createtable t7(id int)

/*!*/;

# at 1166

#150717 11:22:03 server id 1  end_log_pos 1261 CRC32 0xb31be6d1         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437103323/*!*/;

truncatetable t7

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 [email protected]_COMPLETION_TYPE*/;

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=0*/;

1.     做一些操作

mysql>create table t6(id int);

Query OK, 0 rows affected (0.02 sec)

mysql>insert into t6 value(1);

Query OK, 1 row affected (0.00 sec)

mysql>insert into t6 value(2);

Query OK, 1 row affected (0.01 sec)

mysql>insert into t6 value(3);

Query OK, 1 row affected (0.01 sec)

mysql>delete from t6 where id=3;

Query OK, 1 row affected (0.00 sec)

2.        只看jiaowu库中的信息

[[email protected]]# mysqlbinlog mysql.000007 -d jiaowu

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;

/*!40019 [email protected]@session.max_insert_delayed_threads=0*/;

/*!50003 [email protected][email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#150717 11:01:43 server id 1  end_log_pos 120 CRC32 0xc3bad8fd  Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43

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

BINLOG ‘

F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y

usM=

‘/*!*/;

# at 120

# at 219

#150717 11:06:09 server id 1  end_log_pos 300 CRC32 0xd883c511  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102369/*!*/;

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

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

[email protected]@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 300

# at 399

#150717 11:06:09 server id 1  end_log_pos 430 CRC32 0x254bd95b  Xid = 75156

COMMIT/*!*/;

# at 430

#150717 11:06:12 server id 1  end_log_pos 511 CRC32 0x400701db  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102372/*!*/;

BEGIN

/*!*/;

# at 511

# at 610

#150717 11:06:12 server id 1  end_log_pos 641 CRC32 0xc0160921  Xid = 75157

COMMIT/*!*/;

# at 641

#150717 11:06:14 server id 1  end_log_pos 722 CRC32 0x722e009d  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102374/*!*/;

BEGIN

/*!*/;

# at 722

# at 821

#150717 11:06:14 server id 1  end_log_pos 852 CRC32 0x6eccabd7  Xid = 75158

COMMIT/*!*/;

# at 852

#150717 11:06:36 server id 1  end_log_pos 933 CRC32 0x1d97f072  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

BEGIN

/*!*/;

# at 933

# at 1034

#150717 11:06:36 server id 1  end_log_pos 1065 CRC32 0xc36e2552         Xid = 75159

COMMIT/*!*/;

# at 1065

#150717 11:21:20 server id 1  end_log_pos 1166 CRC32 0x937167ae         Query  thread_id=24910 exec_time=0    error_code=0

use `jiaowu`/*!*/;

SET TIMESTAMP=1437103280/*!*/;

createtable t7(id int)

/*!*/;

# at 1166

#150717 11:22:03 server id 1  end_log_pos 1261 CRC32 0xb31be6d1         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437103323/*!*/;

truncatetable t7

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 [email protected]_COMPLETION_TYPE*/;

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=0*/;

3.     忽略前三个操作

[[email protected]]# mysqlbinlog mysql.000007 -o 3

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=1*/;

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

/*!50003 [email protected][email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#150717 11:01:43 server id 1  end_log_pos 120 CRC32 0xc3bad8fd  Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43

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

BINLOG ‘

F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y

usM=

‘/*!*/;

# at 300

#150717 11:06:09 server id 1  end_log_pos 399 CRC32 0x4845ae22  Query  thread_id=24910 exec_time=0     error_code=0

use`test2`/*!*/;

SET TIMESTAMP=1437102369/*!*/;

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

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

[email protected]@session.collation_database=DEFAULT/*!*/;

insertinto t6 value(1)

/*!*/;

# at 399

#150717 11:06:09 server id 1  end_log_pos 430 CRC32 0x254bd95b  Xid = 75156

COMMIT/*!*/;

# at 430

#150717 11:06:12 server id 1  end_log_pos 511 CRC32 0x400701db  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102372/*!*/;

BEGIN

/*!*/;

# at 511

#150717 11:06:12 server id 1  end_log_pos 610 CRC32 0x4d494d54  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102372/*!*/;

insertinto t6 value(2)

/*!*/;

# at 610

#150717 11:06:12 server id 1  end_log_pos 641 CRC32 0xc0160921  Xid = 75157

COMMIT/*!*/;

# at 641

#150717 11:06:14 server id 1  end_log_pos 722 CRC32 0x722e009d  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102374/*!*/;

BEGIN

/*!*/;

# at 722

#150717 11:06:14 server id 1  end_log_pos 821 CRC32 0x46f08ed1  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102374/*!*/;

insertinto t6 value(3)

/*!*/;

# at 821

#150717 11:06:14 server id 1  end_log_pos 852 CRC32 0x6eccabd7  Xid = 75158

COMMIT/*!*/;

# at 852

#150717 11:06:36 server id 1  end_log_pos 933 CRC32 0x1d97f072  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

BEGIN

/*!*/;

# at 933

#150717 11:06:36 server id 1  end_log_pos 1034 CRC32 0xca5d7197         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

deletefrom t6 where id=3

/*!*/;

# at 1034

#150717 11:06:36 server id 1  end_log_pos 1065 CRC32 0xc36e2552         Xid = 75159

COMMIT/*!*/;

# at 1065

#150717 11:21:20 server id 1  end_log_pos 1166 CRC32 0x937167ae         Query  thread_id=24910 exec_time=0    error_code=0

use `jiaowu`/*!*/;

SET TIMESTAMP=1437103280/*!*/;

createtable t7(id int)

/*!*/;

# at 1166

#150717 11:22:03 server id 1  end_log_pos 1261 CRC32 0xb31be6d1         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437103323/*!*/;

truncatetable t7

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 [email protected]_COMPLETION_TYPE*/;

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

4.     将输出结果导出到文件

[[email protected]]# mysqlbinlog mysql.000007 -o 3 -r /root/test.sql

5.     将指定时间中的内容简单显示

[[email protected]]# mysqlbinlog mysql.000007    --start-datetime=‘2015/07/17 11:06:14‘--stop-datetime=‘2015/07/17 11:12:20‘

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=1*/;

/*!40019 [email protected]@session.max_insert_delayed_threads=0*/;

/*!50003 [email protected][email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#150717 11:01:43 server id 1  end_log_pos 120 CRC32 0xc3bad8fd  Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43

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

BINLOG ‘

F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y

usM=

‘/*!*/;

# at 641

#150717 11:06:14 server id 1  end_log_pos 722CRC32 0x722e009d  Query   thread_id=24910 exec_time=0     error_code=0

SET TIMESTAMP=1437102374/*!*/;

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

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

[email protected]@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;

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

[email protected]@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 722

#150717 11:06:14 server id 1  end_log_pos 821 CRC32 0x46f08ed1  Query  thread_id=24910 exec_time=0     error_code=0

use `test2`/*!*/;

SET TIMESTAMP=1437102374/*!*/;

insert into t6 value(3)

/*!*/;

# at 821

#150717 11:06:14 server id 1  end_log_pos 852 CRC32 0x6eccabd7  Xid = 75158

COMMIT/*!*/;

# at 852

#150717 11:06:36 server id 1  end_log_pos 933 CRC32 0x1d97f072  Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

BEGIN

/*!*/;

# at 933

#150717 11:06:36 server id 1  end_log_pos 1034 CRC32 0xca5d7197         Query  thread_id=24910 exec_time=0    error_code=0

SET TIMESTAMP=1437102396/*!*/;

delete from t6 where id=3

/*!*/;

# at 1034

#150717 11:06:36 server id 1  end_log_pos 1065CRC32 0xc36e2552         Xid = 75159

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 [email protected]_COMPLETION_TYPE*/;

/*!50530 [email protected]@SESSION.PSEUDO_SLAVE_MODE=0*/;=0*/;

注意:--start-datetime包含所选择时间,--stop-datatime不包含所选时间,如果只写前者则表明到日志最后结束,如果只写后者则表明从日志最开始时间开始

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 04:27:25

每天进步一点点——mysql——mysqlbinlog的相关文章

每天进步一点点——mysql——Percona XtraBackup(innobackupex)

一.  简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁).XtraBackup支持所有的Percona Server.MySQL.MariaDB和Drizzle. XtraBackup优势 : 1.无需停止数据库进行InnoDB热备 2.增量备份MySQL 3.流压缩到传输到其它服务器 4.能比较容易地创建主从同步 5.备份MySQL时不会增大服务器负载 安装Xt

MYSQL: mysqlbinlog读取二进制文件报错read_log_event()

一.问题描述mysql v 5.0.91[[email protected] bin]# ./mysqlbinlog /tmp/mysql-bin.000015 /!40019 SET @@session.max_insert_delayed_threads=0/;/!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0/;DELIMITER /!/;#at 4#180227 10:31:55 server id 1 end

MySQL mysqlbinlog 访问mysql-bin日志出错

问题 mysqlbinlog -v -v --base64-output=DECODE-ROWS mysql-bin.000166 | less ERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 111, event_type: 35 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@sess

MySQL mysqlbinlog

MySQL binlog日志记录了MySQL数据库从启用日志以来所有对当前数据库的变更.binlog日志属于二进制文件,我们可以从binlog提取出来生成可读的文本或者SQL语句来重建当前数据库以及根据需要实现时点恢复或不完全恢复. 应用实例机器为win7操作系统,起初操作mysql数据库,找初始化文件找了许久也犯了一些初级错误.针对这些问题本人总结了下二进制日志的操作,具体操作如下: 1. 查询二进制文件在data目录 登录Mysql数据据库输入:mysql命令行执行 show variabl

Mysql的binlog日志与mysqlbinlog命令

binlog相关 MySQL 的二进制日志 binlog 可以说是 MySQL 最重要的日志,它记录了所有的 DDL 和 DML 语句(除了数据查询语句select.show等),以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的.binlog 的主要目的是复制和恢复. Binlog日志的两个最重要的使用场景MySQL主从复制:MySQL Replication在Master端开启binlog,Master把它的二进制日志传递给slaves来达到master-sl

MySQL 日志

MySQL 日志 ================================================================================ 概述: ================================================================================ MySQL的日志分类: ★查询日志:general_log ★慢查询日志:log_slow_queries ★错误日志:log_error, log_

mysql的备份和还原

备份:副本 RAID1,RAID10:保证硬件损坏而不会业务终止,不能保证逻辑上的破坏 备份类型: 热备份,温备份和冷备份 热备份:读,写都不受影像 温备份:仅可以执行读操作 冷备份:离线备份:读写操作均中止 物理备份和逻辑备份 物理备份:复制数据文件: 逻辑备份:将数据导出至文本文件中: 完全备份,增量备份和差异备份: 完全备份:备份全部数据: 增量备份:仅备份上次完全备份或增量备份后变化的数据: 差异备份:仅备份上次完全备份以来变化的数据 在线:物理完全备份 还原: 备份什么: 数据 配置文

[转] 使用 MYSQLBINLOG 来恢复数据

使用 MYSQLBINLOG 来恢复数据 2009-04-05 12:47:05 标签:mysql mysqlbinlog 恢复 数据库 数据 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kerry.blog.51cto.com/172631/146259 BINLOG就是一个记录SQL语句的过程,和普通的LOG一样.不过只是她是二进制存储,普通的是十进制存储罢了.1.配置文件里要写的东西:[mysqld]log-bin=

mysql 恢复备份

log-bin=/var/lib/mysql/mysql-binloglog-bin-index = /var/lib/mysql/mysql-binlog mysqlbinlog --stop-date="2005-09-20 20:01:00" /var/lib/mysql/mysql-binlog.000001| mysql -u root -poracle