Mysql binary log binlog command

The first section:

binlog configuration:

Note: MySQL does not enable binary logging by default.

Basic parameters view:

switch:

[(none)]>select @@log_bin;

Log path and name

[(none)]>select @@log_bin_basename;

Service ID number:

[(none)]>select @@server_id;

Binary log format:

[(none)]>select @@binlog_format;

Double One Standard Two:

[(none)]>select @@sync_binlog;


binlog

1. Function:

Cooperate with backup and restore data log, it is the prerequisite of master-slave replication

2. Configuration parameters

server_id=8

log_bin=/data/binlog/mysql-bin

This parameter is created first, and you must remember to authorize it

binlog_format=row #Record the binary format, mainly for variable statements and row patterns

server_id=3306 

Mainly it must be added during the master-slave replication process, but in version 5.7, the following parameters (log_bin) must be used to turn on the binlog log, even if it is a stand-alone machine, it must be added

log_bin=/data/binlog/mysql-bin

(1) Turn on the binary log function

(2) Set the binary log directory and name prefix

binlog_format=row

The record format of binlog??

3. Event

The smallest unit of binary logging

Start position number

End position number

Position:

Start logo: at 194

End flag: end_log_pos 254

194? 254?

The relative position number of an event in the binlog, what is the role of the position number?

In order to facilitate our interception of events, mainly intercept the binary log

For DDL, DCL, a statement is an event

For DML statements: only records committed transactions.


4. What sentence to record

DDL: Record the current DDL (statement method) intact.

DCL: Record the current DDL (statement method) intact.

DML: ROW mode

row 和语句模式的优缺点

statement:可读性较高,日志量少,但是不够严谨

row:行模式,行的变化,记录数据比较准确,可读性很低,日志量大,足够严谨


5、日志查看

show binary logs 查看一共多少个binlog

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

show binlog enents in ‘xxx’ 查看当前事件来截取二进制 的启点和终点

enents 又扩展到了form 和limit 来查询

可以更快速的过滤

mysql -e "show binlog enents in ‘xxx’ form   limit"


mysqlbinlog  -d --start-position  --stop-position --start-datetime --stop-datetime 

--base64-output=decode-rows -vvv



Master [binlog]>show binlog events in 'mysql-bin.000003';

+------------------+-----+----------------+-----------+-------------+----------------------------------------+

| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                   |

+------------------+-----+----------------+-----------+-------------+----------------------------------------+

| mysql-bin.000003 |   4 | Format_desc    |         6 |         123 | Server ver: 5.7.20-log, Binlog ver: 4  |

| mysql-bin.000003 | 123 | Previous_gtids |         6 |         154 |                                        |

| mysql-bin.000003 | 154 | Anonymous_Gtid |         6 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |

| mysql-bin.000003 | 219 | Query          |         6 |         319 | create database binlog                 |

| mysql-bin.000003 | 319 | Anonymous_Gtid |         6 |         384 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |

| mysql-bin.000003 | 384 | Query          |         6 |         486 | use `binlog`; create table t1 (id int) |

+------------------+-----+----------------+-----------+-------------+--------------------------------------

Log_name:binlog文件名

Pos:开始的position    *****

Event_type:事件类型

Format_desc:格式描述,每一个日志文件的第一个事件,多用户没有意义,MySQL识别binlog必要信息

Server_id:mysql服务号标识

End_log_pos:事件的结束位置号 *****

Info:事件内容*****

补充:

SHOW BINLOG EVENTS

   [IN 'log_name']

   [FROM pos]

   [LIMIT [offset,] row_count]

[root@db01 binlog]# mysql -e "show binlog events in 'mysql-bin.000004'" |grep drop


6、截取日志

重点就是启点和终点

[root@db01 binlog]# mysqlbinlog --start-datetime='2020-12-26 17:00:00' --stop-datetime='2020-12-26 17:01:00'  /data/binlog/mysql-bin.000004 


7、用gtid截取二进制日志,当日志量比较大的时候用gtid比较方便

重要参数介绍:

vim /etc/my.cnf

gtid-mode=on

enforce-gtid-consistency=true

systemctl restart mysqld


基于GTID进行查看binlog

具备GTID后,截取查看某些事务日志:

--include-gtids

--exclude-gtids

mysqlbinlog --include-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:1-6' 

--exclude-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:4'  /data/binlog/mysql-bin.000004


Guess you like

Origin blog.51cto.com/15127516/2657681