07:40:41(root@localhost) [(none)] show variables like 'binlog_format'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row inset (0.01 sec)
mariadb:
1 2 3 4 5 6 7
MariaDB [(none)]> show variables like 'binlog_format'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | MIXED | +---------------+-------+ 1 row inset (0.001 sec)
日志文件构成
日志文件:mysql-bin.000001,mysql-bin.000002, 二进制格式
索引文件:mysql-bin.index 文本格式,记录已有的二进制日志文件列表
1 2 3 4 5 6 7
[root@Rocky8-11 ~]# ll /var/lib/mysql/binlog* -rw-r----- 1 mysql mysql 180 May 10 20:43 /var/lib/mysql/binlog.000001 -rw-r----- 1 mysql mysql 180 May 10 20:52 /var/lib/mysql/binlog.000002 -rw-r----- 1 mysql mysql 180 May 10 20:52 /var/lib/mysql/binlog.000003 -rw-r----- 1 mysql mysql 11866 May 10 21:37 /var/lib/mysql/binlog.000004 -rw-r----- 1 mysql mysql 157 May 11 17:03 /var/lib/mysql/binlog.000005 -rw-r----- 1 mysql mysql 80 May 11 17:03 /var/lib/mysql/binlog.index
查看二进制日志文件列表及大小
1 2 3 4 5 6 7 8 9 10 11 12 13
04:53:48(root@localhost) [(none)] show binary logs; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 180 | No | | binlog.000002 | 180 | No | | binlog.000003 | 180 | No | | binlog.000004 | 11866 | No | | binlog.000005 | 201 | No | | binlog.000006 | 180 | No | | binlog.000007 | 687 | No | +---------------+-----------+-----------+ 7 rows in set (0.00 sec)
查看当前正在使用中的二进制日志文件
1 2 3 4 5 6 7
04:52:09(root@localhost) [(none)] show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000007 | 687 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
08:11:16(root@localhost) [(none)] show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 0 | +------------------+-------+ 1 row inset (0.01 sec)
修改二进制日志路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@Rocky8-11 ~]# mkdir -p /data/binlog/ [root@Rocky8-11 ~]# chown mysql. /data/binlog/ [root@Rocky8-11 ~]# ll /data total 0 drwxr-xr-x 2 mysql mysql 6 May 17 15:25 binlog
[root@Rocky8-11 ~]# vim /etc/my.cnf [mysqld] log_bin=/data/binlog/mysql-bin ## mysql-bin 为二进制日志的文件名头
[root@Rocky8-11 ~]# systemctl restart mysqld [root@Rocky8-11 ~]# ll /data/binlog total 8 -rw-r----- 1 mysql mysql 157 May 17 16:12 mysql-bin.000001 -rw-r----- 1 mysql mysql 30 May 17 16:12 mysql-bin.index
查看二进制日志内容
1 2 3 4 5 6 7 8
04:16:45(root@localhost) [(none)] show binlog events in 'mysql-bin.000001'; +------------------+-----+----------------+-----------+-------------+-----------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+-----------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 126 | Server ver: 8.0.30, Binlog ver: 4 | | mysql-bin.000001 | 126 | Previous_gtids | 1 | 157 || +------------------+-----+----------------+-----------+-------------+-----------------------------------+ 2 rows in set (0.00 sec)
[root@Rocky8-11 ~]# mysqlbinlog /data/binlog/mysql-bin.000001 # The proper term is pseudo_replica_mode, but we use this compatibility alias # to make the statement usable on server versions 8.0.24 and older. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #230517 16:12:50 server id 1 end_log_pos 126 CRC32 0x6ac5be4e Start: binlog v 4, server v 8.0.30 created 230517 16:12:50 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' goxkZA8BAAAAegAAAH4AAAABAAQAOC4wLjMwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAACCjGRkEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA CigAAU6+xWo= '/*!*/; # at 126 #230517 16:12:50 server id 1 end_log_pos 157 CRC32 0x3a5441b0 Previous-GTIDs # [empty] 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*/;
[root@Rocky8-11 ~]# mysqlbinlog /data/binlog/mysql-bin.000001 --start-position=999 --stop-position=1171 -v # The proper term is pseudo_replica_mode, but we use this compatibility alias # to make the statement usable on server versions 8.0.24 and older. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 157 #230517 16:12:50 server id 1 end_log_pos 126 CRC32 0x6ac5be4e Start: binlog v 4, server v 8.0.30 created 230517 16:12:50 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' goxkZA8BAAAAegAAAH4AAAABAAQAOC4wLjMwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAACCjGRkEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA CigAAU6+xWo= '/*!*/; # at 999 #230517 16:41:31 server id 1 end_log_pos 1066 CRC32 0x157cc5fd Table_map: `hellodb`.`teachers` mapped to number 96 # at 1066 #230517 16:41:31 server id 1 end_log_pos 1140 CRC32 0x57369f8a Update_rows: table id 96 flags: STMT_END_F
BINLOG ' O5NkZBMBAAAAQwAAACoEAAAAAGAAAAAAAAEAB2hlbGxvZGIACHRlYWNoZXJzAAQCDwH+BCwB9wEI AQHAAgEh/cV8FQ== O5NkZB8BAAAASgAAAHQEAAAAAGAAAAAAAAEAAgAE//8ABAAMAExpbiBDaGFveWluZyoBAAQADABM aW4gQ2hhb3lpbmcoAYqfNlc= '/*!*/; ### UPDATE `hellodb`.`teachers` ### WHERE ### @1=4 ### @2='Lin Chaoying' ### @3=42 ### @4=1 ### SET ### @1=4 ### @2='Lin Chaoying' ### @3=40 ### @4=1 # at 1140 #230517 16:41:31 server id 1 end_log_pos 1171 CRC32 0x60c0cb3b Xid = 33 COMMIT/*!*/; 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*/;
[root@Rocky8-13 ~]# mysqlbinlog -R --host=10.0.0.11 --user=backbinlog --password=123456 --raw --stop-never mysql-bin.000001 mysqlbinlog: [Warning] Using a password on the command line interface can be insecure.
[root@Rocky8-11 ~]# ll /data/binlog/ total 8 -rw-r----- 1 mysql mysql 495 May 17 17:17 mysql-bin.000002 -rw-r----- 1 mysql mysql 30 May 17 17:21 mysql-bin.index
[root@Rocky8-11 ~]# ll /data/binlog/ total 8 -rw-r----- 1 mysql mysql 157 May 17 17:24 mysql-bin.000001 -rw-r----- 1 mysql mysql 30 May 17 17:24 mysql-bin.index
mariadb 10.3 开启二进制日志
创建存放二进制的目录,更改权限
1 2 3 4
[root@Rocky8-12/]# mkdir -p /data/binlog [root@Rocky8-12/]# chown mysql. /data/binlog/ total 0 drwxr-xr-x 2 mysql mysql 53May2314:12 binlog
更改配置文件
1 2 3
[root@Rocky8-12 ~]# vim /etc/my.cnf [mysqld] log_bin = /data/binlog/mysql-bin
重新启动mariadb服务
1 2 3 4 5 6
[root@Rocky8-12 ~]# systemctl restart mariadb.service [root@Rocky8-12 ~]# ll /data/binlog/ total 12 -rw-rw---- 1 mysql mysql 351 May 23 14:17 mysql-bin.000001 -rw-rw---- 1 mysql mysql 328 May 23 14:17 mysql-bin.000002 -rw-rw---- 1 mysql mysql 60 May 23 14:17 mysql-bin.index
查询binlog状态(sql_log_bin、log_bin)
1 2 3 4 5 6 7 8 9 10 11 12 13
MariaDB [(none)]> show variables like '%log_bin%'; +---------------------------------+------------------------------+ | Variable_name | Value | +---------------------------------+------------------------------+ | log_bin | ON | | log_bin_basename | /data/binlog/mysql-bin | | log_bin_compress | OFF | | log_bin_compress_min_len | 256 | | log_bin_index | /data/binlog/mysql-bin.index | | log_bin_trust_function_creators | OFF | | sql_log_bin | ON | +---------------------------------+------------------------------+ 7 rows in set (0.002 sec)