binlog相关
MySQL 的二进制日志 binlog 可以说是 MySQL 最重要的日志,它记录了所有的 DDL 和 DML 语句(除了数据查询语句select、show等),以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的。binlog 的主要目的是复制和恢复。
Binlog日志的两个最重要的使用场景
MySQL主从复制:MySQL Replication在Master端开启binlog,Master把它的二进制日志传递给slaves来达到master-slave数据一致的目的
数据恢复:通过使用 mysqlbinlog工具来使恢复数据
总体的关于binlog的参数如下
sync_binlog = 1 log-bin = mysql-bin binlog_format = row expire_logs_days = 10 binlog_cache_size =4M max_binlog_cache_size =8M max_binlog_size =1024M
相关参数讲解
log-bin
决定了msyql 的binlog的名字,?成的binlog名字为mysql-bin.000001
binlog_format
规定binlog的格式, binlog有三种格式statement, row以及mixed,默认使?默认使?statement(5.7.7之前),建议使?row格式(5.7.7之后默认)
expire_logs_days
过期时间
binlog_do_db
此参数表示只记录指定数据库的?进制?志
binlog_ignore_db
此参数表示不记录指定的数据库的?进制?志
sync_binlog
在提交n次事务后,进?binlog的落盘, 0为不进?强?的刷新操作,?是由?件系统控制刷新?志?件
如果是在线交易和帐有关的数据建议设置成1, 如果是其它数据可以保持为0即可。
max_binlog_size
binlog?件的最?值,默认和最?是1GB,并不能严格限定?进制?件的??
max_binlog_cache_size 表示的是binlog 能够使?的最?cache 内存??
当我们执?多语句事务的时候 所有session的使?的内存超过max_binlog_cache_size的值时
就会报错: “Multi-statement transaction required more than ‘max_binlog_cache_size‘ bytes ofstorage”
mysqlbinlog格式
statment格式
特点:记录每?条数据的SQL,将执?的每?条SQL记录在binlog中,减少?志量,节省IO,提?性能。
缺点:某些SQL中的函数?法使?,?如SYSDATE(),在同步过程中会出现?法同步的问题。
mixed格式
特点:?般的语句使?SQL语句来记录,遇到特殊的语句使?row格式来记录,保证数据的?致性和复制的准确性。
row格式
特点: binlog中仅仅记录哪?条记录被修改,不记录dml的sql语句,会详细记录每?行的更改细节,不会出现?法复制的问题。
缺点:因为要记录每?条修改记录的?志,所以?量占?磁盘IO和?量使?硬盘空间。
注:将二进制日志格式设置为ROW时,有些更改仍然使用基于语句的格式,包括所有DDL语句,例如CREATE TABLE, ALTER TABLE,或 DROP TABLE。
开启binlog
由于log_bin是个只读系统变量,不能动态的修改,只能再my.cnf里[mysqld]模块添加log-bin 配置,表示启用binlog,如果没有给定值,写成 log-bin=,则默认名称为主机名。(注:名称若带有小数点,则只取第一个小数点前的部分作为名称)
[mysqld] log-bin=mysql-bin
此方法需要重启数据库方能生效,如果想临时关掉binlog而又不想重启数据库,可以使用sql_log_bin,sql_log_bin 是一个动态变量,修改该变量时,可以只对当前会话生效(Session),也可以是全局的(Global),当全局修改这个变量时,只会对新的会话生效(这意味当对当前会话也不会生效),因此一般全局修改了这个变量后,都要把原来的所有连接 kill 掉,全局只在MariaDB 5.5之前和MySQL 5.6.22之前生效,后面的版本都已经不支持全局,因为太危险了。
用处:
当还原数据库的时候,如果不关闭二进制日志,那么你还原的过程仍然会记录在二进制日志里面,不仅浪费资源,那么增加了磁盘的容量,还没有必要(特别是利用二进制还原数据库的时候)所以一般还原的时候会选择关闭二进制日志,可以通过修改配置文件,重启关闭二进制日志。
也可以动态命令关闭sql_log_bin,然后导入数据库。
set sql_log_bin=0;# 设为0后,在当前会话上执行的语句都不记录binlog set sql_log_bin=1; # 取消限制
常用的binlog命令
# 是否启用binlog日志 show variables like ‘log_bin‘; # 查看详细的日志配置信息 show global variables like ‘%log%‘; # 查看binlog的目录 show global variables like "%log_bin%"; # 查看当前服务器使用的biglog文件及大小 show binary logs; # 查看最新一个binlog日志文件名称和Position show master status; # 清除所有的binlog?件,并且重置为?个 reset master
清理binlog日志
自动清理:
通过binlog参数(expire_logs_days )来实现mysql?动删除binlog
show binary logs; show variables like ‘expire_logs_days‘; set global expire_logs_days=3; # 过期删除,单位是天
手动清理:
1、使?reset master 重置binlog?件(#reset master后,会造成slave?法找到master的严重后果)
2、直接rm本地删除binlog
# 查看正在使用的binlog show master status;
直接使?rm 命令删除不是当前使?的binlog(切记不要删除正在使?的binlog)
mysqlbinlog命令
root@localhost localhost 15:23:43 (none)>show global variables like "%log_bin%"; +---------------------------------+-----------------------------+ | Variable_name | Value | +---------------------------------+-----------------------------+ | log_bin | ON | | log_bin_basename | /data/mysql/mysql-bin | | log_bin_index | /data/mysql/mysql-bin.index | | log_bin_trust_function_creators | ON | | log_bin_use_v1_row_events | OFF | +---------------------------------+-----------------------------+ 5 rows in set (0.00 sec) root@localhost localhost 15:29:21 test1>show global variables like "binlog_format"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row in set (0.01 sec) root@localhost localhost 15:25:06 abc>create database test1; Query OK, 1 row affected (0.00 sec) root@localhost localhost 15:25:28 abc>use test1; Database changed root@localhost localhost 15:25:34 test1>CREATE TABLE `abc` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> `c1` int(11) DEFAULT NULL, -> PRIMARY KEY (`id`), -> KEY `shouji` (`c1`) USING BTREE -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 sec) [email protected] localhost 15:26:12 test1>insert into abc(c1) values (1); Query OK, 1 row affected (0.13 sec) [email protected] localhost 15:26:23 test1>insert into abc(c1) values (2),(3); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 [email protected] localhost 15:26:43 test1>delete from abc where id=2; Query OK, 1 row affected (0.00 sec) [email protected] localhost 15:26:58 test1>select * from abc; +----+------+ | id | c1 | +----+------+ | 1 | 1 | | 3 | 3 | +----+------+ 2 rows in set (0.00 sec)
[email protected] localhost 15:47:38 test1>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 1361 | | | 0f943fa6-3117-11ea-a98d-005056b351ef:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
[email protected] localhost 15:48:27 test1>show binlog events in ‘mysql-bin.000001‘\G;
*************************** 1. row ***************************
Log_name: mysql-bin.000001
Pos: 4
Event_type: Format_desc
Server_id: 1007231
End_log_pos: 123
Info: Server ver: 5.7.19-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin.000001
Pos: 123
Event_type: Previous_gtids
Server_id: 1007231
End_log_pos: 150
Info:
*************************** 3. row ***************************
Log_name: mysql-bin.000001
Pos: 150
Event_type: Gtid
Server_id: 1007231
End_log_pos: 211
Info: SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:1‘
*************************** 4. row ***************************
Log_name: mysql-bin.000001
Pos: 211
Event_type: Query
Server_id: 1007231
End_log_pos: 304
Info: create database test1
*************************** 5. row ***************************
Log_name: mysql-bin.000001
Pos: 304
Event_type: Gtid
Server_id: 1007231
End_log_pos: 365
Info: SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:2‘
*************************** 6. row ***************************
Log_name: mysql-bin.000001
Pos: 365
Event_type: Query
Server_id: 1007231
End_log_pos: 629
Info: use `test1`; CREATE TABLE `abc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `shouji` (`c1`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
*************************** 7. row ***************************
Log_name: mysql-bin.000001
Pos: 629
Event_type: Gtid
Server_id: 1007231
End_log_pos: 690
Info: SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:3‘
*************************** 8. row ***************************
Log_name: mysql-bin.000001
Pos: 690
Event_type: Query
Server_id: 1007231
End_log_pos: 759
Info: BEGIN
*************************** 9. row ***************************
Log_name: mysql-bin.000001
Pos: 759
Event_type: Table_map
Server_id: 1007231
End_log_pos: 803
Info: table_id: 220 (test1.abc)
*************************** 10. row ***************************
Log_name: mysql-bin.000001
Pos: 803
Event_type: Write_rows
Server_id: 1007231
End_log_pos: 843
Info: table_id: 220 flags: STMT_END_F
*************************** 11. row ***************************
Log_name: mysql-bin.000001
Pos: 843
Event_type: Xid
Server_id: 1007231
End_log_pos: 870
Info: COMMIT /* xid=52 */
*************************** 12. row ***************************
Log_name: mysql-bin.000001
Pos: 870
Event_type: Gtid
Server_id: 1007231
End_log_pos: 931
Info: SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:4‘
*************************** 13. row ***************************
Log_name: mysql-bin.000001
Pos: 931
Event_type: Query
Server_id: 1007231
End_log_pos: 1000
Info: BEGIN
*************************** 14. row ***************************
Log_name: mysql-bin.000001
Pos: 1000
Event_type: Table_map
Server_id: 1007231
End_log_pos: 1044
Info: table_id: 220 (test1.abc)
*************************** 15. row ***************************
Log_name: mysql-bin.000001
Pos: 1044
Event_type: Write_rows
Server_id: 1007231
End_log_pos: 1093
Info: table_id: 220 flags: STMT_END_F
*************************** 16. row ***************************
Log_name: mysql-bin.000001
Pos: 1093
Event_type: Xid
Server_id: 1007231
End_log_pos: 1120
Info: COMMIT /* xid=53 */
*************************** 17. row ***************************
Log_name: mysql-bin.000001
Pos: 1120
Event_type: Gtid
Server_id: 1007231
End_log_pos: 1181
Info: SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:5‘
*************************** 18. row ***************************
Log_name: mysql-bin.000001
Pos: 1181
Event_type: Query
Server_id: 1007231
End_log_pos: 1250
Info: BEGIN
*************************** 19. row ***************************
Log_name: mysql-bin.000001
Pos: 1250
Event_type: Table_map
Server_id: 1007231
End_log_pos: 1294
Info: table_id: 220 (test1.abc)
*************************** 20. row ***************************
Log_name: mysql-bin.000001
Pos: 1294
Event_type: Delete_rows
Server_id: 1007231
End_log_pos: 1334
Info: table_id: 220 flags: STMT_END_F
*************************** 21. row ***************************
Log_name: mysql-bin.000001
Pos: 1334
Event_type: Xid
Server_id: 1007231
End_log_pos: 1361
Info: COMMIT /* xid=54 */
21 rows in set (0.00 sec)
下面是mysqlbinlog命令的参数,摘自官方文档
选项名称 | 描述 | 引用 | 弃用 |
---|---|---|---|
--base64-output | 使用base-64编码打印二进制日志条目 | ||
--bind-address | 使用指定的网络接口连接到MySQL Server | ||
--binlog-row-event-max-size | 二进制日志最大事件大小 | ||
--character-sets-dir | 字符集的安装目录 | ||
--connection-server-id | 用于测试和调试。有关适用的默认值和其他详细信息,请参见文本 | 5.7.5 | |
--database | 仅列出该数据库的条目 | ||
--debug | 编写调试日志 | ||
--debug-check | 程序退出时打印调试信息 | ||
--debug-info | 程序退出时打印调试信息,内存和CPU统计信息 | ||
--default-auth | 身份验证插件使用 | ||
--defaults-extra-file | 除了通常的选项文件,还读取命名的选项文件 | ||
--defaults-file | 只读命名的选项文件 | ||
--defaults-group-suffix | 选项组后缀值 | ||
--disable-log-bin | 禁用二进制日志记录 | ||
--exclude-gtids | 不要显示提供的GTID集中的任何组 | ||
--force-if-open | 读取二进制日志文件,即使打开或未正确关闭 | ||
--force-read | 如果mysqlbinlog读取了无法识别的二进制日志事件,则会输出警告 | ||
--get-server-public-key | 从服务器请求RSA公钥 | 5.7.23 | |
--help | 显示帮助信息并退出 | ||
--hexdump | 显示登录注释的十六进制转储 | ||
--host | MySQL服务器所在的主机 | ||
--idempotent | 仅在处理来自此会话的二进制日志更新时,导致服务器使用幂等模式 | 5.7.0 | |
--include-gtids | 仅显示提供的GTID集中的组 | ||
--local-load | 在指定目录中为LOAD DATA准备本地临时文件 | ||
--login-path | 从.mylogin.cnf中读取登录路径选项 | ||
--no-defaults | 不读取选项文件 | ||
--offset | 跳过日志中的前N个条目 | ||
open_files_limit | 指定要保留的打开文件描述符的数量 | ||
--password | 连接服务器时使用的密码 | ||
--plugin-dir | 安装插件的目录 | ||
--port | 用于连接的TCP / IP端口号 | ||
--print-defaults | 打印默认选项 | ||
--protocol | 使用的连接协议 | ||
--raw | 将事件以原始(二进制)格式写入输出文件 | ||
--read-from-remote-master | 从MySQL主服务器读取二进制日志,而不是读取本地日志文件 | ||
--read-from-remote-server | 从MySQL服务器而不是本地日志文件中读取二进制日志 | ||
--result-file | 直接输出到命名文件 | ||
--rewrite-db | 从基于行的格式编写的日志中播放时,为数据库创建重写规则。可多次使用 | 5.7.1 | |
--secure-auth | 不要以旧(4.1之前)格式向服务器发送密码 | 5.7.4 | 5.7.5 |
--server-id | 仅提取由具有给定服务器ID的服务器创建的那些事件 | ||
--server-id-bits | 告诉mysqlbinlog当mysqld编写的server-id-bits设置为小于最大值时,如何解释二进制日志中的服务器ID;仅受MySQL Cluster版本的mysqlbinlog支持 | ||
--server-public-key-path | 包含RSA公钥的文件的路径名 | 5.7.23 | |
--set-charset | 在输出中添加SET NAMES charset_name语句 | ||
--shared-memory-base-name | 用于共享内存连接的共享内存的名称 | ||
--short-form | 仅显示日志中包含的语句 | ||
--skip-gtids | 不要打印任何GTID。从包含GTID的二进制日志写入转储文件时使用此功能 | ||
--socket | Unix套接字文件或Windows命名管道使用 | ||
--ssl | 启用连接加密 | 5.7.3 | |
--ssl-ca | 包含受信任的SSL证书颁发机构列表的文件 | 5.7.3 | |
--ssl-capath | 包含受信任的SSL证书颁发机构证书文件的目录 | 5.7.3 | |
--ssl-cert | 包含X.509证书的文件 | 5.7.3 | |
--ssl-cipher | 连接加密的允许密码 | 5.7.3 | |
--ssl-crl | 包含证书吊销列表的文件 | ||
--ssl-crlpath | 包含证书吊销列表文件的目录 | ||
--ssl-key | 包含X.509密钥的文件 | 5.7.3 | |
--ssl-mode | 与服务器连接的所需安全状态 | 5.7.11 | |
--ssl-verify-server-cert | 根据服务器证书的公用名身份验证主机名 | 5.7.3 | |
--start-datetime | 从第一个事件中读取时间戳等于或晚于datetime参数的二进制日志 | ||
--start-position | 从位置等于或大于参数的第一个事件读取二进制日志 | ||
--stop-datetime | 在时间戳等于或大于datetime参数的第一个事件时停止读取二进制日志 | ||
--stop-never | 读取最后一个二进制日志文件后保持与服务器的连接 | ||
--stop-never-slave-server-id | 连接到服务器时要报告的从服务器ID | ||
--stop-position | 在第一个事件中停止读取二进制日志,且位置等于或大于参数 | ||
--tls-version | 允许的TLS协议进行加密连接 | 5.7.10 | |
--to-last-log | 不要在从MySQL服务器请求的二进制日志的结尾处停止,而要继续打印到最后一个二进制日志的结尾 | ||
--user | 连接服务器时要使用的MySQL用户名 | ||
--verbose | 将行事件重建为SQL语句 | ||
--verify-binlog-checksum | 验证二进制日志中的校验和 |
[[email protected] mysql]# mysqlbinlog mysql-bin.000001 >/data/tmp/mysqlbinlog.sql [[email protected] mysql]# cat /data/tmp/mysqlbinlog.sql /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200107 15:45:15 server id 1007231 end_log_pos 123 Start: binlog v 4, server v 5.7.19-log created 200107 15:45:15 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ‘ CzcUXg9/Xg8AdwAAAHsAAAABAAQANS43LjE5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAALNxReEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA AEJXKj0= ‘/*!*/; # at 123 #200107 15:45:15 server id 1007231 end_log_pos 150 Previous-GTIDs # [empty] # at 150 #200107 15:45:53 server id 1007231 end_log_pos 211 GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:1‘/*!*/; # at 211 #200107 15:45:53 server id 1007231 end_log_pos 304 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383153/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1344274432/*!*/; 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/*!*/; create database test1 /*!*/; # at 304 #200107 15:46:38 server id 1007231 end_log_pos 365 GTID last_committed=1 sequence_number=2 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:2‘/*!*/; # at 365 #200107 15:46:38 server id 1007231 end_log_pos 629 Query thread_id=3 exec_time=0 error_code=0 use `test1`/*!*/; SET TIMESTAMP=1578383198/*!*/; CREATE TABLE `abc` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c1` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `shouji` (`c1`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!*/; # at 629 #200107 15:47:15 server id 1007231 end_log_pos 690 GTID last_committed=2 sequence_number=3 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:3‘/*!*/; # at 690 #200107 15:47:15 server id 1007231 end_log_pos 759 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383235/*!*/; BEGIN /*!*/; # at 759 #200107 15:47:15 server id 1007231 end_log_pos 803 Table_map: `test1`.`abc` mapped to number 220 # at 803 #200107 15:47:15 server id 1007231 end_log_pos 843 Write_rows: table id 220 flags: STMT_END_F BINLOG ‘ gzcUXhN/Xg8ALAAAACMDAAAAANwAAAAAAAEABXRlc3QxAANhYmMAAgMDAAI= gzcUXh5/Xg8AKAAAAEsDAAAAANwAAAAAAAEAAgAC//wBAAAAAQAAAA== ‘/*!*/; # at 843 #200107 15:47:15 server id 1007231 end_log_pos 870 Xid = 52 COMMIT/*!*/; # at 870 #200107 15:47:23 server id 1007231 end_log_pos 931 GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:4‘/*!*/; # at 931 #200107 15:47:23 server id 1007231 end_log_pos 1000 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383243/*!*/; BEGIN /*!*/; # at 1000 #200107 15:47:23 server id 1007231 end_log_pos 1044 Table_map: `test1`.`abc` mapped to number 220 # at 1044 #200107 15:47:23 server id 1007231 end_log_pos 1093 Write_rows: table id 220 flags: STMT_END_F BINLOG ‘ izcUXhN/Xg8ALAAAABQEAAAAANwAAAAAAAEABXRlc3QxAANhYmMAAgMDAAI= izcUXh5/Xg8AMQAAAEUEAAAAANwAAAAAAAEAAgAC//wCAAAAAgAAAPwDAAAAAwAAAA== ‘/*!*/; # at 1093 #200107 15:47:23 server id 1007231 end_log_pos 1120 Xid = 53 COMMIT/*!*/; # at 1120 #200107 15:47:30 server id 1007231 end_log_pos 1181 GTID last_committed=4 sequence_number=5 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:5‘/*!*/; # at 1181 #200107 15:47:30 server id 1007231 end_log_pos 1250 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383250/*!*/; BEGIN /*!*/; # at 1250 #200107 15:47:30 server id 1007231 end_log_pos 1294 Table_map: `test1`.`abc` mapped to number 220 # at 1294 #200107 15:47:30 server id 1007231 end_log_pos 1334 Delete_rows: table id 220 flags: STMT_END_F BINLOG ‘ kjcUXhN/Xg8ALAAAAA4FAAAAANwAAAAAAAEABXRlc3QxAANhYmMAAgMDAAI= kjcUXiB/Xg8AKAAAADYFAAAAANwAAAAAAAEAAgAC//wCAAAAAgAAAA== ‘/*!*/; # at 1334 #200107 15:47:30 server id 1007231 end_log_pos 1361 Xid = 54 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET [email protected]_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
发现有很多base64编码的BINLOG语句,比如
BINLOG ‘ CzcUXg9/Xg8AdwAAAHsAAAABAAQANS43LjE5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAALNxReEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA AEJXKj0= ‘/*!*/;这样的,这就需要加上--base64-output=DECODE-ROWS --verbose来解成一个sql语句。
[[email protected] mysql]# mysqlbinlog --base64-output=DECODE-ROWS -v mysql-bin.000001 >/data/tmp/mysqlbinlog2.sql [[email protected] mysql]# cat /data/tmp/mysqlbinlog2.sql /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200107 15:45:15 server id 1007231 end_log_pos 123 Start: binlog v 4, server v 5.7.19-log created 200107 15:45:15 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; # at 123 #200107 15:45:15 server id 1007231 end_log_pos 150 Previous-GTIDs # [empty] # at 150 #200107 15:45:53 server id 1007231 end_log_pos 211 GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:1‘/*!*/; # at 211 #200107 15:45:53 server id 1007231 end_log_pos 304 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383153/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1344274432/*!*/; 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/*!*/; create database test1 /*!*/; # at 304 #200107 15:46:38 server id 1007231 end_log_pos 365 GTID last_committed=1 sequence_number=2 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:2‘/*!*/; # at 365 #200107 15:46:38 server id 1007231 end_log_pos 629 Query thread_id=3 exec_time=0 error_code=0 use `test1`/*!*/; SET TIMESTAMP=1578383198/*!*/; CREATE TABLE `abc` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c1` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `shouji` (`c1`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!*/; # at 629 #200107 15:47:15 server id 1007231 end_log_pos 690 GTID last_committed=2 sequence_number=3 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:3‘/*!*/; # at 690 #200107 15:47:15 server id 1007231 end_log_pos 759 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383235/*!*/; BEGIN /*!*/; # at 759 #200107 15:47:15 server id 1007231 end_log_pos 803 Table_map: `test1`.`abc` mapped to number 220 # at 803 #200107 15:47:15 server id 1007231 end_log_pos 843 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=1 ### @2=1 # at 843 #200107 15:47:15 server id 1007231 end_log_pos 870 Xid = 52 COMMIT/*!*/; # at 870 #200107 15:47:23 server id 1007231 end_log_pos 931 GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:4‘/*!*/; # at 931 #200107 15:47:23 server id 1007231 end_log_pos 1000 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383243/*!*/; BEGIN /*!*/; # at 1000 #200107 15:47:23 server id 1007231 end_log_pos 1044 Table_map: `test1`.`abc` mapped to number 220 # at 1044 #200107 15:47:23 server id 1007231 end_log_pos 1093 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=2 ### @2=2 ### INSERT INTO `test1`.`abc` ### SET ### @1=3 ### @2=3 # at 1093 #200107 15:47:23 server id 1007231 end_log_pos 1120 Xid = 53 COMMIT/*!*/; # at 1120 #200107 15:47:30 server id 1007231 end_log_pos 1181 GTID last_committed=4 sequence_number=5 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:5‘/*!*/; # at 1181 #200107 15:47:30 server id 1007231 end_log_pos 1250 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383250/*!*/; BEGIN /*!*/; # at 1250 #200107 15:47:30 server id 1007231 end_log_pos 1294 Table_map: `test1`.`abc` mapped to number 220 # at 1294 #200107 15:47:30 server id 1007231 end_log_pos 1334 Delete_rows: table id 220 flags: STMT_END_F ### DELETE FROM `test1`.`abc` ### WHERE ### @1=2 ### @2=2 # at 1334 #200107 15:47:30 server id 1007231 end_log_pos 1361 Xid = 54 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET [email protected]_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
这样就能看出来具体的sql了。
还可以根据时间来筛选语句:
[[email protected] mysql]# mysqlbinlog --base64-output=DECODE-ROWS -v --start-datetime="2020-01-07 15:47:14" --stop-datetime="2020-01-07 15:47:24" mysql-bin.000001 >/data/tmp/mysqlbinlog3.sql [[email protected] mysql]# cat /data/tmp/mysqlbinlog3.sql /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200107 15:45:15 server id 1007231 end_log_pos 123 Start: binlog v 4, server v 5.7.19-log created 200107 15:45:15 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; # at 629 #200107 15:47:15 server id 1007231 end_log_pos 690 GTID last_committed=2 sequence_number=3 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:3‘/*!*/; # at 690 #200107 15:47:15 server id 1007231 end_log_pos 759 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383235/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1344274432/*!*/; 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/*!*/; BEGIN /*!*/; # at 759 #200107 15:47:15 server id 1007231 end_log_pos 803 Table_map: `test1`.`abc` mapped to number 220 # at 803 #200107 15:47:15 server id 1007231 end_log_pos 843 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=1 ### @2=1 # at 843 #200107 15:47:15 server id 1007231 end_log_pos 870 Xid = 52 COMMIT/*!*/; # at 870 #200107 15:47:23 server id 1007231 end_log_pos 931 GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:4‘/*!*/; # at 931 #200107 15:47:23 server id 1007231 end_log_pos 1000 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383243/*!*/; BEGIN /*!*/; # at 1000 #200107 15:47:23 server id 1007231 end_log_pos 1044 Table_map: `test1`.`abc` mapped to number 220 # at 1044 #200107 15:47:23 server id 1007231 end_log_pos 1093 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=2 ### @2=2 ### INSERT INTO `test1`.`abc` ### SET ### @1=3 ### @2=3 # at 1093 #200107 15:47:23 server id 1007231 end_log_pos 1120 Xid = 53 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET [email protected]_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
根据位置筛选,比我我们上面用show binlog events in ‘mysql-bin.000001‘\G;看到的有条删除类型的语句
*************************** 20. row *************************** Log_name: mysql-bin.000001 Pos: 1294 Event_type: Delete_rows Server_id: 1007231 End_log_pos: 1334 Info: table_id: 220 flags: STMT_END_F
[[email protected] mysql]# mysqlbinlog --base64-output=DECODE-ROWS -v --stop-position="1334" mysql-bin.000001 >/data/tmp/mysqlbinlog4.sql You have new mail in /var/spool/mail/root [[email protected] mysql]# cat /data/tmp/mysqlbinlog4.sql /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200107 15:45:15 server id 1007231 end_log_pos 123 Start: binlog v 4, server v 5.7.19-log created 200107 15:45:15 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; # at 123 #200107 15:45:15 server id 1007231 end_log_pos 150 Previous-GTIDs # [empty] # at 150 #200107 15:45:53 server id 1007231 end_log_pos 211 GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:1‘/*!*/; # at 211 #200107 15:45:53 server id 1007231 end_log_pos 304 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383153/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1344274432/*!*/; 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/*!*/; create database test1 /*!*/; # at 304 #200107 15:46:38 server id 1007231 end_log_pos 365 GTID last_committed=1 sequence_number=2 rbr_only=no SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:2‘/*!*/; # at 365 #200107 15:46:38 server id 1007231 end_log_pos 629 Query thread_id=3 exec_time=0 error_code=0 use `test1`/*!*/; SET TIMESTAMP=1578383198/*!*/; CREATE TABLE `abc` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c1` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `shouji` (`c1`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!*/; # at 629 #200107 15:47:15 server id 1007231 end_log_pos 690 GTID last_committed=2 sequence_number=3 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:3‘/*!*/; # at 690 #200107 15:47:15 server id 1007231 end_log_pos 759 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383235/*!*/; BEGIN /*!*/; # at 759 #200107 15:47:15 server id 1007231 end_log_pos 803 Table_map: `test1`.`abc` mapped to number 220 # at 803 #200107 15:47:15 server id 1007231 end_log_pos 843 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=1 ### @2=1 # at 843 #200107 15:47:15 server id 1007231 end_log_pos 870 Xid = 52 COMMIT/*!*/; # at 870 #200107 15:47:23 server id 1007231 end_log_pos 931 GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:4‘/*!*/; # at 931 #200107 15:47:23 server id 1007231 end_log_pos 1000 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383243/*!*/; BEGIN /*!*/; # at 1000 #200107 15:47:23 server id 1007231 end_log_pos 1044 Table_map: `test1`.`abc` mapped to number 220 # at 1044 #200107 15:47:23 server id 1007231 end_log_pos 1093 Write_rows: table id 220 flags: STMT_END_F ### INSERT INTO `test1`.`abc` ### SET ### @1=2 ### @2=2 ### INSERT INTO `test1`.`abc` ### SET ### @1=3 ### @2=3 # at 1093 #200107 15:47:23 server id 1007231 end_log_pos 1120 Xid = 53 COMMIT/*!*/; # at 1120 #200107 15:47:30 server id 1007231 end_log_pos 1181 GTID last_committed=4 sequence_number=5 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= ‘0f943fa6-3117-11ea-a98d-005056b351ef:5‘/*!*/; # at 1181 #200107 15:47:30 server id 1007231 end_log_pos 1250 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1578383250/*!*/; BEGIN /*!*/; # at 1250 #200107 15:47:30 server id 1007231 end_log_pos 1294 Table_map: `test1`.`abc` mapped to number 220 # at 1294 #200107 15:47:30 server id 1007231 end_log_pos 1334 Delete_rows: table id 220 flags: STMT_END_F ### DELETE FROM `test1`.`abc` ### WHERE ### @1=2 ### @2=2 ROLLBACK /* added by mysqlbinlog */ /*!*/; SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET [email protected]_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
其他选项可以根据需求添加使用。
原文地址:https://www.cnblogs.com/wangb2/p/12162242.html