Binlog 太大导致无法解析怎么办?

由于业务写入了一条大事务,导致 MySQL 的 binlog 膨胀。在解析大的 binlog 时,经常会遇到这个问题,导致无法解析,没有其他工具的情况下,很难分析问题。

作者:孙绪宗,新浪微博 DBA 团队工程师,主要负责 MySQL、PostgreSQL 等关系型数据库运维。

爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。

本文共 3200 字,预计阅读需要 10 分钟。

故障现象

由于业务写入了一条大事务,导致 MySQL 的 binlog 膨胀。在解析大的 binlog 时,经常会遇到这个问题,导致无法解析,没有其他工具的情况下,很难分析问题。

故障复现

bash 复制代码
[root@xuzong mysql]# ls -lh mysql-bin.003300
-rw-r----- 1 my3696 mysql 6.7G Oct 30 16:24 mysql-bin.003300
[root@xuzong mysql]# /usr/local/mysql-5.7.35/bin/mysqlbinlog -vv mysql-bin.003300 > 1.sql
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.334z3P' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)
mysqlbinlog: Error writing file '/tmp/tmp.0Uirch' (Errcode: 28 - No space left on device)

猜测

  1. 可能是配置文件中 tmpdir 的问题,但是修改这个得重启 MySQL。
  2. 能不能在不重启 MySQL 的情况下,修改这个临时空间。

验证猜测

猜测一

看一下 my.cnf 设置的 tmpdir,发现并不是使用的这个参数,看来猜测一不对。

bash 复制代码
[root@mysql mysql]# cat my.cnf | grep tmpdir
tmpdir                          = /data1/dbatemp

猜测二

网上搜了一下,大部分是讲临时表满怎么解决的,也就是猜测一的方案,并没有很明确的方法来修改 mybinlog 解析时,所使用的的临时句柄占用空间。

问题分析

只能看看源码,看一下 mysqlbinlog 到底是怎么获取 tmpdir 的。

c 复制代码
mysqbinlog.cc

int main(int argc, char** argv)
{
........
  MY_TMPDIR tmpdir;
  tmpdir.list= 0;
  if (!dirname_for_local_load)
  {
    if (init_tmpdir(&tmpdir, 0))
      exit(1);
    dirname_for_local_load= my_strdup(PSI_NOT_INSTRUMENTED,
                                      my_tmpdir(&tmpdir), MY_WME);
  }
........
}

mf_tempdir.cc

my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
{
  char *end, *copy;
  char buff[FN_REFLEN];
  DBUG_ENTER("init_tmpdir");
  DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL"));

  Prealloced_array<char*, 10, true> full_list(key_memory_MY_TMPDIR_full_list);

  memset(tmpdir, 0, sizeof(*tmpdir));
  if (!pathlist || !pathlist[0])
  {
    /* Get default temporary directory */
    pathlist=getenv("TMPDIR");	/* Use this if possible */ //这里能看到是获取的机器环境变量
#if defined(_WIN32)
    if (!pathlist)
      pathlist=getenv("TEMP"); //windows是temp
    if (!pathlist)
      pathlist=getenv("TMP");  //linux是tmp
#endif
    if (!pathlist || !pathlist[0])
      pathlist= DEFAULT_TMPDIR;
  }
........
}

好家伙,竟然是获取的机器环境变量,那么这个问题就解决了。

问题处理

临时修改一下机器的 tmpdir 变量即可。

bash 复制代码
[root@mysql mysql]# export TMPDIR="/data1"
[root@mysql mysql]# echo ${TMPDIR:-/tmp}
[root@xuzong mysql]# /usr/local/mysql-5.7.35/bin/mysqlbinlog -vv mysql-bin.003300 > 1.sql

总结

  1. 有问题还是要看看源码。
  2. 可以考虑使用 binlog 解析工具,比如 bin2sql 解决问题。
  3. 可以看看慢日志里是否有记录。

补充

原来这个问题在 MySQL 官方手册 中有所描述,在此做一个补充。

When running mysqlbinlog against a large binary log, be careful that the filesystem has enough space for the resulting files. To configure the directory that mysqlbinlog uses for temporary files, use the TMPDIR environment variable.

更多技术文章,请访问:opensource.actionsky.com/

关于 SQLE

SQLE 是一款全方位的 SQL 质量管理平台,覆盖开发至生产环境的 SQL 审核和管理。支持主流的开源、商业、国产数据库,为开发和运维提供流程自动化能力,提升上线效率,提高数据质量。

相关推荐
+VX:Fegn08957 分钟前
计算机毕业设计|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计·旅游
百锦再10 分钟前
国产数据库的平替亮点——关系型数据库架构适配
android·java·前端·数据库·sql·算法·数据库架构
wusp199413 分钟前
基于vite + nodejs + MongoDB + vue2 的博客发布系统
数据库·mongodb
全栈小513 分钟前
【数据库】浙人医携手金仓数据库,打造全国首个多院区异构多活容灾架构
数据库·1024程序员节·金仓
studytosky44 分钟前
Linux 基础开发工具(3):Git 控制与 GDB 调试实用指南
linux·运维·服务器·网络·数据库·git
凌睿马1 小时前
关于复杂数据结构从MySQL迁移到PostgreSQL的可行性
数据结构·数据库·mysql
是一个Bug1 小时前
声明式事务:深度解析与实战指南
数据库·oracle
laocooon5238578861 小时前
C#二次开发中简单块的定义与应用
android·数据库·c#
不穿格子的程序员1 小时前
Redis篇4——Redis深度剖析:内存淘汰策略与缓存的三大“天坑”
数据库·redis·缓存·雪崩·内存淘汰策略
hans汉斯2 小时前
【软件工程与应用】平移置换搬迁系统设计与实现
数据库·人工智能·系统架构·软件工程·汉斯出版社·软件工程与应用