Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > Mysql Binlog大文件解析

解决Mysql Binlog文件太大导致无法解析问题

作者:爱可生开源社区

这篇文章主要为大家介绍了解决Mysql Binlog文件太大导致无法解析问题,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

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

故障现象

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

故障复现

[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)

猜测

验证猜测

猜测一

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

[root@mysql mysql]# cat my.cnf | grep tmpdir
tmpdir                          = /data1/dbatemp

猜测二

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

问题分析

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

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 变量即可。

[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 &gt; 1.sql

总结

补充

原来这个问题在 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.

以上就是解决Mysql Binlog文件太大导致无法解析问题的详细内容,更多关于Mysql Binlog大文件解析的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文