Bug #68338 RFE: make tmpdir a build-time configurable option
Submitted: 11 Feb 2013 14:06 Modified: 14 Jan 2014 16:54
Reporter: Honza Horak (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S4 (Feature request)
Version:5.5.29 OS:Linux
Assigned to: Tor Didriksen CPU Architecture:Any

[11 Feb 2013 14:06] Honza Horak
Description:
Background: Some distributions use tmpfs for mounting /tmp by default, which has some advantages, but brings also new issues. Fedora started using tmpfs on /tmp in version 18 for example. If not configured otherwise in my.cnf, MySQL uses system's constant P_tmpdir expanded to /tmp on Linux. This can introduce some problems with limited space in /tmp and also some data loss in case of replication slave [1].

In case distributions would like to use /var/tmp, which should be better for MySQL purposes, then we have to patch the source or change tmpdir option in my.cnf, which is however not updated in case it has already existed.

Thus, it would be useful to be able to specify default tmpdir path using a configure option, while using P_tmpdir in case it is not defined explicitly. I've created such patch, so feel free to comment or apply it.

[1] http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_tmpdir

How to repeat:
Compile MySQL -- /tmp is used every-time.

Suggested fix:
diff -up mysql-5.5.29/CMakeLists.txt.tmpdir mysql-5.5.29/CMakeLists.txt
--- mysql-5.5.29/CMakeLists.txt.tmpdir	2012-12-10 07:16:24.000000000 +0100
+++ mysql-5.5.29/CMakeLists.txt	2013-02-07 14:16:49.891906389 +0100
@@ -252,7 +252,9 @@ SET(PLUGINDIR "${DEFAULT_MYSQL_HOME}/${I
 IF(SYSCONFDIR)
   SET(DEFAULT_SYSCONFDIR "${SYSCONFDIR}")
 ENDIF()
-
+IF(TMPDIR)
+  SET(DEFAULT_TMPDIR "${TMPDIR}")
+ENDIF()
 
 # Run platform tests
 INCLUDE(configure.cmake)
diff -up mysql-5.5.29/config.h.cmake.tmpdir mysql-5.5.29/config.h.cmake
--- mysql-5.5.29/config.h.cmake.tmpdir	2012-12-10 07:16:24.000000000 +0100
+++ mysql-5.5.29/config.h.cmake	2013-02-07 14:16:49.891906389 +0100
@@ -600,6 +600,7 @@
 #cmakedefine DEFAULT_CHARSET_HOME "@DEFAULT_CHARSET_HOME@"
 #cmakedefine PLUGINDIR "@PLUGINDIR@"
 #cmakedefine DEFAULT_SYSCONFDIR "@DEFAULT_SYSCONFDIR@"
+#cmakedefine DEFAULT_TMPDIR "@DEFAULT_TMPDIR@"
 
 #cmakedefine SO_EXT "@CMAKE_SHARED_MODULE_SUFFIX@"
 
diff -up mysql-5.5.29/include/my_global.h.tmpdir mysql-5.5.29/include/my_global.h
--- mysql-5.5.29/include/my_global.h.tmpdir	2013-02-11 12:30:09.214779973 +0100
+++ mysql-5.5.29/include/my_global.h	2013-02-11 12:30:45.265777508 +0100
@@ -1501,4 +1501,11 @@ static inline double rint(double x)
 
 #endif /* EMBEDDED_LIBRARY */
 
+/*
+  Define default tmpdir if not already set.
+*/
+#if !defined(DEFAULT_TMPDIR)
+#define DEFAULT_TMPDIR P_tmpdir
+#endif
+
 #endif /* my_global_h */
diff -up mysql-5.5.29/libmysqld/lib_sql.cc.tmpdir mysql-5.5.29/libmysqld/lib_sql.cc
--- mysql-5.5.29/libmysqld/lib_sql.cc.tmpdir	2012-12-10 07:16:24.000000000 +0100
+++ mysql-5.5.29/libmysqld/lib_sql.cc	2013-02-11 12:23:54.369805605 +0100
@@ -558,7 +558,7 @@ int init_embedded_server(int argc, char
     opt_mysql_tmpdir=getenv("TMP");
 #endif
   if (!opt_mysql_tmpdir || !opt_mysql_tmpdir[0])
-    opt_mysql_tmpdir=(char*) P_tmpdir;		/* purecov: inspected */
+    opt_mysql_tmpdir=(char*) DEFAULT_TMPDIR;		/* purecov: inspected */
 
   init_ssl();
   umask(((~my_umask) & 0666));
diff -up mysql-5.5.29/mysys/mf_tempdir.c.tmpdir mysql-5.5.29/mysys/mf_tempdir.c
--- mysql-5.5.29/mysys/mf_tempdir.c.tmpdir	2012-12-10 07:16:24.000000000 +0100
+++ mysql-5.5.29/mysys/mf_tempdir.c	2013-02-07 14:16:49.892906389 +0100
@@ -43,7 +43,7 @@ my_bool init_tmpdir(MY_TMPDIR *tmpdir, c
       pathlist=getenv("TMP");
 #endif
     if (!pathlist || !pathlist[0])
-      pathlist=(char*) P_tmpdir;
+      pathlist=(char*) DEFAULT_TMPDIR;
   }
   do
   {
diff -up mysql-5.5.29/mysys/mf_tempfile.c.tmpdir mysql-5.5.29/mysys/mf_tempfile.c
--- mysql-5.5.29/mysys/mf_tempfile.c.tmpdir	2012-12-10 07:16:24.000000000 +0100
+++ mysql-5.5.29/mysys/mf_tempfile.c	2013-02-07 14:16:49.893906389 +0100
@@ -111,7 +111,7 @@ File create_temp_file(char *to, const ch
 				    sizeof(prefix_buff)-7),"XXXXXX") -
 		     prefix_buff);
     if (!dir && ! (dir =getenv("TMPDIR")))
-      dir=P_tmpdir;
+      dir=DEFAULT_TMPDIR;
     if (strlen(dir)+ pfx_len > FN_REFLEN-2)
     {
       errno=my_errno= ENAMETOOLONG;
diff -up mysql-5.5.29/mysys/mysys_priv.h.tmpdir mysql-5.5.29/mysys/mysys_priv.h
[12 Feb 2013 11:44] MySQL Verification Team
Thank you for a feature request and contribution.
Another related feature is http://bugs.mysql.com/bug.php?id=53119
[14 Jan 2014 16:54] Paul DuBois
Noted in 5.5.36, 5.6.16, 5.7.4 changelogs.

CMake now supports a -DTMPDIR=dir_name option to specify the default
tmpdir value. If unspecified, the value defaults to P_tmpdir in
<stdio.h>.
[15 Jan 2014 16:27] Paul DuBois
Thanks to Honza Horak for the patch.
[2 Feb 2014 17:44] Laurynas Biveinis
5.5$ bzr log -r 4563
------------------------------------------------------------
revno: 4563
committer: Tor Didriksen <tor.didriksen@oracle.com>
branch nick: 5.5
timestamp: Wed 2013-12-18 11:05:18 +0100
message:
  Bug#16316074 RFE: MAKE TMPDIR A BUILD-TIME CONFIGURABLE OPTION
  Bug#68338    RFE: make tmpdir a build-time configurable option
  
  Background: Some distributions use tmpfs for mounting /tmp by
  default, which has some advantages, but brings also new
  issues. Fedora started using tmpfs on /tmp in version 18 for
  example. If not configured otherwise in my.cnf, MySQL uses
  system's constant P_tmpdir expanded to /tmp on Linux. This can
  introduce some problems with limited space in /tmp and also some
  data loss in case of replication slave [1].
  
  In case distributions would like to use /var/tmp, which should be
  better for MySQL purposes, then we have to patch the source or
  change tmpdir option in my.cnf, which is however not updated in
  case it has already existed.
  
  Thus, it would be useful to be able to specify default tmpdir
  path using a configure option, while using P_tmpdir in case it is
  not defined explicitly.
  
  Based on a contribution from Honza Horak
[27 Mar 2014 13:49] Laurynas Biveinis
5.5$ bzr log -r 4583 -n0
------------------------------------------------------------
revno: 4583
committer: Tor Didriksen <tor.didriksen@oracle.com>
branch nick: 5.5-review
timestamp: Thu 2014-01-16 15:43:29 +0100
message:
  Bug#16316074 RFE: MAKE TMPDIR A BUILD-TIME CONFIGURABLE OPTION
  Bug#68338    RFE: make tmpdir a build-time configurable option
  
  Post-push fix: 'cmake -LH | grep TMP' showed TMPDIR as a BOOL option,
  which was a bit confusing: show it as a PATH instead.
[27 Mar 2014 13:50] Laurynas Biveinis
5.5$ bzr log -r 4584 -n0
------------------------------------------------------------
revno: 4584
committer: Tor Didriksen <tor.didriksen@oracle.com>
branch nick: 5.5-review
timestamp: Thu 2014-01-16 18:01:06 +0100
message:
  Bug#16316074 RFE: MAKE TMPDIR A BUILD-TIME CONFIGURABLE OPTION
  Bug#68338    RFE: make tmpdir a build-time configurable option
  
  Post-push fix: windows needs DEFAULT_TMPDIR as well.