MySQL系列:binlog日志详解(参数、操作、GTID、优化、故障演练)

简介

文档:https://dev.mysql.com/doc/refman/5.7/en/binary-log.html

binlog日志包含有关修改数据库内容的 SQL 语句的信息。此信息以描述修改的“事件”的形式存储

作用

主从复制必须依赖于binlog日志
备份恢复必须依赖于binlog日志

系统参数

–log_bin

启用binlog日志记录

Command-Line Format --log-bin=file_name
Type File name

binlog日志文件的默认位置是数据目录

启用binlog日志记录后,服务器会将所有更改数据的语句记录到binlog日志中,用于备份和复制。binlog日志是具有基本名称和数字扩展名的文件序列。

如果您为--log-bin 选项提供值,则该值将用作日志序列的基本名称。服务器通过将数字后缀添加到基本名称来按顺序创建binlog日志文件。在 MySQL 5.7 中,基本名称默认为 host_name-bin,使用主机名。建议您指定一个基本名称,以便您可以继续使用相同的binlog日志文件名,而不管默认名称的更改。

设置此选项会导致 log_bin系统变量设置为ON(或1),而不是基本名称。binlog日志文件基本名称和任何指定的路径都可用作 log_bin_basename系统变量。

(root@localhost) [(none)]> select @@log_bin;
+-----------+
| @@log_bin |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select @@log_bin_basename;
+-----------------------------------+
| @@log_bin_basename                |
+-----------------------------------+
| /data/mysql/3306/binlog/mysql-bin |
+-----------------------------------+
1 row in set (0.00 sec)

–server_id

指定服务器 ID

Command-Line Format --server-id=#
System Variable server_id
Scope Global
Dynamic Yes
Type Integer
Default Value 0
Minimum Value 0
Maximum Value 4294967295

在 MySQL 5.7 中, server_id如果启用了binlog日志记录,则必须指定,否则不允许启动服务器。

(root@localhost) [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

–binlog_format

设置binlog日志记录格式

Command-Line Format --binlog-format=format
System Variable binlog_format
Scope Global, Session
Dynamic Yes
Type Enumeration
Default Value ROW
Valid Values MIXED STATEMENT ROW

在 MySQL 5.7.7 之前,默认格式是 STATEMENT. 在 MySQL 5.7.7 及更高版本中,默认值为ROW

  • STATEMENT:(5.7.7以前)SBR(statement based replication),语句模式原封不动的记录当前DML
  • ROW:(5.7.7或更高 默认值) RBR(ROW based replication) :记录数据行的变化(用户看不懂,需要工具分析)
  • MIXED:(混合)MBR(mixed based replication)模式 :以上两种模式的混合

–sync-binlog(双一标准)

控制 MySQL 服务器将binlog日志同步到磁盘的频率

Command-Line Format --sync-binlog=#
System Variable sync_binlog
Scope Global
Dynamic Yes
Type Integer
Default Value 1
Minimum Value 0
Maximum Value 4294967295
  • 0:禁用 MySQL 服务器将binlog日志同步到磁盘。相反,MySQL 服务器依赖操作系统不时将binlog日志刷新到磁盘,就像它对任何其他文件所做的那样。此设置提供了最佳性能,但如果发生电源故障或操作系统崩溃,服务器可能已提交尚未同步到binlog日志的事务。
  • 1:在提交事务之前启用binlog日志到磁盘的同步。这是最安全的设置,但由于磁盘写入次数增加,可能会对性能产生负面影响。在电源故障或操作系统崩溃的情况下,binlog日志中丢失的事务仅处于准备状态。这允许自动恢复例程回滚事务,从而保证不会从binlog日志中丢失事务。
  • N, 其中是 0 或 1 以外的值:在收集到binlog日志提交组*N*后,将binlog日志同步到磁盘 。N在电源故障或操作系统崩溃的情况下,服务器可能已经提交了尚未刷新到binlog日志的事务。由于磁盘写入次数增加,此设置可能会对性能产生负面影响。较高的值会提高性能,但会增加数据丢失的风险。

–gtid-mode(gtid)

是否启用gtid

Command-Line Format --gtid-mode=MODE
System Variable gtid_mode
Scope Global
Dynamic Yes
Type Enumeration
Default Value OFF
Valid Values OFF``OFF_PERMISSIVE``ON_PERMISSIVE``ON

gtid-mode可取值:

  • OFF:不支持GTID事务,生成的是匿名事务,slave节点也只能应用匿名事务

  • OFF_PERMISSIVE:生成的是匿名事务,slave节点可以应用匿名事务和GTID事务

  • ON_PERMISSIVE:生成的是GTID事务,slave节点可以应用匿名事务和GTID事务(此步骤操作完成后,master节点 的binlog日志就会变成GTID模式)

  • ON:支持GTID事务,生成的是GTID事务,slave节点也只能应用GTID事务

–enforce-gtid-consistency(gtid)

是否允许违反gtid一致性

Command-Line Format --enforce-gtid-consistency[=value]
System Variable enforce_gtid_consistency
Scope Global
Dynamic Yes
Type Enumeration
Default Value OFF
Valid Values OFF``ON``WARN

enforce-gtid-consistency可选值:

  • OFF:允许所有事务违反 GTID 一致性。
  • ON: 不允许任何事务违反 GTID 一致性。
  • WARN:允许所有事务违反 GTID 一致性,但在这种情况下会生成警告

--enforce-gtid-consistency 没有值的设置被解释为将值设置为 ON。该变量还具有多个值的文本别名:0=OFF=FALSE1=ON=TRUE2=WARN

–expire-logs-day(优化参数)

自动删除binlog日志文件的天数。默认值为 0,表示“不自动删除

Command-Line Format --expire-logs-days=#
System Variable expire_logs_days
Scope Global
Dynamic Yes
Type Integer
Default Value 0
Minimum Value 0
Maximum Value 99
Unit days

–binlog_cache_size(优化参数)

在事务期间保存binlog日志更改的缓存大小

Command-Line Format --binlog-cache-size=#
System Variable binlog_cache_size
Scope Global
Dynamic Yes
Type Integer
Default Value 32768
Minimum Value 4096
Maximum Value (64-bit platforms) 18446744073709547520
Maximum Value (32-bit platforms) 4294963200
Unit bytes
Block Size 4096

–max_binlog_cache_size(优化参数)

如果一个事务需要超过这么多字节的内存,服务器会生成一个多语句事务需要超过 ‘max_binlog_cache_size’ 字节的存储错误。最小值为 4096。可能的最大值为 16EB(艾字节)。最大推荐值为4GB;这是因为 MySQL 目前无法处理大于 4GB 的binlog日志位置。

Command-Line Format --max-binlog-cache-size=#
System Variable max_binlog_cache_size
Scope Global
Dynamic Yes
Type Integer
Default Value 18446744073709547520
Minimum Value 4096
Maximum Value 18446744073709547520
Unit bytes
Block Size 4096

–max_binlog_size(优化参数)

命令行格式 --max-binlog-size=#
系统变量 max_binlog_size
范围 全球的
动态的 是的
类型 整数
默认值 1073741824
最小值 4096
最大值 1073741824
单元 字节
块大小 4096

如果写入binlog日志导致当前日志文件大小超过此变量的值,则服务器轮换binlog日志(关闭当前文件并打开下一个文件)。最小值为 4096 字节。最大值和默认值为 1GB。

事务以一个块的形式写入binlog日志,因此永远不会在多个binlog日志之间拆分。因此,如果您有大事务,您可能会看到大于 max_binlog_size.

如果max_relay_log_size为 0,则该值也 max_binlog_size适用于中继日志。

sql_log_bin

此变量控制是否为当前会话启用日志记录到binlog日志(假设binlog日志本身已启用)。默认值为 ON。要为当前会话禁用或启用binlog日志记录,请将会话 sql_log_bin变量设置为 OFFON

System Variable sql_log_bin
Scope Session
Dynamic Yes
Type Boolean
Default Value ON

日志操作

  • DDL :原封不动的记录当前DDL(statement语句方式)
  • DCL :原封不动的记录当前DCL(statement语句方式)
  • DML :只记录已经提交的事务DML

开启日志

mkdir /data/mysql/3306/binlog
chown -R mysql.mysql /data/mysql/3306/binlog
vim /etc/my.cnf
server_id=1                                          
log_bin=/data/mysql/3306/binlog
binlog_format=row

查看日志的开启情况

show variables like '%log_bin%';
(root@localhost) [(none)]> show variables like '%log_bin%';
+---------------------------------+-----------------------------------------+
| Variable_name                   | Value                                   |
+---------------------------------+-----------------------------------------+
| log_bin                         | ON                                      |
| log_bin_basename                | /data/mysql/3306/binlog/mysql-bin       |
| log_bin_index                   | /data/mysql/3306/binlog/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                     |
| log_bin_use_v1_row_events       | OFF                                     |
| sql_log_bin                     | ON                                      |
+---------------------------------+-----------------------------------------+
6 rows in set (0.00 sec)

查看一共多少个binlog

 show binary logs;
(root@localhost) [(none)]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |      1255 |
| mysql-bin.000002 |       217 |
| mysql-bin.000003 |       217 |
| mysql-bin.000004 |       217 |
| mysql-bin.000005 |       217 |
| mysql-bin.000006 |       217 |
| mysql-bin.000007 |       217 |
| mysql-bin.000008 |       217 |
| mysql-bin.000009 |       217 |
| mysql-bin.000010 |       217 |
| mysql-bin.000011 |      3873 |
+------------------+-----------+
11 rows in set (0.00 sec)

查看mysql正在使用的日志文件

show master status;
(root@localhost) [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000014 |      194 |              |                  | a469ece3-10cd-11ed-98cd-525400d5deea:1-32 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

event查看

show binlog events in ''
(root@localhost) [(none)]> flush logs;
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000012 |      194 |              |                  | a469ece3-10cd-11ed-98cd-525400d5deea:1-25 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> show binlog events in 'mysql-bin.000012';
+------------------+-----+----------------+-----------+-------------+-------------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                      |
+------------------+-----+----------------+-----------+-------------+-------------------------------------------+
| mysql-bin.000012 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.36-log, Binlog ver: 4     |
| mysql-bin.000012 | 123 | Previous_gtids |         1 |         194 | a469ece3-10cd-11ed-98cd-525400d5deea:1-25 |
+------------------+-----+----------------+-----------+-------------+-------------------------------------------+
2 rows in set (0.00 sec)

(root@localhost) [(none)]> create database binlogTest;
Query OK, 1 row affected (0.00 sec)

(root@localhost) [(none)]> use binlogTest;
Database changed
(root@localhost) [binlogTest]> create table t1(id int);
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [binlogTest]> show binlog events in 'mysql-bin.000012';
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                                               |
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000012 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.36-log, Binlog ver: 4                              |
| mysql-bin.000012 | 123 | Previous_gtids |         1 |         194 | a469ece3-10cd-11ed-98cd-525400d5deea:1-25                          |
| mysql-bin.000012 | 194 | Gtid           |         1 |         259 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:26' |
| mysql-bin.000012 | 259 | Query          |         1 |         371 | create database binlogTest                                         |
| mysql-bin.000012 | 371 | Gtid           |         1 |         436 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:27' |
| mysql-bin.000012 | 436 | Query          |         1 |         545 | use `binlogtest`; create table t1(id int)                          |
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
6 rows in set (0.00 sec)

详细内容查看

mysqlbinlog --base64-output=decode-rows -vv /data/binlog/mysql-bin.0000013

gtid

介绍

https://dev.mysql.com/doc/refman/5.7/en/replication-gtids.html

GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识,保存在mysql数据目录下的auto.cnf文件里。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
例如:

a469ece3-10cd-11ed-98cd-525400d5deea:31
[root->mcode-server->/data/mysql/3306/data]# cat auto.cnf 
[auto]
server-uuid=a469ece3-10cd-11ed-98cd-525400d5deea

开启gtid

vim /etc/my.cnf
gtid-mode=on
enforce-gtid-consistency=on
systemctl restart mysqld

查看gtid开启情况

show variables like '%gtid%';
(root@localhost) [(none)]> show variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name                    | Value                                     |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery      | ON                                        |
| enforce_gtid_consistency         | ON                                        |
| gtid_executed_compression_period | 1000                                      |
| gtid_mode                        | ON                                        |
| gtid_next                        | AUTOMATIC                                 |
| gtid_owned                       |                                           |
| gtid_purged                      | a469ece3-10cd-11ed-98cd-525400d5deea:1-31 |
| session_track_gtids              | OFF                                       |
+----------------------------------+-------------------------------------------+
8 rows in set (0.00 sec)

mysqlbinlog

[root->mcode-server->/data/mysql/3306/binlog]# mysqlbinlog --help

-?, --help                      # 显示帮助信息并退出
--base64-output=name            # binlog输出语句的base64解码 分为三类:默认是值auto ,仅打印base64编码的需要的信息,如row-based 事件和事件的描述
                                  信息。never 仅适用于不是row-based的事件 decode-rows   配合--verbose选项一起使用解码行事件到带注释的伪SQL语句
--bind-address=name             # 绑定的IP地址
--character-sets-dir=name       # 字符集文件的目录
-d, --database=name             # 仅列出此数据库的条目(仅限本地日志)
--rewrite-db=name               # 将行事件重写为指向,以便将其应用于新数据库
-#, --debug[=#]                 # 输出debug信息,用于调试。默认值为:d:t,/tmp/mysqldump.trace
--debug-check                   # 当程序退出时打印一些调试信息
--debug-info                    # 当程序退出时打印调试信息和内存和CPU使用统计信息
--default-auth=name             # 要使用的默认身份验证客户端插件
-D, --disable-log-bin           # 禁用binlog日志,若开启--to-last-log并发送输出文件到相同的mysql server。这种方式避免无限循环。在规避数据库崩
                                  溃恢复数据的时候有用。注意:需要super权限来使用此选项
-F, --force-if-open             # 若binlog非正常关闭,强制开启binlog,默认是on可使用--skip-force-if-open关闭
-f, --force-read                # 强制读取未知的binlog事件
-H, --hexdump                   # 使用十六进制和ASCII码导出输出的信息
-h, --host=name                 # 获取binlog的服务名
-i, --idempotent                # 通知服务器使用幂等模式应用行事件
-l, --local-load=name           # 准备LOAD DATA INFILE的本地临时文件指定目录
-o, --offset=#                  # 跳过前n个条目
-p, --password[=name]           # 连接到服务器的密码
--plugin-dir=name               # 客户端插件的目录
-P, --port=#                    # 用于连接的端口,0表示默认值。端口使用的优先级:my.cnf,$ MYSQL_TCP_PORT,/etc/services,内置默认值(3306)
--protocol=name                 # 用于连接的协议(tcp, socket, pipe, memory)
-R, --read-from-remote-server   # 从MySQL服务器读取binlog日志,是read-from-remote-master = BINLOG-DUMP-NON-GTIDS的别名。
--read-from-remote-master=name  
--raw                           # 配合参数-R一起使用,输出原始的binlog数据而不是SQL语句
-r, --result-file=name          # 输出指定的文件,和--raw一起使用,此时是数据文件的前缀
--secure-auth                   # 如果客户端使用旧的(4.1.1之前的)协议,则拒绝连接到服务器
--server-id=#                   # 提取给定id的服务器创建的binlog条目                
--server-id-bits=#              # 设置server-id中的有效位数
--set-charset=name              # 添加'SET NAMES character_set' 到输出
-s, --short-form                # 仅适用于常规查询,没有额外的信息和row-based事件信息。仅用于测试,不使用于生产环境。如果你想抑制
                                  base64-output,考虑使用--base64-output = never代替
-S, --socket=name               # 连接时使用的socket文件
--ssl-mode=name                 # SSL连接模式
--ssl-ca=name                   # PEM格式的CA文件
--ssl-capath=name               # CA目录
--ssl-cert=name                 # PEM格式的X509证书
--ssl-cipher=name               # 要使用的SSL密码
--ssl-key=name                  # PEM格式的X509密钥
--ssl-crl=name                  # 证书吊销列表
--ssl-crlpath=name              # 证书吊销列表路径
--tls-version=name              # 要使用的TLS版本,允许值为:tlsv1、tlsv1.1
--start-datetime=name           # binlog文件读取的起始时间点,可接受datetime和timestamp类型,格式2004-12-25 11:25:56
-j, --start-position=#          # 在位置等于或大于 的第一个事件处开始读取binlog日志 *`N`*
--stop-datetime=name            # binlog文件读取的结束时间点
--stop-never                    # 等待来自服务器的更多数据,而不是在最后一个日志结束时停止。隐式地设置--to-last-log ,但不是在最后一个日志结
                                  束时停止而是继续等待直到服务器断开连接
--stop-never-slave-server-id=#  # 从服务器server_id使用--read-from-remote-server --stop-never。该选项不能和--connection-server-id一起使用
--connection-server-id=#        # 从服务器server_id使用--read-from-remote-server,该选项不能和--stop-never-slave-server-id一起使用
--stop-position=#               # 
-t, --to-last-log               # 和-r一起使用,不会在请求的binlog结尾处停止,而是继续打印,直到mysql服务器的最后一个binlog结束。如果将输出发
                                  送到同一个MySQL服务器,可能会导致无休止的循环
-u, --user=name                 # 连接到服务器用户名
-v, --verbose                   # 重新构建伪SQL语句的行信息输出,-v -v会增加列类型的注释信息
-V, --version                   # 打印版本信息
--open-files-limit=#            # 打开文件的限制,用于保留文件描述符以供此程序使用
-c, --verify-binlog-checksum    # 验证binlog的事件信息
--binlog-row-event-max-size=#   # 指定基于行的binlog的大小,改值必须是256的倍数
--skip-gtids                    # 不要保留全局事务标识符,而是让服务器像执行新事务一样执行这些事务。
--include-gtids=name            # 打印提供了全局事务标识符的事件
--exclude-gtids=name            # 打印所有事件,但提供全局事务标识符的事件除外

–base64-output=name

binlog输出语句的base64解码 分为三类:

auto(默认) ,仅打印base64编码的需要的信息,如row-based 事件和事件的描述信息。

never 仅适用于不是row-based的事件

decode-rows 配合–verbose选项一起使用解码行事件到带注释的伪SQL语句

mysqlbinlog --base64-output=never mysql-bin.000012
mysqlbinlog --base64-output=decode-rows -v  mysql-bin.000012

-d,–database=name

仅列出此数据库的条目(仅限本地日志)

mysqlbinlog -d binlogTest  mysql-bin.000012

-r,–result-file=name

将文本输出写入的文件

mysqlbinlog mysql-bin.000012 -r ./result.sql

–start-datetime=name

binlog文件读取的起始时间点,可接受datetime和timestamp类型,格式2004-12-25 11:25:56

mysqlbinlog --start-datetime='2022-08-19 14:10-24'  mysql-bin.000012

–stop-datetime=name

binlog文件读取的结束时间点

mysqlbinlog --stop-datetime='2022-08-19 14:10-24'  mysql-bin.000012

-j,–start-position=#

在位置等于或大于 的第一个事件处开始读取binlog日志 N

 mysqlbinlog --start-position=1410 mysql-bin.000012 --base64-output=decode-rows -v

–stop-position=#

在位置等于或大于 的第一个事件处停止读取binlog日志 N

mysqlbinlog --start-position=1410 --stop-position=1488 mysql-bin.000012 --base64-output=decode-rows -v

-v,–verbose

重构行事件并将它们显示为注释的 SQL 语句。如果此选项被给出两次(通过传入“-vv”或“–verbose --verbose”),则输出包括用于指示列数据类型和一些元数据的注释,以及行查询日志事件(如果已配置)。

mysqlbinlog mysql-bin.000012 --base64-output=decode-rows -vv

–include-gtids=name

仅显示 中列出的组 gtid_set

mysqlbinlog mysql-bin.000012 --include-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:1-31' --exclude-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:31' 

–exclude-gtids=name

不显示 中列出的任何组 gtid_set

mysqlbinlog mysql-bin.000012 --include-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:1-31' --exclude-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:31' 

–skip-gtids

不要在输出转储文件中包含binlog日志文件中的 GTID

不要保留全局事务标识符,而是让服务器像执行新事务一样执行这些事务

开启GTID后,MySQL恢复binlog时,重复GTID的事务不会再执行了,想恢复就得采用此参数

mysqlbinlog --skip-gtids mysql-bin.000012 >./dump.sql
mysqlbinlog --skip-gtids  --include-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:1-31' --exclude-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:31'  mysql-bin.000012 >./dump.sql

其他操作

自动清理日志

企业建议,至少保留两个全备周期+1的binlog

(root@localhost) [(none)]> select @@expire_logs_days;
+--------------------+
| @@expire_logs_days |
+--------------------+
|                  7 |
+--------------------+
1 row in set (0.00 sec)

临时生效,重启后失效

(root@localhost) [(none)]> set global expire_logs_days=30;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [(none)]> show variables like 'expire%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 30    |
+------------------+-------+
1 row in set (0.00 sec) 

永久生效

my.cnf
expire_logs_days=15

手工清理日志

1.清理三天前的数据

(root@localhost) [(none)]> purge binary logs before now() - interval 3 day;
Query OK, 0 rows affected (0.00 sec)

2.清理mysql-bin.0000013之前的

(root@localhost) [(none)]> purge binary logs to 'mysql-bin.000013';
Query OK, 0 rows affected (0.00 sec)

注意:不要手工 rm binlog文件

3.reset master

(root@localhost) [db1]> reset master;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [db1]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主从关系中,主库执行此操作,主从环境必崩

滚动日志

1.flush logs

(root@localhost) [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000013 |      356 |              |                  | a469ece3-10cd-11ed-98cd-525400d5deea:1-32 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> flush logs;
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000014 |      194 |              |                  | a469ece3-10cd-11ed-98cd-525400d5deea:1-32 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

2.重启mysql也会自动滚动一个新的

3.日志文件达到指定大小时也会滚动一个新的,通过max_binlog_size参数控制,备份时,加入参数也可以自动滚动

(root@localhost) [(none)]> select @@max_binlog_size;
+-------------------+
| @@max_binlog_size |
+-------------------+
|         536870912 |
+-------------------+
1 row in set (0.00 sec)
my.cnf
max_binlog_size=512M

优化

binlog_cache_size = 2M #为每个session 分配的内存,在事务过程中用来存储binlog日志的缓存, 提高记录bin-log的效率。没有什么大事务,dml也不是很频繁的情况下可以设置小一点,如果事务大而且多,dml操作也频繁,则可以适当的调大一点。前者建议是--1M,后者建议是:即 2--4M
max_binlog_cache_size = 8M #表示的是binlog 能够使用的最大cache 内存大小
max_binlog_size= 512M #指定binlog日志文件的大小,如果当前的日志大小达到max_binlog_size,还会自动创建新的binlog日志。你不能将该变量设置为大于1GB或小于4096字节。默认值是1GB。在导入大容量的sql文件时,建议关闭sql_log_bin,否则硬盘扛不住,而且建议定期做删除。
expire_logs_days = 30 #定义了mysql清除过期日志的时间。binlog日志自动删除的天数。默认值为0,表示“没有自动删除”。

binlog_cache_size = 2M

为每个session 分配的内存,在事务过程中用来存储binlog日志的缓存, 提高记录bin-log的效率。没有什么大事务,dml也不是很频繁的情况下可以设置小一点,如果事务大而且多,dml操作也频繁,则可以适当的调大一点。前者建议是–1M,后者建议是:即 2–4M

max_binlog_cache_size = 8M

表示的是binlog 能够使用的最大cache 内存大小

max_binlog_size= 1G

指定binlog日志文件的大小,如果当前的日志大小达到max_binlog_size,还会自动创建新的binlog日志。你不能将该变量设置为大于1GB或小于4096字节。默认值是1GB。在导入大容量的sql文件时,建议关闭sql_log_bin,否则硬盘扛不住,而且建议定期做删除

expire_logs_days = 30

定义了mysql清除过期日志的时间。binlog日志自动删除的天数。默认值为0,表示“没有自动删除”。

综合参数

log_bin=/data/mysql/3306/binlog/mysql-bin
binlog_format=row
server-id=1
gtid-mode=on
enforce-gtid-consistency=on
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=1G
expire_logs_days=30
# 双1标准
sync_binlog=1

故障演练

创建了一个库 db1, 导入了表t1 ,t1表中录入了很多数据
一个开发人员,drop database db;
没有备份,日志都在.怎么恢复?
思路:找到建库语句到删库之前所有的日志,进行恢复.(开启了GTID模式)

(root@localhost) [(none)]> drop database if exists db1 ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> create database db1 charset utf8;  
Query OK, 1 row affected (0.00 sec)

(root@localhost) [(none)]> use db1;
Database changed
(root@localhost) [db1]> create table t1(id int);
Query OK, 0 rows affected (0.03 sec)

(root@localhost) [db1]> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(root@localhost) [db1]> insert into t1 values(4),(5),(6);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(root@localhost) [db1]> commit;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [db1]> update t1 set id=30 where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

(root@localhost) [db1]> commit;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [db1]> delete from t1 where id=4;
Query OK, 1 row affected (0.00 sec)

(root@localhost) [db1]> commit;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [db1]> insert into t1 values(7),(8),(9);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(root@localhost) [db1]> commit;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [db1]> drop database db1;
Query OK, 1 row affected (0.01 sec)

========================
(0) drop database if exists db1 ;
(1) create database db1 charset utf8;     
(2) use db1;
(3) create table t1 (id int);
(4) insert into t1 values(1),(2),(3);
(5) insert into t1 values(4),(5),(6);
(6) commit
(7) update t1 set id=30 where id=3;
(8) commit;
(9) delete from t1 where id=4;
(10)commit;
(11)insert into t1 values(7),(8),(9);
(12)commit;
(13)drop database db1;
========================
drop database if exists db1 ;
create database db1 charset utf8; 
use db1;
create table t1 (id int);
insert into t1 values(1),(2),(3);
insert into t1 values(4),(5),(6);
commit;
update t1 set id=30 where id=3;
commit;
delete from t1 where id=4;
commit;
insert into t1 values(7),(8),(9);
commit;
drop database db1;
=======
运行以上语句,模拟故障场景
需求:将数据库恢复到以下状态(提示第9步和第13步是误操作,其他都是正常操作)

查看当前binlog文件

show master status;
(root@localhost) [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000014 |     2126 |              |                  | a469ece3-10cd-11ed-98cd-525400d5deea:1-41 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

查看当前binlog events

show binlog events in 'mysql-bin.000014 '
(root@localhost) [(none)]> show binlog events in 'mysql-bin.000014';
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                               |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000014 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.36-log, Binlog ver: 4                              |
| mysql-bin.000014 |  123 | Previous_gtids |         1 |         194 | a469ece3-10cd-11ed-98cd-525400d5deea:1-32                          |
| mysql-bin.000014 |  194 | Gtid           |         1 |         259 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:33' |
| mysql-bin.000014 |  259 | Query          |         1 |         352 | drop database if exists db1                                        |
| mysql-bin.000014 |  352 | Gtid           |         1 |         417 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:34' |
| mysql-bin.000014 |  417 | Query          |         1 |         521 | create database db1 charset utf8                                   |
| mysql-bin.000014 |  521 | Gtid           |         1 |         586 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:35' |
| mysql-bin.000014 |  586 | Query          |         1 |         681 | use `db1`; create table t1(id int)                                 |
| mysql-bin.000014 |  681 | Gtid           |         1 |         746 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:36' |
| mysql-bin.000014 |  746 | Query          |         1 |         817 | BEGIN                                                              |
| mysql-bin.000014 |  817 | Table_map      |         1 |         861 | table_id: 109 (db1.t1)                                             |
| mysql-bin.000014 |  861 | Write_rows     |         1 |         911 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000014 |  911 | Xid            |         1 |         942 | COMMIT /* xid=66 */                                                |
| mysql-bin.000014 |  942 | Gtid           |         1 |        1007 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:37' |
| mysql-bin.000014 | 1007 | Query          |         1 |        1078 | BEGIN                                                              |
| mysql-bin.000014 | 1078 | Table_map      |         1 |        1122 | table_id: 109 (db1.t1)                                             |
| mysql-bin.000014 | 1122 | Write_rows     |         1 |        1172 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000014 | 1172 | Xid            |         1 |        1203 | COMMIT /* xid=67 */                                                |
| mysql-bin.000014 | 1203 | Gtid           |         1 |        1268 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:38' |
| mysql-bin.000014 | 1268 | Query          |         1 |        1339 | BEGIN                                                              |
| mysql-bin.000014 | 1339 | Table_map      |         1 |        1383 | table_id: 109 (db1.t1)                                             |
| mysql-bin.000014 | 1383 | Update_rows    |         1 |        1429 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000014 | 1429 | Xid            |         1 |        1460 | COMMIT /* xid=69 */                                                |
| mysql-bin.000014 | 1460 | Gtid           |         1 |        1525 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:39' |
| mysql-bin.000014 | 1525 | Query          |         1 |        1596 | BEGIN                                                              |
| mysql-bin.000014 | 1596 | Table_map      |         1 |        1640 | table_id: 109 (db1.t1)                                             |
| mysql-bin.000014 | 1640 | Delete_rows    |         1 |        1680 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000014 | 1680 | Xid            |         1 |        1711 | COMMIT /* xid=71 */                                                |
| mysql-bin.000014 | 1711 | Gtid           |         1 |        1776 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:40' |
| mysql-bin.000014 | 1776 | Query          |         1 |        1847 | BEGIN                                                              |
| mysql-bin.000014 | 1847 | Table_map      |         1 |        1891 | table_id: 109 (db1.t1)                                             |
| mysql-bin.000014 | 1891 | Write_rows     |         1 |        1941 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000014 | 1941 | Xid            |         1 |        1972 | COMMIT /* xid=73 */                                                |
| mysql-bin.000014 | 1972 | Gtid           |         1 |        2037 | SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:41' |
| mysql-bin.000014 | 2037 | Query          |         1 |        2126 | drop database db1                                                  |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
35 rows in set (0.00 sec)

查看binlog详细内容

mysqlbinlog --base64-output=decode-rows -vv --start-datetime='2022-08-22 09:00' --stop-datetime='2022-08-22 12:00'  -d db1  mysql-bin.000014  >/tmp/result.sql
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220819 15:57:58 server id 1  end_log_pos 123 CRC32 0xc117f37e 	Start: binlog v 4, server v 5.7.36-log created 220819 15:57:58
# Warning: this binlog is either in use or was not closed properly.
# at 194
#220822 11:13:31 server id 1  end_log_pos 259 CRC32 0xcb8bcdca 	GTID	last_committed=0	sequence_number=1	rbr_only=no
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:33'/*!*/;
# at 259
#220822 11:13:31 server id 1  end_log_pos 352 CRC32 0xd19a4e55 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138011/*!*/;
SET @@session.pseudo_thread_id=24/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=224,@@session.collation_connection=224,@@session.collation_server=224/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
drop database if exists db1
/*!*/;
# at 352
#220822 11:13:47 server id 1  end_log_pos 417 CRC32 0xaf5fe918 	GTID	last_committed=1	sequence_number=2	rbr_only=no
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:34'/*!*/;
# at 417
#220822 11:13:47 server id 1  end_log_pos 521 CRC32 0x91172a9a 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138027/*!*/;
create database db1 charset utf8
/*!*/;
# at 521
#220822 11:14:07 server id 1  end_log_pos 586 CRC32 0xd73afa06 	GTID	last_committed=2	sequence_number=3	rbr_only=no
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:35'/*!*/;
# at 586
#220822 11:14:07 server id 1  end_log_pos 681 CRC32 0x39129c09 	Query	thread_id=24	exec_time=0	error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1661138047/*!*/;
create table t1(id int)
/*!*/;
# at 681
#220822 11:14:40 server id 1  end_log_pos 746 CRC32 0x7c89b1d8 	GTID	last_committed=3	sequence_number=4	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:36'/*!*/;
# at 746
#220822 11:14:40 server id 1  end_log_pos 817 CRC32 0x6f5f3852 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138080/*!*/;
BEGIN
/*!*/;
# at 817
#220822 11:14:40 server id 1  end_log_pos 861 CRC32 0xd0d79c66 	Table_map: `db1`.`t1` mapped to number 109
# at 861
#220822 11:14:40 server id 1  end_log_pos 911 CRC32 0xcb1cbbdf 	Write_rows: table id 109 flags: STMT_END_F
### INSERT INTO `db1`.`t1`
### SET
###   @1=1 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=3 /* INT meta=0 nullable=1 is_null=0 */
# at 911
#220822 11:14:40 server id 1  end_log_pos 942 CRC32 0x2ca37ce3 	Xid = 66
COMMIT/*!*/;
# at 942
#220822 11:15:00 server id 1  end_log_pos 1007 CRC32 0xd70a75bd 	GTID	last_committed=4	sequence_number=5	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:37'/*!*/;
# at 1007
#220822 11:15:00 server id 1  end_log_pos 1078 CRC32 0x29ea80b8 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138100/*!*/;
BEGIN
/*!*/;
# at 1078
#220822 11:15:00 server id 1  end_log_pos 1122 CRC32 0xdeb8ca92 	Table_map: `db1`.`t1` mapped to number 109
# at 1122
#220822 11:15:00 server id 1  end_log_pos 1172 CRC32 0xffa6a19b 	Write_rows: table id 109 flags: STMT_END_F
### INSERT INTO `db1`.`t1`
### SET
###   @1=4 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=5 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=6 /* INT meta=0 nullable=1 is_null=0 */
# at 1172
#220822 11:15:00 server id 1  end_log_pos 1203 CRC32 0x7307f602 	Xid = 67
COMMIT/*!*/;
# at 1203
#220822 11:17:21 server id 1  end_log_pos 1268 CRC32 0xa8d53a45 	GTID	last_committed=5	sequence_number=6	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:38'/*!*/;
# at 1268
#220822 11:17:21 server id 1  end_log_pos 1339 CRC32 0x9156e38d 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138241/*!*/;
BEGIN
/*!*/;
# at 1339
#220822 11:17:21 server id 1  end_log_pos 1383 CRC32 0x6efec1fd 	Table_map: `db1`.`t1` mapped to number 109
# at 1383
#220822 11:17:21 server id 1  end_log_pos 1429 CRC32 0xa15c3e47 	Update_rows: table id 109 flags: STMT_END_F
### UPDATE `db1`.`t1`
### WHERE
###   @1=3 /* INT meta=0 nullable=1 is_null=0 */
### SET
###   @1=30 /* INT meta=0 nullable=1 is_null=0 */
# at 1429
#220822 11:17:21 server id 1  end_log_pos 1460 CRC32 0x3beb5cd2 	Xid = 69
COMMIT/*!*/;
# at 1460
#220822 11:17:46 server id 1  end_log_pos 1525 CRC32 0xd33b8151 	GTID	last_committed=6	sequence_number=7	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:39'/*!*/;
# at 1525
#220822 11:17:46 server id 1  end_log_pos 1596 CRC32 0x52fe1a6e 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138266/*!*/;
BEGIN
/*!*/;
# at 1596
#220822 11:17:46 server id 1  end_log_pos 1640 CRC32 0x28d68e80 	Table_map: `db1`.`t1` mapped to number 109
# at 1640
#220822 11:17:46 server id 1  end_log_pos 1680 CRC32 0x75166daf 	Delete_rows: table id 109 flags: STMT_END_F
### DELETE FROM `db1`.`t1`
### WHERE
###   @1=4 /* INT meta=0 nullable=1 is_null=0 */
# at 1680
#220822 11:17:46 server id 1  end_log_pos 1711 CRC32 0xc2ec60fc 	Xid = 71
COMMIT/*!*/;
# at 1711
#220822 11:18:11 server id 1  end_log_pos 1776 CRC32 0xaac0949c 	GTID	last_committed=7	sequence_number=8	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:40'/*!*/;
# at 1776
#220822 11:18:11 server id 1  end_log_pos 1847 CRC32 0xb6ba1980 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138291/*!*/;
BEGIN
/*!*/;
# at 1847
#220822 11:18:11 server id 1  end_log_pos 1891 CRC32 0x5955c15b 	Table_map: `db1`.`t1` mapped to number 109
# at 1891
#220822 11:18:11 server id 1  end_log_pos 1941 CRC32 0xabb399cc 	Write_rows: table id 109 flags: STMT_END_F
### INSERT INTO `db1`.`t1`
### SET
###   @1=7 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=8 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `db1`.`t1`
### SET
###   @1=9 /* INT meta=0 nullable=1 is_null=0 */
# at 1941
#220822 11:18:11 server id 1  end_log_pos 1972 CRC32 0x339d37ab 	Xid = 73
COMMIT/*!*/;
# at 1972
#220822 11:18:32 server id 1  end_log_pos 2037 CRC32 0xb9a33d03 	GTID	last_committed=8	sequence_number=9	rbr_only=no
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:41'/*!*/;
# at 2037
#220822 11:18:32 server id 1  end_log_pos 2126 CRC32 0xa621a531 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138312/*!*/;
drop database db1
/*!*/;
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*/;
# at 1460
#220822 11:17:46 server id 1  end_log_pos 1525 CRC32 0xd33b8151 	GTID	last_committed=6	sequence_number=7	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:39'/*!*/;
# at 1525
#220822 11:17:46 server id 1  end_log_pos 1596 CRC32 0x52fe1a6e 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138266/*!*/;
BEGIN
/*!*/;
# at 1596
#220822 11:17:46 server id 1  end_log_pos 1640 CRC32 0x28d68e80 	Table_map: `db1`.`t1` mapped to number 109
# at 1640
#220822 11:17:46 server id 1  end_log_pos 1680 CRC32 0x75166daf 	Delete_rows: table id 109 flags: STMT_END_F
### DELETE FROM `db1`.`t1`
### WHERE
###   @1=4 /* INT meta=0 nullable=1 is_null=0 */
# at 1680
#220822 11:17:46 server id 1  end_log_pos 1711 CRC32 0xc2ec60fc 	Xid = 71
COMMIT/*!*/;
COMMIT/*!*/;
# at 1972
#220822 11:18:32 server id 1  end_log_pos 2037 CRC32 0xb9a33d03 	GTID	last_committed=8	sequence_number=9	rbr_only=no
SET @@SESSION.GTID_NEXT= 'a469ece3-10cd-11ed-98cd-525400d5deea:41'/*!*/;
# at 2037
#220822 11:18:32 server id 1  end_log_pos 2126 CRC32 0xa621a531 	Query	thread_id=24	exec_time=0	error_code=0
SET TIMESTAMP=1661138312/*!*/;
drop database db1
/*!*/;

基于position号恢复

mysqlbinlog  --start-position=259 --stop-position=1460  --skip-gtids  -d db1   /data/mysql/3306/binlog/mysql-bin.000014 >/tmp/t1.sql
mysqlbinlog  --start-position=1711 --stop-position=1972 --skip-gtids  -d db1   /data/mysql/3306/binlog/mysql-bin.000014 >/tmp/t2.sql

mysql>
set sql_log_bin=0;
source /tmp/t1.sql;
source /tmp/t2.sql;
set sql_log_bin=1;

基于gtid恢复

mysqlbinlog --include-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:33-38' --skip-gtids -d db1 /data/mysql/3306/binlog/mysql-bin.000014 >/tmp/t1.sql
mysqlbinlog --include-gtids='a469ece3-10cd-11ed-98cd-525400d5deea:40' --skip-gtids -d db1 /data/mysql/3306/binlog/mysql-bin.000014 >/tmp/t2.sql

mysql>
set sql_log_bin=0;
source /tmp/t1.sql;
source /tmp/t2.sql;
set sql_log_bin=1;

你可能感兴趣的:(mysql,数据库,后端)