从PHP脚本运行mysqldump无效,但可在SSH上运行 [英] Running mysqldump from a PHP script doesn't work but works on SSH
问题描述
我正在尝试使用以下简单行通过PHP脚本备份数据库:
I'm trying to back a database via a PHP script using this one simple line:
passthru("/usr/bin/mysqldump --opt --host=localhost --user=\"myuser\" --password=\"mypass\" db_name > /var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql");
我没有收到任何错误或警告.但是,如果我复制该字符串并通过SSH在服务器上完全执行该字符串,则效果很好.
I get no errors or warnings or anything. but if I copy that string and execute it exactly as is on the server via SSH it works perfectly.
/usr/bin/mysqldump --opt --host=localhost --user="myuser" --password="mypass" db_name > /var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql
我尝试使用exec()和system()代替passthru(),但结果相同.这真的很奇怪,我无法弄清楚可能是什么问题.有什么想法吗?
I tried using exec() and system() instead of passthru() but same results. This is really strange and I can't focure out what could be the problem. Any ideas?
推荐答案
有根据的猜测
如果我可以进行有根据的猜测,那是因为运行php-script的用户(即运行httpd的用户)没有写/var/www/vhosts/mydomain.com/httpdocs/tools/
的权限.
尽管您用来执行命令的用户已经拥有.
Though the user you are using to execute the command has.
要查看是否有任何打印到STDERR
的与问题相关的内容,请使用下面的代码段!
To see if there is anything printed to STDERR
that is relevant to the problem, use the below snippet!
$tubes = array(
0 => array("pipe", "r"),
// print contents on STDOUT to file
1 => array("file", "/var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql", "w"),
2 => array("pipe", "w")
);
$p_handle = proc_open (
"/usr/bin/mysqldump --opt --host=localhost --user=\"myuser\" --password=\"mypass\" db_name",
$tubes, $pipes
);
if (is_resource ($p_handle)) {
fclose ($pipes[0]);
$stderr_data = stream_get_contents ($pipes[2]); fclose($pipes[2]);
$proc_ret = proc_close ($p_handle);
echo "--------- STDERR:\n$stderr_data\n";
echo "------------ RET: $proc_ret\n";
} else {
die ("Unable to execute external resource, aborting!");
}
检查日志文件!
您是否已检查与httpd
关联的error_log
?
Check the log files!
Have you checked the error_log
associated with your httpd
?
这篇关于从PHP脚本运行mysqldump无效,但可在SSH上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!