diff --git a/client/mysql_upgrade.c b/client/mysql_upgrade.c
index df70ed2c14857a9ce8885d71a9f2f0c667a6914c..d67933fa240b337f2acc5804cb0479d1f177c780 100644
--- a/client/mysql_upgrade.c
+++ b/client/mysql_upgrade.c
@@ -17,14 +17,25 @@
 #include <sslopt-vars.h>
 #include "../scripts/mysql_fix_privilege_tables_sql.c"
 
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
+#ifndef WEXITSTATUS
+# ifdef __WIN__
+#  define WEXITSTATUS(stat_val) (stat_val)
+# else
+#  define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
+# endif
+#endif
+
 static char mysql_path[FN_REFLEN];
 static char mysqlcheck_path[FN_REFLEN];
-static char defaults_file_option[32+FN_REFLEN];
 
 static my_bool opt_force, opt_verbose;
 static char *opt_user= (char*)"root";
 
-static DYNAMIC_STRING ds_options;
+static DYNAMIC_STRING ds_args;
 
 static char *opt_password= 0;
 static my_bool tty_password= 0;
@@ -35,8 +46,6 @@ static char *default_dbug_option= (char*) "d:t:O,/tmp/mysql_upgrade.trace";
 
 static char **defaults_argv;
 
-static File defaults_fd= -1;
-
 static my_bool not_used; /* Can't use GET_BOOL without a value pointer */
 
 #include <help_start.h>
@@ -105,14 +114,10 @@ static struct my_option my_long_options[]=
 
 static void free_used_memory(void)
 {
-  /* Close the defaults file */
-  if (defaults_fd != -1)
-    my_close(defaults_fd, MYF(0));
-
   /* Free memory allocated by 'load_defaults' */
   free_defaults(defaults_argv);
 
-  dynstr_free(&ds_options);
+  dynstr_free(&ds_args);
 }
 
 
@@ -159,30 +164,30 @@ static void verbose(const char *fmt, ...)
 
 /*
   Add one option - passed to mysql_upgrade on command line
-  or by defaults file(my.cnf) - to a dynamic string that later
-  can be written to a temporary file. In this way we pass the
-  same arguments on to mysql and mysql_check
+  or by defaults file(my.cnf) - to a dynamic string, in
+  this way we pass the same arguments on to mysql and mysql_check
 */
 
 static void add_one_option(DYNAMIC_STRING* ds,
                            const struct my_option *opt,
                            const char* argument)
-{
-  dynstr_append(ds, opt->name);
 
+{
+  const char* eq= NullS;
+  const char* arg= NullS;
   if (opt->arg_type != NO_ARG)
   {
-    dynstr_append(ds, "=");
+    eq= "=";
     switch (opt->var_type & GET_TYPE_MASK) {
     case GET_STR:
-    case GET_STR_ALLOC:
-      dynstr_append(ds, argument);
+      arg= argument;
       break;
     default:
       die("internal error at %s: %d",__FILE__, __LINE__);
     }
   }
-  dynstr_append(ds, "\n");
+  dynstr_append_os_quoted(ds, "--", opt->name, eq, arg, NullS);
+  dynstr_append(&ds_args, " ");
 }
 
 
@@ -211,8 +216,8 @@ get_one_option(int optid, const struct my_option *opt,
     add_option= FALSE;
     if (argument)
     {
-      /* Add password to ds_options before overwriting the arg with x's */
-      add_one_option(&ds_options, opt, argument);
+      /* Add password to ds_args before overwriting the arg with x's */
+      add_one_option(&ds_args, opt, argument);
       while (*argument)
         *argument++= 'x';                       /* Destroy argument */
       tty_password= 0;
@@ -232,51 +237,14 @@ get_one_option(int optid, const struct my_option *opt,
     /*
       This is an option that is accpted by mysql_upgrade just so
       it can be passed on to "mysql" and "mysqlcheck"
-      Save it in the ds_options string
+      Save it in the ds_args string
     */
-    add_one_option(&ds_options, opt, argument);
+    add_one_option(&ds_args, opt, argument);
   }
   return 0;
 }
 
 
-/*
-  Write the options that should be passed on to
-  mysql and mysqlcheck to a temporary file
-*/
-
-static void create_defaults_file(void)
-{
-  static char defaults_file_path[FN_REFLEN];
-  DBUG_ENTER("create_defaults_file");
-
-  if ((defaults_fd= create_temp_file(defaults_file_path, NULL,
-                            "cnf", O_CREAT | O_SHARE,
-                            MYF(MY_WME))) < 0)
-    die("Failed to create temporary file for defaults");
-
-  DBUG_PRINT("info", ("Writing options: %s", ds_options.str));
-  if (my_write(defaults_fd, ds_options.str, ds_options.length,
-               MYF(MY_FNABP | MY_WME)))
-    die("Failed to write to '%s'", defaults_file_path);
-
-  /*
-     Dont close the temporary file yet, it will be used
-     by mysql and mysqlcheck
-  */
-
-  /*
-    Create the option that should be added to
-    tools in order to use this file
-  */
-  my_snprintf(defaults_file_option, sizeof(defaults_file_option),
-              "--defaults-file=%s", defaults_file_path);
-  DBUG_PRINT("info", ("defaults_file_option: %s", defaults_file_option));
-
-  DBUG_VOID_RETURN;
-}
-
-
 static int run_command(char* cmd,
                        DYNAMIC_STRING *ds_res)
 {
@@ -466,7 +434,7 @@ static int run_query(const char *query, DYNAMIC_STRING *ds_res,
   DBUG_ENTER("run_query");
   DBUG_PRINT("enter", ("query: %s", query));
   if ((fd= create_temp_file(query_file_path, NULL,
-                            "sql", O_CREAT | O_SHARE,
+                            "sql", O_CREAT | O_SHARE | O_RDWR,
                             MYF(MY_WME))) < 0)
     die("Failed to create temporary file for defaults");
 
@@ -476,7 +444,7 @@ static int run_query(const char *query, DYNAMIC_STRING *ds_res,
 
   ret= run_tool(mysql_path,
                 ds_res,
-                defaults_file_option,
+                ds_args.str, // MASV... quoted?
                 "--database=mysql",
                 "--batch", /* Turns off pager etc. */
                 force ? "--force": "--skip-force",
@@ -631,7 +599,7 @@ static int run_mysqlcheck_upgrade(void)
   verbose("Running 'mysqlcheck'...");
   return run_tool(mysqlcheck_path,
                   NULL, /* Send output from mysqlcheck directly to screen */
-                  defaults_file_option,
+                  ds_args.str,
                   "--check-upgrade",
                   "--all-databases",
                   "--auto-repair",
@@ -747,7 +715,7 @@ int main(int argc, char **argv)
   setscreenmode(SCR_AUTOCLOSE_ON_EXIT);
 #endif
 
-  if (init_dynamic_string(&ds_options, "[client]\n", 512, 256))
+  if (init_dynamic_string(&ds_args, "", 512, 256))
     die("Out of memory");
 
   load_defaults("my", load_default_groups, &argc, &argv);
@@ -760,14 +728,12 @@ int main(int argc, char **argv)
   {
     opt_password= get_tty_password(NullS);
     /* add password to defaults file */
-    dynstr_append(&ds_options, "password=");
-    dynstr_append(&ds_options, opt_password);
-    dynstr_append(&ds_options, "\n");
+    dynstr_append_os_quoted(&ds_args, "--password=", opt_password, NullS);
+    dynstr_append(&ds_args, " ");
   }
   /* add user to defaults file */
-  dynstr_append(&ds_options, "user=");
-  dynstr_append(&ds_options, opt_user);
-  dynstr_append(&ds_options, "\n");
+  dynstr_append_os_quoted(&ds_args, "--user=", opt_user, NullS);
+  dynstr_append(&ds_args, " ");
 
   /* Find mysql */
   find_tool(mysql_path, IF_WIN("mysql.exe", "mysql"));
@@ -775,13 +741,6 @@ int main(int argc, char **argv)
   /* Find mysqlcheck */
   find_tool(mysqlcheck_path, IF_WIN("mysqlcheck.exe", "mysqlcheck"));
 
-  /*
-     Create the defaults file(a small my.cnf) which will pass
-     all arguments accepted by mysql_upgrade on to mysqlcheck
-     and mysql command line client
-   */
-  create_defaults_file();
-
   /*
     Read the mysql_upgrade_info file to check if mysql_upgrade
     already has been run for this installation of MySQL
diff --git a/mysql-test/r/mysql_upgrade.result b/mysql-test/r/mysql_upgrade.result
index ce4706c6d92261b923662cc8a9044c175c7c4f5b..f8b55a7ce49a040ead45e0cfdc852c3c45b4b27d 100644
--- a/mysql-test/r/mysql_upgrade.result
+++ b/mysql-test/r/mysql_upgrade.result
@@ -58,5 +58,5 @@ mysql.time_zone_transition_type                    OK
 mysql.user                                         OK
 DROP USER mysqltest1@'%';
 Run mysql_upgrade with a non existing server socket
-mysqlcheck: Got error: 2002: Can't connect to local MySQL server through socket 'var/tmp/no_sock_here' (2) when trying to connect
+mysqlcheck: Got error: 2005: Unknown MySQL server host 'not_existing_host' (1) when trying to connect
 FATAL ERROR: Upgrade failed
diff --git a/mysql-test/t/mysql_upgrade.test b/mysql-test/t/mysql_upgrade.test
index 973e91579875b74287be558da568f43d9b7be892..7ac7aecb838a4d5de585322908432594fd694c7c 100644
--- a/mysql-test/t/mysql_upgrade.test
+++ b/mysql-test/t/mysql_upgrade.test
@@ -58,4 +58,4 @@ DROP USER mysqltest1@'%';
 --replace_result $MYSQLTEST_VARDIR var
 --replace_regex /.*mysqlcheck.*: Got/mysqlcheck: Got/
 --error 1
---exec $MYSQL_UPGRADE --skip-verbose --force --socket=$MYSQLTEST_VARDIR/tmp/no_sock_here 2>&1
+--exec $MYSQL_UPGRADE --skip-verbose --force --host=not_existing_host 2>&1
diff --git a/scripts/comp_sql.c b/scripts/comp_sql.c
index 328dab0cf139c74e6a504ad7341f58742617cdcc..53359d384c15513eb8602d49056e4045b9f139e7 100644
--- a/scripts/comp_sql.c
+++ b/scripts/comp_sql.c
@@ -106,6 +106,15 @@ int main(int argc, char *argv[])
         curr++;
       }
     }
+    if (*(curr-1) != '\n')
+    {
+      /*
+        Some compilers have a max string length,
+        insert a newline at every 512th char in long
+        strings
+      */
+      fprintf(out, "\"\n\"");
+    }
   }
 
   fprintf(out, "\\\n\"};\n");