mysql主从之binlog日志
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日志的更多相关文章
- mysql主从之binlog的工作模式
一 三种模式介绍 1.1 查看mysql主库的binlog格式 binlog仅在主库设置即可,从库无需设置 binlog的默认方式为STATEMENT ( show variables like '% ...
- (转)Mysql数据库之Binlog日志使用总结
使用mysqlbinlog提取二进制日志 原文:http://blog.csdn.net/leshami/article/details/41962243 MySQL binlog日志记录了MySQL ...
- mysql备份和bin-log日志
总结]:mysql备份和bin-log日志 备份数据: mysqldump -uroot -p123456 test -l -F '/tmp/test.sql' -l:读锁(只能读取,不能更新) -F ...
- mysql 正确清理binlog日志的两种方法
前言: MySQL中的binlog日志记录了数据库中数据的变动,便于对数据的基于时间点和基于位置的恢复,但是binlog也会日渐增大,占用很大的磁盘空间,因此,要对binlog使用正确安全的方法清理掉 ...
- Mysql数据库之Binlog日志使用总结
binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...
- mysql三种binlog日志的理解
最近,一直在纠结要不要改数据库binlog的日志格式,原先用的是row格式,导致数据库binlog日志较大,磁盘空间本来也不是很大,所以就想看看能不能改变binlog日志.在该binlog日志之前,先 ...
- binlog之二:怎么样安全删除mysql下的binlog日志
删除binlog方法 第一种方法: mysql> show binary logs; 查看mysql bin-log日志,除了这个以外的,其它都可以使用删除.mysql> purge bi ...
- 脚本备份MySQL数据库和binlog日志
用Mysqldump实现全库备份+binlog的数据还原 首先是为mysql做指定库文件的全库备份 vim mysqlbak.sh #!/bin/bash #定义数据库目录,要能找到mysqldump ...
- Docker下mysql容器开启binlog日志(保留7天)
现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...
随机推荐
- 2015 Objective-C 三大新特性
http://www.cocoachina.com/ios/20150617/12148.html Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的 ...
- Java练习 SDUT-1119_输入数字星期,输出英文(switch语句)
C语言实验--输入数字星期,输出英文(switch语句) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 从键盘上输入数 ...
- lattice planner 规划详解
大家好,我是来自百度智能驾驶事业群的许珂诚.今天很高兴能给大家分享Apollo 3.0新发布的Lattice规划算法. Lattice算法隶属于规划模块.规划模块以预测模块.routing模块.高精地 ...
- Hbase数据模型 行
- Shell echo 命令
Shell 的echo 指令与PHP的echo指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo &quo ...
- php parse_url linux 解析问题
耕毅 解析url函数parse_url() (PHP 4, PHP 5, PHP 7) parse_url — 解析 URL,返回其组成部分 mixed parse_url ( string $url ...
- Getting started with the basics of programming exercises_4
1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...
- @hdu - 6427@ Problem B. Beads
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 m 种不同颜色的珠子,颜色分别为 1~m,每一种颜色的珠子有 ...
- @NOI模拟2017.06.30 - T1@ Left
目录 @description@ @solution@ @accepted code@ @details@ @description@ JOHNKRAM 最近在研究排序网络,但他发现他不会制作比较器, ...
- @游记@ CQOI2019(十二省联考)
目录 @day - 0@ @day - 1@ @day - 2@ @后记@ 我只是来打酱油哒-- 顶多能进个 E 类继续打酱油. 原本还在互奶 A 队,结果现在--铁定进不了队啦. 对初中生的歧视啊 ...