问题
I'm playing around with a backup script for mysql. A variation of this used to work, but I haven't looked at it since php4. It's returning an empty file. The weird thing is that if I go to the command line and use the EXACT same command, I get the file I'm expecting.
I've poked around the internet and can't find anything... thoughts?
Bad code?
$db_host='localhost';
$db_user='root';
$db_pass='root';
$db_name='gakkou';
$dir='backups';
$file_list=scandir($dir);
if(count($file_list)>10) unlink($dir.'/'.$file_list[2]); //delete old file
$prefix=date("YmdHi").'_';
$command='mysqldump -u'.$db_user.' --password="'.$db_pass.'" --databases '.$db_name.' | gzip > '.$dir.'/'.$prefix.'_backup.sql.gzip';
exec($command,$output,$return_val);
This works perfectly: mysqldump -uroot -proot -hlocalhost gakkou > /webdocs/gakkou/backups/mysql_backup.sql
and it is exactly the same as the command the php file executes (except for the file name).
EDIT: updated with working code for anyone interested. This turned out to be two separate issues. Using MAMP, I needed to specify the path /Applications/MAMP/Library/bin/mysqldump
. Then on the production server the crazy password gummed up the works.
回答1:
OK. FINALLY got it figured out. My super stupid password was messing with mysqldump because it had an "&" in it. Didn't have to change the password. I just enclosed the password in parentheses: --password="'.$db_pass.'"
Now it works as expected. So many hours wasted... @MarcB, thank you so much for your help. Didn't know how to return the errors and that was definitely the biggest roadblock.
来源:https://stackoverflow.com/questions/22123576/mysqldump-empty-file-using-php-but-not-command-line