PHP exec() mysqldump备份数据库 备份文件为空的问题

原文链接 http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/


               注意原文作者列出的正确案例

mysqldump – with exec() function from php outputs empty file

This error occurs on any operating system (windows, linux). The problem is that instead of getting a sql file with the database data you get a empty (0 kb.) file.

So we have the following code:

?
1
2
3
4
5
$command = "mysqldump --opt --skip-extended-insert --complete-insert -h " . $DB_HOST . " -u " . $DB_USER . " -p " . $DB_PASS . " " . $DB_NAME . " > backup.sql" ;
 
exec ( $command , $ret_arr , $ret_code );
echo "ret_arr: <br />" ;
print_r( $ret_arr );

and we get an empty file and no output.

So we will fix this error in a few steps:


1. First we need to make sure that we have access to mysqldump command. For Linux machines this command is accessible from anywhere if not you will have to find the place where mysqldump file is (usually the bin folder of mysql).

In order to do this we have to get some output from our command so we will strip all the options from the command and we will remain with this:

?
1
$command = "mysqldump" ; // mysqldump.exe on Windows

So execute the php script. It’s ok if you get output like this:

Array
(
    [0] => Usage: mysqldump [OPTIONS] database [tables]
    [1] => OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
    [2] => OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
    [3] => For more options, use mysqldump --help
)

If you don’t see something like that then you must check to see the path to the mysqldump command.

If you are in windows make sure you append the full path to the command. If you have a folder like I have C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe with spaces in it you must make sure that you enclose the command between quotes like this:

?
1
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert -h " . $DB_HOST . " -u " . $DB_USER . " -p " . $DB_PASS . " " . $DB_NAME . " > backup.sql" ;

If append the right path to the command and you still cannot get the output then this article can’t help.


2. Make sure you have the rights to create the sql file. This step is mostly for Linux machines where it is very possible that you may try to create a file from php in a folder where you don’t have writing rights.

So to test this after the previous step is done you can do the following: append to the previous command extra options so that the output is not returned but instead written in a file. So we have the previous command:

In Windows:

?
1
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" > backup.sql" ;

In Linux:

?
1
$command = "mysqldump > backup.sql" ;

After running the file “backup.sql” should  be created.

3.  You must now correct your statement. This means that we must use the long versions of the options like this:

Windows:

?
1
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert --host=" . $DB_HOST . " --user=" . $DB_USER . " --password=" . $DB_PASS . " " . $DB_NAME . " > backup.sql" ;

Linux:

?
1
$command = "mysqldump --opt --skip-extended-insert --complete-insert --host=" . $DB_HOST . " --user=" . $DB_USER . " --password=" . $DB_PASS . " " . $DB_NAME . " > backup.sql" ;

The fix: The options for mysqldump when called from php must be in the longer version. Instead of –u use –user, instead of –p use –password and so on.

This entry was posted in  MySQLmysqldumpPHP by  gbl. Bookmark the  permalink.

12 THOUGHTS ON “MYSQLDUMP – WITH EXEC() FUNCTION FROM PHP OUTPUTS EMPTY FILE


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值