diff --git a/include/my_sys.h b/include/my_sys.h
index fe66aeef48c..4a035fde78a 100644
--- a/include/my_sys.h
+++ b/include/my_sys.h
@@ -766,8 +766,8 @@ extern size_t system_filename(char * to,const char *from);
 extern size_t unpack_filename(char * to,const char *from);
 extern char * intern_filename(char * to,const char *from);
 extern int pack_filename(char * to, const char *name, size_t max_length);
-extern char * my_path(char * to,const char *progname,
-                        const char *own_pathname_part);
+enum my_set_basedir_from { MY_SBF_ENV, MY_SBF_DEFAULT, MY_SBF_PROGNAME };
+extern enum my_set_basedir_from my_set_basedir(char *to, size_t tolen);
 extern char * my_load_path(char * to, const char *path,
                              const char *own_path_prefix);
 extern int wild_compare(const char *str,const char *wildstr,
diff --git a/mysys/mf_path.c b/mysys/mf_path.c
index 7da925d6f5f..ed1ea7e5287 100644
--- a/mysys/mf_path.c
+++ b/mysys/mf_path.c
@@ -14,107 +14,38 @@
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
 
 #include "mysys_priv.h"
-#include <m_string.h>
 
-static char *find_file_in_path(char *to,const char *name);
-
-       /* Finds where program can find it's files.
-          pre_pathname is found by first locking at progname (argv[0]).
-          if progname contains path the path is returned.
-          else if progname is found in path, return it
-          else if progname is given and POSIX environment variable "_" is set
-          then path is taken from "_".
-          If filename doesn't contain a path append MY_BASEDIR_VERSION or
-          MY_BASEDIR if defined, else append "/my/running".
-          own_path_name_part is concatinated to result.
-          my_path puts result in to and returns to */
-
-char * my_path(char * to, const char *progname,
-               const char *own_pathname_part)
-{
-  char *start, *end, *prog;
-  size_t to_length;
-  DBUG_ENTER("my_path");
-
-  start=to;                                    /* Return this */
-  if (progname && (dirname_part(to, progname, &to_length) ||
-                  find_file_in_path(to,progname) ||
-                  ((prog=getenv("_")) != 0 &&
-                    dirname_part(to, prog, &to_length))))
-  {
-    (void) intern_filename(to,to);
-    if (!test_if_hard_path(to))
-    {
-      if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
-       bchange((uchar*) to, 0, (uchar*) curr_dir, strlen(curr_dir), strlen(to)+1);
-    }
-  }
-  else
-  {
-    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
-       (end = getenv("MY_BASEDIR")) == 0)
-    {
-#ifdef DEFAULT_BASEDIR
-      end= (char*) DEFAULT_BASEDIR;
-#else
-      end= (char*) "/my/";
-#endif
-    }
-    (void) intern_filename(to,end);
-    to=strend(to);
-    if (to != start && to[-1] != FN_LIBCHAR)
-      *to++ = FN_LIBCHAR;
-    (void) strmov(to,own_pathname_part);
-  }
-  DBUG_PRINT("exit",("to: '%s'",start));
-  DBUG_RETURN(start);
-} /* my_path */
-
-
-       /* test if file without filename is found in path */
-       /* Returns to if found and to has dirpart if found, else NullS */
-
-#if defined(__WIN__)
-#define F_OK 0
-#define PATH_SEP ';'
-#define PROGRAM_EXTENSION ".exe"
-#else
-#define PATH_SEP ':'
-#endif
-
-static char *find_file_in_path(char *to, const char *name)
-{
-  char *path,*pos,dir[2];
-  const char *ext="";
-
-  if (!(path=getenv("PATH")))
-    return NullS;
-  dir[0]=FN_LIBCHAR; dir[1]=0;
-#ifdef PROGRAM_EXTENSION
-  if (!fn_ext(name)[0])
-    ext=PROGRAM_EXTENSION;
-#endif
-
-  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
-  {
-    if (path != pos)
-    {
-      strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
-      if (!access(to,F_OK))
-      {
-       to[(uint) (pos-path)+1]=0;      /* Return path only */
-       return to;
-      }
-    }
-  }
-#ifdef __WIN__
-  to[0]=FN_CURLIB;
-  strxmov(to+1,dir,name,ext,NullS);
-  if (!access(to,F_OK))                        /* Test in current dir */
-  {
-    to[2]=0;                           /* Leave ".\" */
-    return to;
-  }
-#endif
-  return NullS;                                /* File not found */
-}
+/** Guesses the basedir
+
+  1. if $MY_BASEDIR_VERSION environment variables is set - use it
+  2. otherise if $MY_BASEDIR environment variables is set - use it
+  3. otherwise get the absolute path of the running executable
+     and go one level up
+*/
+
+extern enum my_set_basedir_from my_set_basedir(char *to, size_t tolen)
+{
+  char *env;
+  char buf[FN_REFLEN];
+  size_t dirlen;
+  if ((env= getenv("MY_BASEDIR_VERSION")) || (env= getenv("MY_BASEDIR")))
+  {
+    strmake(to, env, tolen - 1);
+    return MY_SBF_ENV;
+  }
+  if (!test_if_hard_path(my_progname) &&
+      my_realpath(buf, my_progname, MYF(MY_WME)))
+    goto err;
+  if ((dirlen= dirname_length(buf)) > 3)
+    buf[dirlen - 1]= 0;
+  if (!(dirlen= dirname_length(buf)))
+    goto err;
+
+  buf[dirlen > 1 ? dirlen - 1 : 1]= 0;
+  strmake(to, buf, tolen - 1);
+  return MY_SBF_PROGNAME;
+
+err:
+  strmake(to, DEFAULT_MYSQL_HOME, tolen - 1);
+  return MY_SBF_DEFAULT;
+}
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 47c8b0b98af..65c549600cb 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -6299,7 +6299,7 @@ int mysqld_main(int argc, char **argv)
   if (Service.GetOS()) /* true NT family */
   {
     char file_path[FN_REFLEN];
-    my_path(file_path, argv[0], "");                 /* Find name in path */
+    my_set_basedir(file_path, sizeof(file_path));
     fn_format(file_path,argv[0],file_path,"",
              MY_REPLACE_DIR | MY_UNPACK_FILENAME | MY_RESOLVE_SYMLINKS);
 
@@ -8952,31 +8952,18 @@ static int mysql_init_variables(void)
   shared_memory_base_name= default_shared_memory_base_name;
 #endif
 
-#if defined(__WIN__)
-  /* Allow Win32 users to move MySQL anywhere */
-  {
-    char prg_dev[LIBLEN];
-    char executing_path_name[LIBLEN];
-    if (!test_if_hard_path(my_progname))
-    {
-      // we don't want to use GetModuleFileName inside of my_path since
-      // my_path is a generic path dereferencing function and here we care
-      // only about the executing binary.
-      GetModuleFileName(NULL, executing_path_name, sizeof(executing_path_name));
-      my_path(prg_dev, executing_path_name, NULL);
-    }
-    else
-      my_path(prg_dev, my_progname, "mysql/bin");
-    strcat(prg_dev,"/../");                    // Remove 'bin' to get base dir
-    cleanup_dirname(mysql_home,prg_dev);
-  }
-#else
-  const char *tmpenv;
-  if (!(tmpenv = getenv("MY_BASEDIR_VERSION")))
-    tmpenv = DEFAULT_MYSQL_HOME;
-  strmake_buf(mysql_home, tmpenv);
-  set_sys_var_value_origin(&mysql_home_ptr, sys_var::ENV);
-#endif
+  switch (my_set_basedir(mysql_home, sizeof(mysql_home)))
+  {
+  case MY_SBF_ENV:
+    set_sys_var_value_origin(&mysql_home_ptr, sys_var::ENV);
+    break;
+  case MY_SBF_PROGNAME:
+    set_sys_var_value_origin(&mysql_home_ptr, sys_var::AUTO);
+    break;
+  case MY_SBF_DEFAULT:
+    set_sys_var_value_origin(&mysql_home_ptr, sys_var::COMPILE_TIME);
+    break;
+  }
 
   if (wsrep_init_vars())
     return 1;
