mysql查看binlog日志内容

mysql的binlog日志位置可通过show variables like '%datadir%';查看,直接打开无法查看,要看其内容2个办法:


1、登录到mysql查看binlog
只查看第一个binlog文件的内容
mysql> show binlog events;


查看指定binlog文件的内容
mysql> show binlog events in 'mysql-bin.000002';
[python]  view plain  copy
  1. mysql> show binlog events in 'mysql-bin.000001';  
  2. +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+  
  3. | Log_name         | Pos  | Event_type  | Server_id | End_log_pos | Info                                                      |  
  4. +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+  
  5. | mysql-bin.000001 |    4 | Format_desc |       195 |         106 | Server ver: 5.1.73-log, Binlog ver: 4                     |  
  6. | mysql-bin.000001 |  106 | Query       |       195 |         198 | use `hadoop`; delete from user where id=3                 |  
  7. | mysql-bin.000001 |  198 | Intvar      |       195 |         226 | INSERT_ID=4                                               |  
  8. | mysql-bin.000001 |  226 | Query       |       195 |         332 | use `hadoop`; INSERT INTO user (id,name)VALUES (NULL,1)   |  
  9. | mysql-bin.000001 |  332 | Query       |       195 |         424 | use `hadoop`; delete from user where id=3                 |  
  10. | mysql-bin.000001 |  424 | Intvar      |       195 |         452 | INSERT_ID=5                                               |  
  11. | mysql-bin.000001 |  452 | Query       |       195 |         560 | use `hadoop`; INSERT INTO user (id,name)VALUES (NULL,222) |  
  12. | mysql-bin.000001 |  560 | Query       |       195 |         660 | use `hadoop`; DELETE FROM `user` WHERE (`id`='1')         |  
  13. | mysql-bin.000001 |  660 | Intvar      |       195 |         688 | INSERT_ID=6                                               |  
  14. | mysql-bin.000001 |  688 | Query       |       195 |         795 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('555')  |  
  15. | mysql-bin.000001 |  795 | Intvar      |       195 |         823 | INSERT_ID=7                                               |  
  16. | mysql-bin.000001 |  823 | Query       |       195 |         930 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('555')  |  
  17. | mysql-bin.000001 |  930 | Intvar      |       195 |         958 | INSERT_ID=8                                               |  
  18. | mysql-bin.000001 |  958 | Query       |       195 |        1065 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('555')  |  
  19. | mysql-bin.000001 | 1065 | Intvar      |       195 |        1093 | INSERT_ID=9                                               |  
  20. | mysql-bin.000001 | 1093 | Query       |       195 |        1200 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('555')  |  
  21. | mysql-bin.000001 | 1200 | Query       |       195 |        1300 | use `hadoop`; DELETE FROM `user` WHERE (`id`='9')         |  
  22. | mysql-bin.000001 | 1300 | Query       |       195 |        1400 | use `hadoop`; DELETE FROM `user` WHERE (`id`='8')         |  
  23. | mysql-bin.000001 | 1400 | Query       |       195 |        1500 | use `hadoop`; DELETE FROM `user` WHERE (`id`='7')         |  
  24. | mysql-bin.000001 | 1500 | Query       |       195 |        1600 | use `hadoop`; DELETE FROM `user` WHERE (`id`='4')         |  
  25. | mysql-bin.000001 | 1600 | Query       |       195 |        1700 | use `hadoop`; DELETE FROM `user` WHERE (`id`='5')         |  
  26. | mysql-bin.000001 | 1700 | Query       |       195 |        1800 | use `hadoop`; DELETE FROM `user` WHERE (`id`='6')         |  
  27. | mysql-bin.000001 | 1800 | Intvar      |       195 |        1828 | INSERT_ID=10                                              |  
  28. | mysql-bin.000001 | 1828 | Query       |       195 |        1935 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('555')  |  
  29. | mysql-bin.000001 | 1935 | Intvar      |       195 |        1963 | INSERT_ID=11                                              |  
  30. | mysql-bin.000001 | 1963 | Query       |       195 |        2070 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('666')  |  
  31. | mysql-bin.000001 | 2070 | Intvar      |       195 |        2098 | INSERT_ID=12                                              |  
  32. | mysql-bin.000001 | 2098 | Query       |       195 |        2205 | use `hadoop`; INSERT INTO `user` (`name`) VALUES ('777')  |  
  33. +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+  

或者

登录mysql安装的那台机器,找到my.cnf.我的my.cnf文件在etc地下,所有执行 cat /etc/my.cnf ,看到如图所示的binlogs存放位置 
这里写图片描述 
进入日志所在目录 cd /home/mysql/logs/binlogs, 输入ls -al 查看需要查看的binlogs产生的时间。

这里写图片描述


查看当前正在写入的binlog文件
mysql> show master status\G
[python]  view plain  copy
  1. mysql> show master status\G  
  2. *************************** 1. row ***************************  
  3.             File: mysql-bin.000002  
  4.         Position: 106  
  5.     Binlog_Do_DB:  
  6. Binlog_Ignore_DB: mysql,information_schema,performance_schema  
  7. 1 row in set (0.00 sec)  


获取binlog文件列表
mysql> show binary logs;
[python]  view plain  copy
  1. mysql> show binary logs;  
  2. +------------------+-----------+  
  3. | Log_name         | File_size |  
  4. +------------------+-----------+  
  5. | mysql-bin.000001 |      3548 |  
  6. | mysql-bin.000002 |       106 |  
  7. +------------------+-----------+  
  8. 2 rows in set (0.00 sec)  



2、用mysqlbinlog工具查看
基于开始/结束时间
[root@hd3 ~]# mysqlbinlog --start-datetime='2016-08-02 00:00:00' --stop-datetime='2016-08-03 23:01:01' -d hadoop /var/lib/mysql/mysql-bin.000001


如果需要查询2017-09-17 07:21:09到2017-09-19 07:59:50 数据库为geeRunner 的操作日志,输入如下命令将数据写入到一个备用的txt即可。

mysqlbinlog --no-defaults --database=geeRunner --start-datetime="2017-09-17 07:21:09" --stop-datetime="2017-09-19 07:59:50" binlogs.000080 > sanjiaomao.txt
  • 1

如果本地查询,输入命令

 mysqlbinlog --no-defaults --database=geeRunner --start-datetime="2017-09-17 07:21:09" --stop-datetime="2017-09-19 07:59:50" binlogs.000080 | more
  • 1

这里写图片描述

如果取下来查询,使用winscp工具,登录到db所在机器,将数据取出来。

如果需要过滤,只查询insert,update,delete的语句,可以这样写:

mysqlbinlog --no-defaults --database=raceEnroll  binlogs.000078 |grep update |more




基于pos值,注:hadoop是库名,/var/lib/mysql/mysql-bin.000001是二进制文件路径
[root@hd3 ~]# mysqlbinlog --start-position=2098 --stop-position=2205 -d hadoop /var/lib/mysql/mysql-bin.000001

[python]  view plain  copy
  1. /*!40019 SET @@session.max_insert_delayed_threads=0*/;  
  2. /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;  
  3. DELIMITER /*!*/;  
  4. # at 4  
  5. #160803 17:49:51 server id 195  end_log_pos 106         Start: binlog v 4, server v 5.1.73-log created 160803 17:49:51 at startup  
  6. # Warning: this binlog is either in use or was not closed properly.  
  7. ROLLBACK/*!*/;  
  8. BINLOG '  
  9. P76hVw/DAAAAZgAAAGoAAAABAAQANS4xLjczLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA  
  10. AAAAAAAAAAAAAAAAAAA/vqFXEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC  
  11. '/*!*/;  
  12. # at 2098  
  13. #160803 18:53:56 server id 195  end_log_pos 2205        Query   thread_id=1481  exec_time=115   error_code=0  
  14. use `hadoop`/*!*/;  
  15. SET TIMESTAMP=1470221636/*!*/;  
  16. SET @@session.pseudo_thread_id=1481/*!*/;  
  17. SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;  
  18. SET @@session.sql_mode=0/*!*/;  
  19. SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;  
  20. /*!\C utf8 *//*!*/;  
  21. SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;  
  22. SET @@session.lc_time_names=0/*!*/;  
  23. SET @@session.collation_database=DEFAULT/*!*/;  
  24. INSERT INTO `user` (`name`) VALUES ('777')  
  25. /*!*/;  
  26. DELIMITER ;  
  27. # End of log file  
  28. ROLLBACK /* added by mysqlbinlog */;  
  29. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;  
  • 22
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值