简体   繁体   English

PHP中的mysqldump使用exec返回空文件

[英]mysqldump in php using exec returns empty file

I have gone over several posts where people have managed to have their issues resolved but unfortunately no success for me. 我浏览了几篇文章,人们设法解决了他们的问题,但不幸的是,这对我没有成功。 I am using wamp on windows and my php file looks as follows 我在Windows上使用Wamp,我的php文件如下所示

<?php 
  include_once("config.php");
  $_user = "username";
  $_pass = "password";
  $_db = "dbname";
  $command = 'C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql';
  exec($command);
  echo "<br />".$command;
?>

A file name test.sql is created but it is of 0 bytes. 创建了文件名test.sql,但它的大小为0个字节。 I also tried changing mysqldump to mysqldump.exe but no success. 我也尝试将mysqldump更改为mysqldump.exe,但没有成功。 Prior to this I was trying by not defining path of mysqldump in that case the process never used to end. 在此之前,我试图通过不定义mysqldump的路径来进行尝试,在这种情况下,该进程永远不会结束。 Now the process ends immediately but the file is empty. 现在,该过程立即结束,但是文件为空。

I've had the same problem too and was pulling my hair because none of the solutions I found worked for me. 我也遇到了同样的问题,并且因为无法找到对我有用的解决方案而束手无策。

Finally I tried running the command from cmd (windows' command prompt) and found out that Windows couldn't find the path to that MySQL command. 最后,我尝试从cmd(Windows的命令提示符)运行命令,发现Windows找不到该MySQL命令的路径。

Anyway, Long story short, here's what worked for me and you can get rid of the absolute path to mydsqldump command: 总之,长话短说,这是对我有用的方法,您可以摆脱mydsqldump命令的绝对路径:

  1. Follow instruction on this page to add MySQL to Windows' Path. 按照本页上的说明将MySQL添加到Windows的路径中。
  2. Restart your WAMP. 重新启动您的WAMP。 If you have a cmd window opened, close it too. 如果您打开了cmd窗口,请也将其关闭。
  3. ( optional if you want to test the newly inserted path ) Open a cmd window by searching for "cmd" in windows search then enter "mysqldump" without the quotes. 如果要测试新插入的路径,则是可选的 )通过在Windows搜索中搜索“ cmd”来打开cmd窗口,然后输入不带引号的“ mysqldump”。
    If it returns a usage instruction as below then congratulations, your path to MySQL works! 如果它返回如下用法说明 ,那么恭喜您,您的MySQL路径正常!
Usage: mysqldump [OPTIONS] database [tables] 用法:mysqldump [OPTIONS]数据库[表]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] 或mysqldump [OPTIONS]-数据库[OPTIONS] DB1 [DB2 DB3 ...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS] 或mysqldump [OPTIONS]-所有数据库[OPTIONS]
For more options, use mysqldump --help 有关更多选项,请使用mysqldump --help
  1. In your code, remove the absolute path to your (C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin) mysqldump command, leaving only mysqldump without any path. 在代码中,删除您的(C:\\ wamp \\ bin \\ mysql \\ mysql5.6.17 \\ bin)mysqldump命令的绝对路径,仅保留mysqldump而没有任何路径。 Below is what works for me: 以下是对我有效的方法:

     $command = 'mysqldump --opt --host=YOUR_HOST --password="YOUR_PASSWORD" --user=YOUR_DB_USER --databases YOUR_DB_NAME > PATH_TO_FILE/FILE_NAME.sql'; 
  2. Replace capitalized part in the code above with your own information 用您自己的信息替换上面代码中的大写部分

  3. Run your code again. 再次运行您的代码。

    Hope this helps more or less. 希望这或多或少有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM