简体   繁体   中英

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

<?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. I also tried changing mysqldump to mysqldump.exe but no success. Prior to this I was trying by not defining path of mysqldump in that case the process never used to end. 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.

Anyway, Long story short, here's what worked for me and you can get rid of the absolute path to mydsqldump command:

  1. Follow instruction on to add MySQL to Windows' Path. 上的说明将MySQL添加到Windows的路径中。
  2. Restart your WAMP. If you have a cmd window opened, close it too.
  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.
    If it returns a as below then congratulations, your path to MySQL works! ,那么恭喜您,您的MySQL路径正常!
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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