简体   繁体   English

php exec() - mysqldump创建一个空文件

[英]php exec() - mysqldump creates an empty file

I want to create a backup from a database, but I get only a blank file. 我想从数据库创建备份,但我只得到一个空白文件。

include('config.php');

$command = "mysqldump --opt -h ".$_host." -u ".$_user." -p ".$_pass." ".$_db." > test.sql";
exec($command);

echo "<br />".$command;

test.sql is created where the .php file is located. 在.php文件所在的位置创建test.sql。

Edit: 编辑:

Note! 注意! I'm using XAMPP WINDOWS ! 我正在使用XAMPP WINDOWS!

Solution: 解:

Because I'm using a Windows Web Server (XAMPP), I needed to specify the path: 因为我使用的是Windows Web服务器(XAMPP),所以我需要指定路径:

$command = 'd:\xampp\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql';
  1. I removed the space between the -p and the pass. 我删除了-p和pass之间的空格。 It looks like: -pMYPASSWORD 它看起来像: -pMYPASSWORD
  2. Replaced " with ' 替换为" with '

I think if you are using a Linux based web server, you don't have to specify the path for mysqldump. 我想如果您使用的是基于Linux的Web服务器,则不必指定mysqldump的路径。

Cheers! 干杯! :-) :-)

These are the parameters 这些是参数

-uROOT -pPASSWORD --databases DB --result-file=FILE.SQL -uROOT -pPASSWORD --databases DB --result-file = FILE.SQL

Try this one: 试试这个:

$command = 'd:\xampp\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql 2>&1';

-its about permission issue. - 关于许可问题。

You should remove the space between the -p and the password. 您应该删除-p和密码之间的空格。

--opt isn't needed with recent versions of mysql. 最新版本的mysql不需要--opt

Other than that, your syntax is correct so if it doesn't work with the -p fix, you should check the parameters and the produced command. 除此之外,您的语法是正确的,因此如果它不能与-p fix一起使用,您应该检查参数和生成的命令。

If you look at the manual for exec the output goes to an array that is the second argument. 如果查看exec的手册,输出将转到第二个参数的数组。 That may be the source of the problem. 这可能是问题的根源。

Take the variables out of the quotes and remove --opt. 从引号中取出变量并删除--opt。

Also make sure that you are having unique file names 还要确保您拥有唯一的文件名

$backupfile = $dbname . date("YmdHis") . '.sql';

$command = "D:\\xampp\\mysql\\bin\\mysqldump -u $_user -p$_pass $_db > $backupfile";

system($command);

For those on Macs 适用于Mac上的用户

I was battling this issue the entire evening today. 我今天整个晚上都在和这个问题作斗争。 Silly mistake: one needs to chmod the directory that you are dumping the file to. 愚蠢的错误:需要chmod你要转储文件的目录。

I ran this which fixed the issue: 我跑了这个修复了这个问题:

chmod -R 777 your_dump_directory/

Try this: 试试这个:

$DBUSER="user";
$DBPASSWD="password";
$DATABASE="DBname";

$filename = "backup-" . date("d-m-Y") . ".sql";

$command = '"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" '.$DATABASE ." -u".$DBUSER ." -p".$DBPASSWD." > your_web_site/$filename";
passthru($command);
  • Change the route to the direction of the mysqldump.exe application of your computer. 将路由更改为计算机的mysqldump.exe应用程序的方向。
  • Respect the " " inside the command. 尊重命令中的“”。

Then force the download of the file: 然后强制下载文件:

if (file_exists("your_web_site/".$filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile("your_web_site/".$filename);
exit;
}

Edit: You have to give permissions to the folder where you keep copies. 编辑:您必须授予保留副本的文件夹的权限。

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

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