Mysqldump is not working correctly

I am writing code from PHP to back up a database.

Here's my code:

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

      

But I am getting 0 bytes in my file (mydb.sql). I also run with passthru (), system (), but it still gets a 0 byte value.

I am trying to use the command. He works.

I am using the latest XAMPP for my localhost.

So how can I make it work correctly?

+2


a source to share


5 answers


This is probably a permissions issue, or the fact that you are not passing the password. To see errors, put STDERR in STDOUT with2>&1

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

      



This will show you the errors you usually see on the command line.

+9


a source


Most likely mysqldump is not in the PHP / Apache path. This will cause the shell to spit out a "command not found" error on STDERR and not output to STDOUT (which you redirect to a file and end up with 0 lengths).



Try adding the full path to mysqldump ( c:\mysql\bin\mysqldump

or whatever) and try again.

+2


a source


you need to pass the password without a space after the -p parameter, and the same for the username will go right after -u

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

i the anther issue on shared host, it turned out that I have to use the host username and password and not the database username and password

+1


a source


try it!

$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);

      

0


a source


commands like exec () and system () do not work on all servers, only local servers and dedicated servers, if you host a server you will need to see if your hosting plan allows exec (command) and system () access unless you let me hire another plan.

0


a source







All Articles