简体   繁体   中英

MysqlDump Return a Empty database backup file

I use this code for download a backup of MySql database but it download a empty file. Please also guide for restore this backup.

<?php
//connect to database
include'connect.php';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
system($command);
?>

Replace system with echo to show the generated command, and execute it on the command line manually. Errors are usually shown on STDERR, which isn't caught by the system call, and if you get an empty output that means it couldn't output anything. Fix the error and then fix your code. I'd also use passthru instead of system .

To restore the backup afterwards use (from the commandline):

mysql -u<user> -p <database> < myfile.sql

Your variables are sticked to the options. Try to change this:

$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";

To this:

$command = "mysqldump -h $hostname -u $username -p $password $dbname > $backupFile";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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