mysql的binlog说明

主从复制依赖于二进制日志文件,简称为binlog

binlog里面有存放了偏移信息

mysql主库binlog信息查看命令

[root@master ~]#  mysql -u root -p123456   #登录到mysql里
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show master logs; #binlog列表查看,也可以直接到/data/mysql查看
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
| master-bin. | |
+-------------------+-----------+
rows in set (0.00 sec) mysql> show master status;  #记录目前的binlog+偏移信息
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin. | | | | | #398即是偏移
+-------------------+----------+--------------+------------------+-------------------+

创建一个库和表,观察日志

mysql> create database darren;
Query OK, row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| darren |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec)
mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_darren |
+------------------+
| test |
+------------------+

查看binlog日志

mysql> mysql> show binlog events in 'master-bin.000002';  #全部查看
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | create database darren |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | use `darren`; create table test (id int) |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------+
mysql> mysql> show binlog events in 'master-bin.000002' limit ; 只查看前两个语句
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
rows in set (0.00 sec) mysql> show binlog events in 'master-bin.000002' from ; #查看偏移量是398以后的语句
+-------------------+-----+----------------+-----------+-------------+------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+------------------------------------------+
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | create database darren |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | use `darren`; create table test (id int) |
+-------------------+-----+----------------+-----------+-------------+------------------------------------------+
mysql> mysql> show binlog events in 'master-bin.000002' from limit ; #查看偏移量为398的前一条
+-------------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+--------------------------------------+
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
+-------------------+-----+----------------+-----------+-------------+--------------------------------------+

直接使用命令查看二进制文件

[root@master ~]# cd /data/mysql/

[root@master mysql]# mysqlbinlog master-bin.000002

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x7ae93041 Start: binlog v , server v 5.7.-log created :: at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
z8scXQ8BAAAAdwAAAHsAAAABAAQANS43LjI2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADPyxxdEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AUEw6Xo=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0x81e643cc Previous-GTIDs
# [empty]
# at
# :: server id end_log_pos CRC32 0x0a3cc640 Anonymous_GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0xbd85ba24 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'
/*!*/;
# at
# :: server id end_log_pos CRC32 0x674f9414 Anonymous_GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0x2106de6f Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
create database darren
/*!*/;
# at
# :: server id end_log_pos CRC32 0xbf9b983e Anonymous_GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0x773a1598 Query thread_id= exec_time= error_code=
use `darren`/*!*/;
SET TIMESTAMP=/*!*/;
create table test (id int)
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

增删改查,然后查看binlog

mysql> insert into test values ();    #写入一个数据,有事务的提交
Query OK, row affected (0.01 sec)
mysql> select * from test; #查一个数据
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec) mysql> update mysql> update test set id ; #改一个数据,有事务的提交
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> select * from test;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)
mysql> delete from test; #删除数据,有事务的提交
Query OK, row affected (0.00 sec)
mysql> show binlog events in 'master-bin.000002' from ; #查看日志
+-------------------+------+----------------+-----------+-------------+------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+------+----------------+-----------+-------------+------------------------------------------+
| master-bin. | | Query | | | create database darren |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | use `darren`; create table test (id int) |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | BEGIN |
| master-bin. | | Table_map | | | table_id: (darren.test) |
| master-bin. | | Write_rows | | | table_id: flags: STMT_END_F |
| master-bin. | | Xid | | | COMMIT /* xid=44 */ |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | BEGIN |
| master-bin. | | Table_map | | | table_id: (darren.test) |
| master-bin. | | Update_rows | | | table_id: flags: STMT_END_F |
| master-bin. | | Xid | | | COMMIT /* xid=49 */ |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | BEGIN |
| master-bin. | | Table_map | | | table_id: (darren.test) |
| master-bin. | | Delete_rows | | | table_id: flags: STMT_END_F |
| master-bin. | | Xid | | | COMMIT /* xid=52 */ |
+-------------------+------+----------------+-----------+-------------+------------------------------------------+

#错误sql不会写入binlog

mysql主库binlog清空,测试教学环境用,线上环境慎用。或者初搭建mysql主从用

mysql> reset master;
Query OK, rows affected (0.01 sec) mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
+-------------------+-----------+
row in set (0.00 sec) mysql> show binlog events in 'master-bin.000001' ;
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+

mysql主库刷新binlog,一般来说也是测试环境用

mysql> flush logs;               #新增一个日志
Query OK, rows affected (0.01 sec) mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
| master-bin. | |
+-------------------+-----------+
rows in set (0.01 sec) mysql> use darren;
Database changed
mysql> insert into test values ();
Query OK, row affected (0.01 sec) mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
| master-bin. | |
+-------------------+-----------+
rows in set (0.00 sec)
mysql> show binlog events in 'master-bin.000001' ;
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Rotate | | | master-bin.;pos= |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+ mysql> show binlog events in 'master-bin.000002' ; #查看日志内容
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | BEGIN |
| master-bin. | | Table_map | | | table_id: (darren.test) |
| master-bin. | | Write_rows | | | table_id: flags: STMT_END_F |
| master-bin. | | Xid | | | COMMIT /* xid=62 */ |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+

测试数据删除,让两个数据库的内容保持一致

mysql> show binlog events in 'master-bin.000002 ^C;
mysql> drop database darren;
Query OK, row affected (0.02 sec) mysql> reset master;
Query OK, rows affected (0.01 sec)

mysql主从之binlog日志的更多相关文章

  1. mysql主从之binlog的工作模式

    一 三种模式介绍 1.1 查看mysql主库的binlog格式 binlog仅在主库设置即可,从库无需设置 binlog的默认方式为STATEMENT ( show variables like '% ...

  2. (转)Mysql数据库之Binlog日志使用总结

    使用mysqlbinlog提取二进制日志 原文:http://blog.csdn.net/leshami/article/details/41962243 MySQL binlog日志记录了MySQL ...

  3. mysql备份和bin-log日志

    总结]:mysql备份和bin-log日志 备份数据: mysqldump -uroot -p123456 test -l -F '/tmp/test.sql' -l:读锁(只能读取,不能更新) -F ...

  4. mysql 正确清理binlog日志的两种方法

    前言: MySQL中的binlog日志记录了数据库中数据的变动,便于对数据的基于时间点和基于位置的恢复,但是binlog也会日渐增大,占用很大的磁盘空间,因此,要对binlog使用正确安全的方法清理掉 ...

  5. Mysql数据库之Binlog日志使用总结

    binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...

  6. mysql三种binlog日志的理解

    最近,一直在纠结要不要改数据库binlog的日志格式,原先用的是row格式,导致数据库binlog日志较大,磁盘空间本来也不是很大,所以就想看看能不能改变binlog日志.在该binlog日志之前,先 ...

  7. binlog之二:怎么样安全删除mysql下的binlog日志

    删除binlog方法 第一种方法: mysql> show binary logs; 查看mysql bin-log日志,除了这个以外的,其它都可以使用删除.mysql> purge bi ...

  8. 脚本备份MySQL数据库和binlog日志

    用Mysqldump实现全库备份+binlog的数据还原 首先是为mysql做指定库文件的全库备份 vim mysqlbak.sh #!/bin/bash #定义数据库目录,要能找到mysqldump ...

  9. Docker下mysql容器开启binlog日志(保留7天)

    现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...

随机推荐

  1. UVa 10220 【大整数】

    uva 10220 可采用uva 623这道题求N!,再最后稍微改一下就好. 参考代码: } #include<cstdio> #include<cstring> #inclu ...

  2. Auto CAD 安装问题 “acad.exe - 系统错误 ”

    Auto CAD 安装不上,提示“cad装不上 提示无法启动此程序,因为计算机中丢失 ac1st16.dll” 解决方法: 我的电脑——>右键 属性——>高级选项卡(win7的是高级系统设 ...

  3. c#操作sqlite db3数据库

    首先添加引用 System.Data.SQLite.dll,引用只用添加这个,但SQLite.Interop.dll文件必须也和它同时放在Debug目录下 然后可用: SQLiteConnection ...

  4. H5页面IOS中键盘弹出导致点击错位的问题

    IOS在点击输入框弹出键盘  键盘回缩 后 定位没有相应改变  还有  textarea 也会弹出键盘 $("input").blur(function() { console.l ...

  5. @loj - 2339@ 「WC2018」通道

    目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 11328 年,C 国的科学家们研发了一种高速传送通道,可以在很短的 ...

  6. POJ3080 Blue Jeans 题解 KMP算法

    题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...

  7. 神经网络入门——8XOR感知器

    XOR 感知器     XOR 感知器就是一个这样的逻辑门:输入相同返回 0 ,输入不同返回 1.与之前的感知器不同,这里并不是线性可分的.要处理这类较为复杂的问题,我们需要把感知器连接起来. 我们用 ...

  8. rowStyle设置Bootstrap Table行样式

    日常开发中我们通常会用到隔行变色来美化表格,也会根据每行的数据显示特定的背景颜色,如果库存低于100的行显示红色背景 CSS样式 <style> .bg-blue { background ...

  9. element-ui-——el-uploadexcel导入

    布局文件:(选择文件放在了弹框内部——即点击导入按钮后弹框显示,先下载模板再选择文件点击提交按钮才上传) )) { this.$notify({ message: '数据导入成功', type: 's ...

  10. dell装系统

    Dell f2进入bios USB support改为enable Uefi 改为legacy F12可看到USB