mysql查看binlog

mysqlbinlog简介

binlog又叫二进制日志文件,它会将mysql中所有修改数据库数据的Query以二进制的形式记录到日志文件中,如:create,insert,drop,update等;(对于select操作则不会被记录到binlog里,因为它并没有修改数据库的数据)。binlog一般存储在数据目录下,并且命名为:mysql-bin.*(这个可以在配置文件中修改my.cnf:log-bin=mysql-bin,就是文件名的前缀;mysqld在每个 binlog 名后面添加一个数字扩展名。每次启动服务器或刷新日志时增加文件的大小大于max_binlog_size,一个事务不会被拆分开)。

binlog主要是用于保证数据完整的,如主从备份,通过从binlog文件中读取操作来在salve机上进行同样的操作,保证主从备份,当然不可能每次都从开始的地方redo,所以每条记录都有一个时间截TIMESTAMP。

binglog的类型

binlog的格式有三种,这也反应了mysql的复制技术:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复制(mixed-based replication, MBR)。相应地,binlog的格式也有三种:STATEMENT,ROW,MIXED。

mysql>show variables like ‘binlog_format’ #查看binlog的格式

开启binlog日志

编辑器打开mysql配置文件
vi /etc/my.cnf
在[mysqld] 区块
设置/添加 log-bin=mysql-bin 确认是打开状态(值 mysql-bin 是日志的基本名或前缀名);
重启mysqld服务使配置生效

常用binlog日志操作命令


    1.查看binlog相关的配置
      mysql> show variables like 'log_%';
    2.查看所有binlog日志列表
      mysql> show master logs;
    3.查看master状态,即最后(最新)一个binlog日志的编号名称,及其最后一个操作事件pos结束点(Position)值
      mysql> show master status;
    4.刷新log日志,自此刻开始产生一个新编号的binlog日志文件
      mysql> flush logs;
      注:每当mysqld服务重启时,会自动执行此命令,刷新binlog日志;在mysqldump备份数据时加 -F 选项也会刷新binlog日志;
    5.重置(清空)所有binlog日志
      mysql> reset master;

查看binglog日志

mysqlbinlog
本地查看

基于开始/结束时间
shell> mysqlbinlog –start-datetime=’2013-09-10 00:00:00’ –stop-datetime=’2013-09-10 01:01:01’ -d 库名 二进制文件
基于pos值
shell> mysqlbinlog –start-postion=107 –stop-position=1000 -d 库名 二进制文件

远程查看

指定开始/结束时间,并把结果重定向到本地t.binlog文件中.
shell> mysqlbinlog -u username -p password -hl-db1.dba.beta.cn6.qunar.com -P3306
–read-from-remote-server –start-datetime=’2013-09-10 23:00:00’ –stop-datetime=’2013-09-10 23:30:00’ mysql-bin.000001 > t.binlog

登录mysql查看

mysql> show binlog events [IN ‘log_name’] [FROM pos] [LIMIT [offset,] row_count];

sample:

mysql> show binlog events in 'mabinbindeMacBook-Pro-2-bin.000014';
+------------------------------------+-----+----------------+-----------+-------------+---------------------------------------------------------+
| Log_name                           | Pos | Event_type     | Server_id | End_log_pos | Info                                                    |
+------------------------------------+-----+----------------+-----------+-------------+---------------------------------------------------------+
| mabinbindeMacBook-Pro-2-bin.000014 |   4 | Format_desc    |       140 |         123 | Server ver: 5.7.16-log, Binlog ver: 4                   |
| mabinbindeMacBook-Pro-2-bin.000014 | 123 | Previous_gtids |       140 |         154 |                                                         |
| mabinbindeMacBook-Pro-2-bin.000014 | 154 | Anonymous_Gtid |       140 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                    |
| mabinbindeMacBook-Pro-2-bin.000014 | 219 | Query          |       140 |         300 | BEGIN                                                   |
| mabinbindeMacBook-Pro-2-bin.000014 | 300 | Query          |       140 |         417 | use `test1`; delete from `test1`.`obj1` where `id`='4'  |
| mabinbindeMacBook-Pro-2-bin.000014 | 417 | Xid            |       140 |         448 | COMMIT /* xid=9 */                                      |
| mabinbindeMacBook-Pro-2-bin.000014 | 448 | Anonymous_Gtid |       140 |         513 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                    |
| mabinbindeMacBook-Pro-2-bin.000014 | 513 | Query          |       140 |         592 | BEGIN                                                   |
| mabinbindeMacBook-Pro-2-bin.000014 | 592 | Query          |       140 |         709 | use `blog`; delete from `blog`.`article` where `id`='1' |
| mabinbindeMacBook-Pro-2-bin.000014 | 709 | Xid            |       140 |         740 | COMMIT /* xid=39 */                                     |
| mabinbindeMacBook-Pro-2-bin.000014 | 740 | Rotate         |       140 |         805 | mabinbindeMacBook-Pro-2-bin.000015;pos=4                |
+------------------------------------+-----+----------------+-----------+-------------+---------------------------------------------------------+

参考资料

MySQL :: MySQL 5.7 Reference Manual :: 5.4.4 The Binary Log
MySQL :: MySQL 5.7 Reference Manual :: 4.6.7 mysqlbinlog — Utility for Processing Binary Log Files
MySQL的binlog日志
MySQL的binlog数据如何查看

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值