未解决Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Err

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: <div id="header" style="background-color:rgb(0,78,97); font-family:Verdana,'Lucida Grande','Lucida Sans Unicode',Tahoma,Arial,sans-serif; line-height:19px"> <div id="logo" style="padding:10px"><a

Bug #62055 Race condition in check_temp_dir() from multiple mysqld instances
Submitted: 2 Aug 2011 10:24 Modified: 19 Dec 2011 1:29
Reporter: Yoshinori Matsunobu (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category: MySQL Server: Replication Severity: S3 (Non-critical)
Version: 5.1.59, 5.5.15 OS: Any
Assigned to:   Target Version:  
Triage: Needs Triage: D3 (Medium)

[2 Aug 2011 10:24] Yoshinori Matsunobu
Description:
In check_temp_dir(), mysqld creates a temporary file and removes it immediately, without protecting any mutex/lockfile/etc. 
--------------
  /*
    Check if the directory exists.
   */
  if (!(dirp=my_dir(tmp_dir,MYF(MY_WME))))
    DBUG_RETURN(1);
  my_dirend(dirp);

  /*
    Check permissions to create a file.
   */
  if ((fd= mysql_file_create(key_file_misc,
                             tmp_file, CREATE_MODE,
                             O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW,
                             MYF(MY_WME))) < 0)
  DBUG_RETURN(1);

  /*
    Clean up.
   */
  mysql_file_close(fd, MYF(0));
--------------

check_temp_dir() is called at initializing SQL thread. 
  if (check_temp_dir(rli->slave_patternload_file))
The filename is fixed to SQL_LOAD-.

A problem might happen when multiple mysqld instances on the same host start SQL thread at the same time. SQL thread might abort with the following error.

110802 00:00:00 [ERROR] Slave SQL: Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Errcode: 17), Error_code: 1

Setting tmpdir separately from each mysqld instance is certainly a workaround for this issue, but this should be a problem anyway.

How to repeat:
See above
[2 Aug 2011 11:09] Valeriy Kravchuk
Thank you for the problem report. Verified by code review.
[19 Dec 2011 1:29] Jon Stephens
Documented fix in the 5.6.5 changelog as follows:

      A race condition could occur when running multiple instances of mysqld on
      a single machine, when more than slave thread was started at the same
      time, and each such thread tried to use the same temporary file.

Closed.
[19 Dec 2011 1:29] Jon Stephens
Thank you for your bug report. This issue has been committed to our source repository of that product and will be incorporated into the next release.

If necessary, you can access the source repository and build the latest available version, including the bug fix. More information about accessing the source trees is available at

    http://dev.mysql.com/doc/en/installing-source.html
 
  

2016-03-09 10:22:28 15680 [Note] Event Scheduler: Loaded 0 events
2016-03-09 10:22:28 15680 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.6.10-log'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
2016-03-09 10:22:28 15680 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000001' at position 151, relay log './t1-relay-bin.000001' position: 4
2016-03-09 10:22:28 15680 [ERROR] Slave SQL: Unable to use slave's temporary directory /tmp                                                          | - Can't read dir of '/tmp                                                          |/' (Errcode: 2 - No such file or directory), Error_code: 12


Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Errcode: 17)

这个错误时在Mysql主从配置产生的,最后找到这个Mysql的一个bug

http://bugs.mysql.com/bug.php?id=62055

bug的主要原因是:打开文件的函数中指定打开模式时,如果O_CREAT和O_EXCL同时指定,那么当文件存在时会导致打开文件出错,这个使用方法本来也没有什么错误,但是当使用Mysql主从备份机制,在一台服务器上安装多个mysqld实例时,就会出问题,代码在Mysql源码中/sql/slave.cc文件中,Mysql5.1.68是在2904行

复制代码
/*
  Check the temporary directory used by commands like
  LOAD DATA INFILE.
 */
static 
int check_temp_dir(char* tmp_file)
{
  int fd;
  MY_DIR *dirp;
  char tmp_dir[FN_REFLEN];
  size_t tmp_dir_size;

  DBUG_ENTER("check_temp_dir");

  /*
    Get the directory from the temporary file.
  */
  dirname_part(tmp_dir, tmp_file, &tmp_dir_size);

  /*
    Check if the directory exists.
   */
  if (!(dirp=my_dir(tmp_dir,MYF(MY_WME))))
    DBUG_RETURN(1);
  my_dirend(dirp);

  /*
    Check permissions to create a file.
   */
  if ((fd= my_create(tmp_file, CREATE_MODE,
                     O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW,
                     MYF(MY_WME))) < 0)
  DBUG_RETURN(1);

  /*
    Clean up.
   */
  my_close(fd, MYF(0));
  my_delete(tmp_file, MYF(0));

  DBUG_RETURN(0);
}
复制代码

上面红色的是调用了一个函数打开文件,my_create,在这个函数中第三个参数传递了O_EXCL,但是并没有O_CREAT,下面继续看my_create函数,它在/mysys/my_create.c文件中定义

复制代码
File my_create(const char *FileName, int CreateFlags, int access_flags,
           myf MyFlags)
{
  int fd, rc;
  DBUG_ENTER("my_create");
  DBUG_PRINT("my",("Name: '%s' CreateFlags: %d  AccessFlags: %d  MyFlags: %d",
           FileName, CreateFlags, access_flags, MyFlags));

#if !defined(NO_OPEN_3)
  fd = open((char *) FileName, access_flags | O_CREAT,
        CreateFlags ? CreateFlags : my_umask);
#elif defined(VMS)
  fd = open((char *) FileName, access_flags | O_CREAT, 0,
        "ctx=stm","ctx=bin");
#elif defined(__WIN__)
  fd= my_sopen((char *) FileName, access_flags | O_CREAT | O_BINARY,
           SH_DENYNO, MY_S_IREAD | MY_S_IWRITE);
#else
  fd = open(FileName, access_flags);
#endif

  if ((MyFlags & MY_SYNC_DIR) && (fd >=0) &&
      my_sync_dir_by_file(FileName, MyFlags))
  {
    my_close(fd, MyFlags);
    fd= -1;
  }

  rc= my_register_filename(fd, FileName, FILE_BY_CREATE,
                           EE_CANTCREATEFILE, MyFlags);
  /*
    my_register_filename() may fail on some platforms even if the call to
    *open() above succeeds. In this case, don't leave the stale file because
    callers assume the file to not exist if my_create() fails, so they don't
    do any cleanups.
  */
  if (unlikely(fd >= 0 && rc < 0))
  {
    int tmp= my_errno;
    my_delete(FileName, MyFlags);
    my_errno= tmp;
  }
  
  DBUG_RETURN(rc);
} /* my_create */
复制代码

红色的字体部分代码是为了实现跨平台,其中默认是蓝色字体代码,可以明显的看到,这时将O_CREAT添加进来了,此时就造成了O_CREAT和O_EXCL同时使用了。

在POSIX关于open函数的文档中可以看到,当O_CREAT和O_EXCL同时使用时,如果文件存在就会失败。

http://linux.die.net/man/3/open

 

参考:
http://www.cnblogs.com/lit10050528/p/4155325.html

总结不要使用 5.6.10 5.1.59, 5.5.15 版本 
使用主从切换的时候,会遇到这个问题。


 
 
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
8月前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
67 0
|
4月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 关系型数据库 MySQL
【SQL异常】启动MySQL报错:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
【SQL异常】启动MySQL报错:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
141 1
|
SQL 人工智能 Java
SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
|
SQL 安全 Java
6. 成功解决:Driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' could not be found, make sure the 'MS SQL Server (Native)' driver (jar file) is installed.
在使用 Kettle(Spoon) 工具创建 SQL Server 数据库连接时,提示:Driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' could not be found, make sure the 'MS SQL Server (Native)' driver (jar file) is installed. com.microsoft.sqlserver.jdbc.SQLServerDriver
1613 1
|
SQL 关系型数据库 MySQL
Can‘‘t connect to MySQL server on localhost (10061)以及忘机sql密码和用户名的解决方法
Can‘‘t connect to MySQL server on localhost (10061)以及忘机sql密码和用户名的解决方法
|
SQL 关系型数据库 MySQL
springboot-plus 导入starter-mysql.sql时出现[Err] 1064 - You have an error in your SQL syntax; check the
springboot-plus 导入starter-mysql.sql时出现[Err] 1064 - You have an error in your SQL syntax; check the
161 0
springboot-plus 导入starter-mysql.sql时出现[Err] 1064 - You have an error in your SQL syntax; check the
|
存储 关系型数据库 MySQL
MySQL问题解决[Err] 1005 - Can't create table '.\ \#sql-b34_61.frm' (errno: 150)M
MySQL问题解决[Err] 1005 - Can't create table '.\ \#sql-b34_61.frm' (errno: 150)M
469 0
|
SQL 关系型数据库 MySQL
MySQL运行SQL:[ERR] 1231 - Variable ‘time_zone‘ can‘t be set to the value of ‘NULL‘
MySQL运行SQL:[ERR] 1231 - Variable ‘time_zone‘ can‘t be set to the value of ‘NULL‘
2401 0
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate() or executeLargeUpdate().
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate() or executeLargeUpdate().
3444 0