mysqldump 通过 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11679275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysqldump via PHP
提问by Ross McLellan
I have a PHP script that gets passed the MySQL connection details of a remote server and I want it to execute a mysqldumpcommand. To do this I'm using the php exec()function:
我有一个 PHP 脚本,它传递了远程服务器的 MySQL 连接详细信息,我希望它执行mysqldump命令。为此,我使用了 phpexec()函数:
<?php
exec("/usr/bin/mysqldump -u mysql-user -h 123.145.167.189 -pmysql-pass database_name > /path-to-export/file.sql", $output);
?>
When the right login details are passed to it, it'll work absolutely fine.
However, I'm having trouble checking ifit executes as expected and if it doesn't finding out why not.
The $outputarray returns as empty, whereas if I run the command directly on the command line a message is printed out telling me the login failed. I want to capture such error messages and display them. Any ideas on how to do that?
当正确的登录详细信息传递给它时,它绝对可以正常工作。但是,我有麻烦的检查,如果它按预期执行,如果不找出为什么不。该$output数组返回为空,而如果我直接在命令行上运行该命令,则会打印出一条消息,告诉我登录失败。我想捕获此类错误消息并显示它们。关于如何做到这一点的任何想法?
回答by Fabio Mora
You should check the third parameter of execfunction: &$return_var.
您应该检查exec函数的第三个参数:&$return_var.
$return_var = NULL;
$output = NULL;
$command = "/usr/bin/mysqldump -u mysql-user -h 123.145.167.189 -pmysql-pass database_name > /path-to-export/file.sql";
exec($command, $output, $return_var);
By convention in Unix a process returns anything other than 0 when something goes wrong.
按照 Unix 的约定,当出现问题时,进程返回 0 以外的任何值。
And so you can:
所以你可以:
if($return_var) { /* there was an error code: $return_var, see the $output */ }
回答by Burak Tamtürk
Because this line redirect the stdout output > /path-to-export/file.sqltry this,
因为这一行重定向 stdout 输出> /path-to-export/file.sql试试这个,
<?php
exec("/usr/bin/mysqldump -u mysql-user -h 123.145.167.189 -pmysql-pass database_name", $output);
/* $output will have sql backup, then save file with these codes */
$h=fopen("/path-to-export/file.sql", "w+");
fputs($h, $output);
fclose($h);
?>
回答by JazZ
The solution I found is to run the command in a sub-shell and then output the stderrto stdout(2>&1). This way, the $outputvariable is populated with the error message (if any).
我找到的解决方案是在子 shell 中运行命令,然后输出stderr到stdout( 2>&1)。这样,该$output变量将填充错误消息(如果有)。
i.e. :
IE :
exec("(mysqldump -uroot -p123456 my_database table_name > /path/to/dump.sql) 2>&1", $output, $exit_status);
var_dump($exit_status); // (int) The exit status of the command (0 for success, > 0 for errors)
echo "<br />";
var_dump($output); // (array) If exit status != 0 this will handle the error message.
Results :
结果 :
int(6)
array(1) { [0]=> string(46) "mysqldump: Couldn't find table: "table_name"" }
整数(6)
array(1) { [0]=> string(46) "mysqldump: 找不到表:"table_name"" }
Hope it helps !
希望能帮助到你 !
回答by Jon T
I was looking for the exact same solution, and I remembered I'd already solved this a couple of years ago, but forgotten about it.
我一直在寻找完全相同的解决方案,我记得几年前我已经解决了这个问题,但忘记了。
As this page is high in Google for the question, here's how I did it:
由于此页面在 Google 中的问题排名很高,因此我是这样做的:
<?php
define("BACKUP_PATH", "/full/path/to/backup/folder/with/trailing/slash/");
$server_name = "your.server.here";
$username = "your_username";
$password = "your_password";
$database_name = "your_database_name";
$date_string = date("Ymd");
$cmd = "mysqldump --hex-blob --routines --skip-lock-tables --log-error=mysqldump_error.log -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
$arr_out = array();
unset($return);
exec($cmd, $arr_out, $return);
if($return !== 0) {
echo "mysqldump for {$server_name} : {$database_name} failed with a return code of {$return}\n\n";
echo "Error message was:\n";
$file = escapeshellarg("mysqldump_error.log");
$message = `tail -n 1 $file`;
echo "- $message\n\n";
}
?>
It's the --log-error=[/path/to/error/log/file]part of mysqldump that I always forget about!
这是我总是忘记的 mysqldump的--log-error=[/path/to/error/log/file]部分!
回答by dmnc
As exec()is fetching just stdoutwhich is redirected to the file, we have partial or missing result in the file and we don't know why. We have to get message from stderrand exec()can't do that. There are several solutions, all has been already found so this is just a summary.
正如exec()只获取stdout重定向到文件的内容一样,我们在文件中有部分或丢失的结果,我们不知道为什么。我们必须从那里获取消息stderr,exec()但不能这样做。有几种解决方案,都已经找到了,所以这只是一个总结。
- Solution from Jon: log errors from
mysqldumpand handle them separately (can't apply for every command). - Redirect outputs to separate files, i.e.
mysqldump ... 2> error.log 1> dump.sqland read the error log separately as in previous solution. - Solution from JazZ: write the dump as a subshell and redirect stderr of the subshell to stdout which can php
exec()put in the$outputvariable. - Solution from Pascal: better be using
proc_open()instead ofexec()because we can getstdoutandstderrseparately (directly from pipes).
- Jon 的解决方案:记录错误
mysqldump并单独处理它们(不能适用于每个命令)。 - 将输出重定向到单独的文件,即
mysqldump ... 2> error.log 1> dump.sql和以前的解决方案一样单独读取错误日志。 - JazZ 的解决方案:将转储作为子shell 编写,并将子shell 的stderr 重定向到stdout,php 可以
exec()将其放入$output变量中。 - Pascal 的解决方案:最好使用
proc_open()而不是exec()因为我们可以单独获取stdout和stderr(直接从管道中获取)。
回答by Avinash Raut
write below code to get the database export in .sql file.
编写以下代码以获取 .sql 文件中的数据库导出。
<?php exec('mysqldump --user=name_user --password=password_enter --host=localhost database_name > filenameofsql.sql'); ?>

