MySQL binlog 作用介绍

发布时间: 更新时间: 总字数:4743 阅读时间:10m 作者: IP上海 分享 网址

MySQL 的 binlog(二进制日志) 是数据库的核心组件之一,主要用于记录所有对数据库的修改操作

介绍

  • Binlog 是 MySQL 实现高可用、数据安全和可扩展性的基石,主要服务于数据复制、灾难恢复和审计

主从复制(Replication)

  • 作用:Binlog 是实现 MySQL 主从复制的核心,主库(Master)将修改操作记录到 binlog 中,从库(Slave)读取这些日志并重放(Replay),从而实现数据同步
  • 流程
    1. 主库将数据变更(INSERTUPDATEDELETE 等)写入 binlog
    2. 从库通过 I/O 线程拉取主库的 binlog
    3. 从库的 SQL 线程解析并执行 binlog 中的操作,保持数据一致性

数据恢复(Point-in-Time Recovery)

  • 作用:结合全量备份和 binlog,可将数据库恢复到任意时间点
  • 场景
    • 误删数据时,通过全量备份 + 重放 binlog 到误操作前的时间点
    • 硬件故障导致数据丢失后,恢复至最近可用状态
  • 工具:使用 mysqlbinlog 工具解析 binlog,提取特定时间段内的 SQL 操作

记录数据变更

  • 所有 修改数据的操作(DML:INSERT/UPDATE/DELETE)
    • update 修改数据与原数据相同时,更新不生效,也不会产生 binlog
  • 部分 修改结构的操作(DDL:CREATE/ALTER/DROP)
  • 不记录 不修改数据的操作(如 SELECT)

审计与数据分析

  • 审计:通过解析 binlog,追踪数据库的历史操作,满足合规性要求
  • 数据分析:分析数据变更模式(如高频更新表)

与其他日志的协作

  • 与 Redo Log 的区别
    • Binlog:MySQL 服务层日志,记录逻辑操作,用于复制和恢复
    • Redo Log:InnoDB 引擎层日志,记录物理页修改,用于崩溃恢复
  • 两阶段提交:事务提交时,InnoDB 先写 redo log(Prepare),再写 binlog,最后提交 redo log(Commit),确保数据一致性

binlog 的格式

  • STATEMENT基于 SQL 语句的复制(statement-based replication, SBR)
    • 记录原始 SQL 语句(节省空间,但可能因函数/触发器导致主从不一致)
  • ROW基于行的复制(row-based replication, RBR),MySQL innodb 默认 binlog 格式
    • 记录每行数据的变化(更安全,兼容性更好;缺点是会产生大量的日志,如 alter table 的时日志会暴涨)
  • MIXED混合模式复制(mixed-based replication, MBR)
    • 混合模式,自动选择 STATEMENTROW

合理配置 binlog 格式(推荐 ROW)和定期清理策略,可平衡性能与存储成本。

配置与管理

  • 启用 binlog
# my.cnf 配置示例
[mysqld]
server_id = 1                        # 主从复制需唯一

expire_logs_days = 7                 # 自动清理 7 天前的日志
binlog_format = ROW                  # 推荐使用 ROW 格式
# log_bin = /var/lib/mysql/mysql-bin # 启用 binlog
log-bin=mysql-bin                    # 二进制日志文件,文件名后缀为.00000*)
log-bin-index=mysql-bin.index        # 二进制日志索引文件
max_binlog_size          = 1G        # 最大和默认值是 1G
sync-binlog              = 1

# 只记录指定的库
#binlog-do-db=db_name
# 不记录指定的库
#binlog-ignore-db=db_name
  • 常用命令
    • 查看 binlog 列表:SHOW BINARY LOGS;
    • 删除旧日志:PURGE BINARY LOGS BEFORE '2023-01-01';
    • 查看日志内容:mysqlbinlog mysql-bin.000001

binlog 常用命令

# 是否启用binlog日志
show variables like 'log_bin';

# 查看 log 类型
SHOW VARIABLES LIKE '%log_bin%';

# 查询 expire_logs_days 命令
SHOW VARIABLES LIKE 'expire_logs_days';
set global expire_logs_days=60;

查看 binlog 日志信息

# 查看本节点 binlog 的文件信息
show binary logs;

# 查看正在写入的二进制文件,及当前position
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1024 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.08 sec)

刷新 binlog

mysql > flush binary logs;

mysql> flush logs;
Query OK, 0 rows affected (0.12 sec)

mysql>  show binary logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       202 | No        |
| mysql-bin.000002 |      1024 | No        |
| mysql-bin.000003 |       128 | No        |
+------------------+-----------+-----------+
3 rows in set (0.07 sec)

查看 binlog 的详细信息

# 查询binlog日志
mysql> show binlog events;

# 查询 mysql-bin.0000xx
mysql> show binlog events in 'mysql-bin.0000xx';

# 查询 mysql-bin.0000xx,从pos点 1024 开始查起:
mysql> show binlog events in 'mysql-bin.0000xx' from 1024;

# 查询 mysql-bin.0000xx,从pos点 1024 开始查起,查询10条
mysql> show binlog events in 'mysql-bin.0000xx' from 1024 limit 10;

# 查询 mysql-bin.0000xx,从pos点 1024 开始查起,偏移2行,查询10条
mysql> show binlog events in 'mysql-bin.0000xx' from 1024 limit 2,10;

binlog 相关参数

  • binlog_chache_size: 默认 32768
  • max_binlog_cache_size
  • sync_binlog 控制 binlog 写入磁盘
    • sync_binlog = 0 时,表示 innodb 不会主动控制将 binlog 落盘,innodb 仅仅会将 binlog 写入到 OS Cache 中,至于什么时间将 binlog 刷入磁盘中完全依赖于操作系统
    • sync_binlog = 1 时,表示事物 commit 时将 binlog 落盘(推荐)
    • sync_binlog=N 当 N 大于 1 时,表示开启 组提交(group commit),如 N=5,那 MySQL 就会等收集 5 个 binlog 后再将这 5 个 binlog 同步到磁盘上
  • binlog_rows_query_log_events=ON 启用,可以显示查询日志
mysql> show variables like '%binlog_cache%';
+-----------------------+----------------------+
| Variable_name         | Value                |
+-----------------------+----------------------+
| binlog_cache_size     | 32768                |
| max_binlog_cache_size | 18446744073709547520 |
+-----------------------+----------------------+
2 rows in set (0.00 sec)

relaylog

在从节点上,内容和 binlog 一致,是从主节点同步过来的

mysql > show slave status;
...
               Relay_Log_File: relay-bin.xxxxxx
                Relay_Log_Pos: 1024
        Relay_Master_Log_File: mysql-bin.xxxxxx
...

mysql > show relaylog events in 'relay-bin.xxxxxx' from 1024 limit 10\G;

二阶段提交

MySQL 事务在提交的时候,会记录 事务日志(redo log)二进制日志(binlog)

mysql 二阶段提交

图片参考

MySQL 将 redo log 的写入拆成了两个步骤:preparecommit,即两阶段提交。两阶段提交的目的是为了让两份日志之间的逻辑一致。

binlog 分析工具

mysqlbinlog

安装

apt install mariadb-server
# or
apt install mysql-server

help

mysqlbinlog--help ...
$ mysqlbinlog --help
mysqlbinlog --help
mysqlbinlog  Ver 8.0.41-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Dumps a MySQL binary log in a format usable for viewing or for piping to
the mysql command line client.

Usage: mysqlbinlog [options] log-files
  -?, --help          Display this help and exit.
  --base64-output=name
                      Determine when the output statements should be
                      base64-encoded BINLOG statements: 'never' disables it and
                      works only for binlogs without row-based events;
                      'decode-rows' decodes row events into commented
                      pseudo-SQL statements if the --verbose option is also
                      given; 'auto' prints base64 only when necessary (i.e.,
                      for row-based events and format description events).  If
                      no --base64-output[=name] option is given at all, the
                      default is 'auto'.
  --bind-address=name IP address to bind to.
  --character-sets-dir=name
                      Directory for character set files.
  -d, --database=name List entries for just this database (local log only).
  --rewrite-db=name   Rewrite the row event to point so that it can be applied
                      to a new database
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  --debug-check       This is a non-debug version. Catch this and exit.
  --debug-info        This is a non-debug version. Catch this and exit.
  --default-auth=name Default authentication client-side plugin to use.
  -D, --disable-log-bin
                      Disable binary log. This is useful, if you enabled
                      --to-last-log and are sending the output to the same
                      MySQL server. This way you could avoid an endless loop.
                      You would also like to use it when restoring after a
                      crash to avoid duplication of the statements you already
                      have. NOTE: you will need a SUPER privilege to use this
                      option.
  -F, --force-if-open If the IN_USE flag is set in the first event, run
                      anyways, and do not fail in case the file ends with a
                      truncated event. The IN_USE flag is set only for the
                      binary log that is currently written by the server; in
                      case the server has crashed, the flag remains set until
                      the server is started up again and recovers the binary
                      log. Without -F, mysqlbinlog refuses to process file with
                      the flag set. Since the server may be writing the file,
                      it is considered normal that the last event is truncated.
                      (Defaults to on; use --skip-force-if-open to disable.)
  -f, --force-read    Force reading unknown binlog events.
  -H, --hexdump       Augment output with hexadecimal and ASCII event dump.
  -h, --host=name     Get the binlog from server.
  -i, --idempotent    Notify the server to use idempotent mode before applying
                      Row Events
  -l, --local-load=name
                      Prepare local temporary files for LOAD DATA INFILE in the
                      specified directory.
  -o, --offset=#      Skip the first N entries.
  -p, --password[=name]
                      Password to connect to remote server.
  --plugin-dir=name   Directory for client-side plugins.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -R, --read-from-remote-server
                      Read binary logs from a MySQL server. This is an alias
                      for read-from-remote-source=BINLOG-DUMP-NON-GTIDS.
  --read-from-remote-master=name
                      This option is deprecated and will be removed in a future
                      version. Use read-from-remote-source instead.
  --read-from-remote-source=name
                      Read binary logs from a MySQL server through the
                      COM_BINLOG_DUMP or COM_BINLOG_DUMP_GTID commands by
                      setting the option to either BINLOG-DUMP-NON-GTIDS or
                      BINLOG-DUMP-GTIDS, respectively. If
                      --read-from-remote-source=BINLOG-DUMP-GTIDS is combined
                      with --exclude-gtids, transactions are filtered out on
                      the source, to avoid unnecessary network traffic.
  --raw               Requires -R. Output raw binlog data instead of SQL
                      statements, output is to log files.
  -r, --result-file=name
                      Direct output to a given file. With --raw this is a
                      prefix for the file names.
  --server-id=#       Extract only binlog entries created by the server having
                      the given id.
  --server-id-bits=#  Set number of significant bits in server-id
  --set-charset=name  Add 'SET NAMES character_set' to the output.
  -s, --short-form    Just show regular queries: no extra info and no row-based
                      events. This is for testing only, and should not be used
                      in production systems. If you want to suppress
                      base64-output, consider using --base64-output=never
                      instead.
  -S, --socket=name   The socket file to use for connection.
  --server-public-key-path=name
                      File path to the server public RSA key in PEM format.
  --get-server-public-key
                      Get server public key
  --ssl-mode=name     SSL connection mode.
  --ssl-ca=name       CA file in PEM format.
  --ssl-capath=name   CA directory.
  --ssl-cert=name     X509 cert in PEM format.
  --ssl-cipher=name   SSL cipher to use.
  --ssl-key=name      X509 key in PEM format.
  --ssl-crl=name      Certificate revocation list.
  --ssl-crlpath=name  Certificate revocation list path.
  --tls-version=name  TLS version to use, permitted values are: TLSv1.2,
                      TLSv1.3
  --ssl-fips-mode=name
                      SSL FIPS mode (applies only for OpenSSL); permitted
                      values are: OFF, ON, STRICT
  --tls-ciphersuites=name
                      TLS v1.3 cipher to use.
  --ssl-session-data=name
                      Session data file to use to enable ssl session reuse
  --ssl-session-data-continue-on-failed-reuse
                      If set to ON, this option will allow connection to
                      succeed even if session data cannot be reused.
  --start-datetime=name
                      Start reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  -j, --start-position=#
                      Start reading the binlog at position N. Applies to the
                      first binlog passed on the command line.
  --stop-datetime=name
                      Stop reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --stop-never        Wait for more data from the server instead of stopping at
                      the end of the last log. Implicitly sets --to-last-log
                      but instead of stopping at the end of the last log it
                      continues to wait till the server disconnects.
  --stop-never-slave-server-id=#
                      The server_id that is reported when connecting to a
                      source server when using --read-from-remote-server
                      --stop-never. This option is deprecated and will be
                      removed in a future version. Use connection-server-id
                      instead.
  --connection-server-id=#
                      The server_id that will be reported when connecting to a
                      source server when using --read-from-remote-server. This
                      option cannot be used together with
                      stop-never-slave-server-id.
  --stop-position=#   Stop reading the binlog at position N. Applies to the
                      last binlog passed on the command line.
  -t, --to-last-log   Requires -R. Will not stop at the end of the requested
                      binlog but rather continue printing until the end of the
                      last binlog of the MySQL server. If you send the output
                      to the same MySQL server, that may lead to an endless
                      loop.
  -u, --user=name     Connect to the remote server as username.
  -v, --verbose       Reconstruct pseudo-SQL statements out of row events. -v
                      -v adds comments on column data types.
  -V, --version       Print version and exit.
  --open-files-limit=#
                      Used to reserve file descriptors for use by this program.
  -c, --verify-binlog-checksum
                      Verify checksum binlog events.
  --binlog-row-event-max-size=#
                      The maximum size of a row-based binary log event in
                      bytes. Rows will be grouped into events smaller than this
                      size if possible. This value must be a multiple of 256.
  --skip-gtids        Do not preserve Global Transaction Identifiers; instead
                      make the server execute the transactions as if they were
                      new.
  --include-gtids=name
                      Print events whose Global Transaction Identifiers were
                      provided.
  --exclude-gtids=name
                      Print all events but those whose Global Transaction
                      Identifiers were provided.
  --print-table-metadata
                      Print metadata stored in Table_map_log_event
  -C, --compress      Use compression in server/client protocol.
  --compression-algorithms=name
                      Use compression algorithm in server/client protocol.
                      Valid values are any combination of
                      'zstd','zlib','uncompressed'.
  --zstd-compression-level=#
                      Use this compression level in the client/server protocol,
                      in case --compression-algorithms=zstd. Valid range is
                      between 1 and 22, inclusive. Default is 3.
  --require-row-format
                      Fail when printing an event that was not logged using row
                      format or other forbidden events like Load instructions
                      or the creation/deletion of temporary tables.

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysqlbinlog client
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
--no-defaults           Don't read default options from any option file,
                        except for login file.
--defaults-file=#       Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
                        Also read groups with concat(group, suffix)
--login-path=#          Read this path from the login file.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}          Value (after reading options)
----------------------------------------- --------------------------------
base64-output                             (No default value)
bind-address                              (No default value)
character-sets-dir                        (No default value)
database                                  (No default value)
rewrite-db                                (No default value)
default-auth                              (No default value)
disable-log-bin                           FALSE
force-if-open                             TRUE
force-read                                FALSE
hexdump                                   FALSE
host                                      (No default value)
idempotent                                FALSE
local-load                                (No default value)
offset                                    0
plugin-dir                                (No default value)
port                                      0
read-from-remote-server                   FALSE
read-from-remote-master                   (No default value)
read-from-remote-source                   (No default value)
raw                                       FALSE
result-file                               (No default value)
server-id                                 0
server-id-bits                            32
set-charset                               (No default value)
short-form                                FALSE
socket                                    (No default value)
server-public-key-path                    (No default value)
get-server-public-key                     FALSE
ssl-ca                                    (No default value)
ssl-capath                                (No default value)
ssl-cert                                  (No default value)
ssl-cipher                                (No default value)
ssl-key                                   (No default value)
ssl-crl                                   (No default value)
ssl-crlpath                               (No default value)
tls-version                               (No default value)
tls-ciphersuites                          (No default value)
ssl-session-data                          (No default value)
ssl-session-data-continue-on-failed-reuse FALSE
start-datetime                            (No default value)
start-position                            4
stop-datetime                             (No default value)
stop-never                                FALSE
stop-never-slave-server-id                -1
connection-server-id                      -1
stop-position                             18446744073709551615
to-last-log                               FALSE
user                                      (No default value)
open-files-limit                          64
verify-binlog-checksum                    FALSE
binlog-row-event-max-size                 4294967040
skip-gtids                                FALSE
include-gtids                             (No default value)
exclude-gtids                             (No default value)
print-table-metadata                      FALSE
compress                                  FALSE
compression-algorithms                    (No default value)
zstd-compression-level                    3
require-row-format                        FALSE

常用参数:

  • --start-position=1024 起始 pos 点
  • --stop-position=2048 结束 pos 点
  • --start-datetime="2015-05-01 00:00:00" 起始时间点
  • --stop-datetime="2015-05-01 00:00:00" 结束时间点
  • --database=test 指定只恢复 test 数据库

常用命令

# 查看混合模式的二进制文件,默认使用 base64 编码
mysqlbinlog mysql-bin.001024 > mysql-bin.001024.sql

# 将 base64 转换成 sql
mysqlbinlog --base64-output=decode-rows -vv mysql-bin.001024 > mysql-bin.001024.sql

mysqlbinlog --start-position='4'  --stop-position='100' mysqlbinlog.001024 > 001024.log;
mysqlbinlog --start-datetime='2015-05-01 00:00:00' mysqlbinlog.001024 > 001024.log;

binlog 分析

  • 针对基于 binlog 文件和位置
# 统计操作频繁的表,其中 column 命令来自 apt install bsdmainutils
mysqlbinlog --no-defaults --base64-output=decode-rows -v -v mysql-bin.001024 | awk '/###/{if($0~/UPDATE|INSERT|DELETE/)count[$2" "$NF]++}END{for(i in count)print i,"\t",count[i]}' | column -t | sort -k3nr | more
  • 针对基于 gtid 的?统计 insert、update、delete 执行语句以及每个表执行次数
$ mysqlbinlog --base64-output=DECODE-ROWS -v /usr/local/mnt/mysql/log/mysql-bin.xxxxxx  > xxxxxx.sql

$ awk '
/INSERT INTO `/ {match($0, /`([^`]*)`\.`([^`]*)`/, arr); print "INSERT\t" arr[1] "." arr[2]}
/UPDATE `/ {match($0, /`([^`]*)`\.`([^`]*)`/, arr); print "UPDATE\t" arr[1] "." arr[2]}
/DELETE FROM `/ {match($0, /`([^`]*)`\.`([^`]*)`/, arr); print "DELETE\t" arr[1] "." arr[2]}
' xxxxxx.sql | sort | uniq -c | awk '{print $2, $3, $1}' OFS='\t'

analysis_binlog

git clone https://gitee.com/mo-shan/analysis_binlog.git
cd analysis_binlog

# 将这里的mysqlbinlog_path改成mysqlbinlog工具的绝对路径,否则可能会因版本太低导致错误
sed -i 's#^mysqlbinlog="/data/mysql/base/bin/mysqlbinlog"#mysqlbinlog=\"/mysqlbinlog_path\"#g' bin/analysis_binlog

# 将这里的analysis_binlog_path改成analysis_binlog的家目录的绝对路径
sed -i 's#^work_dir=.*#work_dir=\"/analysis_binlog_path\"#g' bin/analysis_binlog

# 赋权
chmod +x bin/analysis_binlog
echo "export PATH=$(pwd)/bin:${PATH}" >> ${HOME}/.bashrc

# 统计操作表 SQL 类型,如 select、update、delete、insert
# -w : 指定并行数, 当需要分析多个binlog文件时该参数有效, 默认是1
# -t : 指定显示结果的格式/内容, 供选选项有"detail|simple"
# -s : 指定排序规则, 供选选项有"insert|update|delete"
analysis_binlog -bfile=/path-to/mysql-bin.000xxx,/path-to/mysql-bin.000xxy -w=2 -t=simple

统计结果在 /<path-to>/analysis_binlog/res/mysql-bin.000xxx.res

F&Q

binlog 太大占用大量磁盘空间的解决方法

  • binlog 会日渐增大,占用很大的磁盘空间,因此,要对 binlog 使用正确安全的方法清理掉一部分没用的日志

帮助命令:

mysql> help purge
Name: 'PURGE BINARY LOGS'
Description:
Syntax:
PURGE { BINARY | MASTER } LOGS
    { TO 'log_name' | BEFORE datetime_expr }

The binary log is a set of files that contain information about data
modifications made by the MySQL server. The log consists of a set of
binary log files, plus an index file (see
http://dev.mysql.com/doc/refman/5.7/en/binary-log.html).

The PURGE BINARY LOGS statement deletes all the binary log files listed
in the log index file prior to the specified log file name or date.
BINARY and MASTER are synonyms. Deleted log files also are removed from
the list recorded in the index file, so that the given log file becomes
the first in the list.

This statement has no effect if the server was not started with the
--log-bin option to enable binary logging.

URL: http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html

Examples:
PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';
# 0 表示永不过期
mysql> show variables like '%expire_logs_days%';
  • 解决方式:设置保留 7 天
mysql> set global expire_logs_days=7;

# 永久生效,在 my.cnf 中设置
[mysqld]
expire_logs_days = 7
  • 手动清理日志
mysql> PURGE BINARY LOGS TO 'binlog.1000xxx'
mysql> PURGE BINARY LOGS TO 'binlog.000001';

# 删除到指定 binlog
mysql> PURGE BINARY LOGS BEFORE '2015-06-22 12:00:00';

# 删除3天前的日志
mysql> PURGE BINARY LOGS BEFORE DATE_SUB( NOW(), INTERVAL 3 DAY);

实战,查看主库和从库正在使用的 binlog 是哪个文件

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000049
         Position: 763545655
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.20.20.22
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000049
          Read_Master_Log_Pos: 763860016
               Relay_Log_File: gse-2-relay-bin.000142
                Relay_Log_Pos: 763859782
        Relay_Master_Log_File: mysql-bin.000049
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...
             Master_Server_Id: 1
                  Master_UUID: fe29dfe3-d853-11e9-8eda-6c92bf9969ab
             Master_Info_File: ../mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           Master_Retry_Count: 86400
...
1 row in set (0.01 sec)
  • 清理方法一:删除指定日期以前的日志索引中 binlog 日志文件
purge master logs before '2018-12-01 17:20:00';
PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY);  # 删除10天前日志
35 2 * * * mysql -e "PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY);" >/dev/null 2>&1
  • 清理方法二:删除指定日志文件的日志索引中 binlog 日志文件
purge master logs to'mysql-bin.000048';

注意:

  • 时间和文件名一定不可以写错,尤其是时间中的年和文件名中的序号,以防不小心将正在使用的 binlog 删除!!!
  • 切勿删除正在使用的 binlog!!!

自动清理 binlog

使用如下方法查询当前 binlog 的过期时间,若为 0 表示不过期

mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 0     |
+------------------+-------+
1 row in set (0.00 sec)

使用如下方法设置binlog过期时间,设置 30 表示 30 天后自动清理之前的过期日志

mysql> set global expire_logs_days = 30;

注意:

  • 过期时间设置的要适当,对于主从复制,要看从库的延迟决定过期时间,避免主库 binlog 还未传到从库便因过期而删除,导致主从不一致!

或通过配置文件配置

vim /etc/my.cnf
expire_logs_days = x  # binlog 自动删除的天数。默认值为0,表示`没有自动删除`

mysqlbinlog: unknown variable ‘default-character-set=utf8’

mysqlbinlog --no-defaults mysql-bin.000xxx

或修改 mysql 的字符集并重启服务

# /etc/my.cnf

[mysqld]
character-set-server = utf8

参考

  1. https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html#sysvar_binlog_format
  2. https://dev.mysql.com/doc/refman/8.4/en/replication-options-binary-log.html#sysvar_binlog_format
  3. https://dev.mysql.com/doc/refman/8.4/en/binary-log-setting.html
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数