KimKha
KimKha

Reputation: 4510

mysqldump doesn't run correctly

I write some code with PHP to backup database.

Here my code:

exec("mysqldump --opt -h localhost -u root test > mydb.sql");

But I get 0-byte in my file (mydb.sql). I also run with passthru(), system(), but it still gets 0-byte.

I try to use command. It works.

I use the lastest XAMPP for my localhost.

So, how can I do to make it works correctly?

Upvotes: 2

Views: 4916

Answers (5)

Flaviano Silva
Flaviano Silva

Reputation: 117

commands such as exec () and system () do not work on all servers, only local servers and dedicated servers, if you host a server will have to see whether your hosting plan allows access to exec (command) and system () if you will not allow to hire another plan.

Upvotes: 0

Flaviano Silva
Flaviano Silva

Reputation: 117

try this!

$username = "root";
$password = "";
$hostname = "localhost";
$dbname   = "test";

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));

$command = "C:\AppServ\MySQL\bin\mysqldump --add-drop-table --host=$hostname --   user=$username --password=$password ".$dbname;

system($command);

Upvotes: 0

Bakly
Bakly

Reputation: 650

you have to pass the password with no space after the -p option and the same for the username goes directly after -u

exec("mysqldump --opt -h localhost -uUSER -pPASSWORD DBNAME > mydb.sql");

i anther problem on a shared host it turned out i have to use the host username and password not the database username and password

Upvotes: 1

Marc B
Marc B

Reputation: 360572

Most likely mysqldump is not in PHP/Apache's path. That will cause the shell to spit out a "command not found" error on STDERR, and no output on STDOUT (which you redirect to a file and ends up with 0 length).

Try adding the full path to mysqldump (c:\mysql\bin\mysqldump or whatever) and try again.

Upvotes: 2

Andy
Andy

Reputation: 17771

It's likely a permissions issue, or the fact you're not passing a password. To see errors, route STDERR to STDOUT using 2>&1

exec("mysqldump --opt -h localhost -u root test > mydb.sql 2>&1", $output);
print_r($output);

This will show you the errors you'd normally see on the command line.

Upvotes: 8

Related Questions