mysqldump mac / linux的常用安装位置

时间:2013-12-19 01:10:06

标签: php mysqldump

我想知道mysqldump的所有常见位置。我提出的清单如下:

'/usr/bin/mysqldump', //Linux
'/usr/local/mysql/bin/mysqldump', //Mac OS X
'/usr/local/bin/mysqldump', //Linux
'/usr/mysql/bin/mysqldump'; //Linux

通常mysqldump不在路径中,所以我试图查看所有位置。(我是从php脚本运行的)

有没有我遗失的东西?

1 个答案:

答案 0 :(得分:6)

除了你在问题中给出的路径之外,我找不到任何其他路径。但是,我想到的一件事是mysqldump在大多数情况下应该与mysql二进制文件位于同一目录中。现在,在大多数情况下,mysql命令也将在路径中。

因此,您可以将两个逻辑组合起来,以获得mysqldump二进制文件的位置,在大多数情况下,如下所示:

function detect_mysqldump_location() {

  // 1st: use mysqldump location from `which` command.
  $mysqldump = `which mysqldump`;
  if (is_executable($mysqldump)) return $mysqldump;

  // 2nd: try to detect the path using `which` for `mysql` command.
  $mysqldump = dirname(`which mysql`) . "/mysqldump";
  if (is_executable($mysqldump)) return $mysqldump;

  // 3rd: detect the path from the available paths.
  // you can add additional paths you come across, in future, here.
  $available = array(
    '/usr/bin/mysqldump', // Linux
    '/usr/local/mysql/bin/mysqldump', //Mac OS X
    '/usr/local/bin/mysqldump', //Linux
    '/usr/mysql/bin/mysqldump' //Linux
   );
  foreach($available as $apath) {
    if (is_executable($apath)) return $apath;
  }

  // 4th: auto detection has failed!
  // lets, throw an exception, and ask the user to provide the path instead, manually.
  $message  = "Path to \"mysqldump\" binary could not be detected!\n"
  $message .= "Please, specify it inside the configuration file provided!"
  throw new RuntimeException($message);
}

现在,您可以将上述功能用于您的目的。并且,如果上述函数抛出错误,则为用户提供手动提供mysqldump二进制的显式路径的方法。应该适用于您的用例:)