OrangeRind OrangeRind - 1 year ago 120
MySQL Question

Unable to execute mysqldump from PHP script

I am trying to create periodic backups (poor man's cron) of my database using

function. I am using XAMPP/PHP7 on macOS.

$command = "$mysqldump_location -u$db_user -h$db_host -p$db_password $db_name > $backup_file_location";

When I run the PHP script, I get no SQL dump in the path mentioned in
but if I execute the same
string on the terminal directly I get the desired SQL file in the desired location.

I am unable to understand what could be the problem here. Also open to suggestions on better ways to dump the entire DB.


The value of

The value of

is the folder in while I am developing my app.

Answer Source

The resolution of the issue, from above comments, was that the PHP request executes in XAMPP as a user that has limited privileges, and the mysqldump process inherits those privileges.

Checking the exit status of the process run by exec() confirmed that mysqldump exited with a nonzero exit status, indicating it failed for some reason.

Opening write privileges to 777 on the directory where the mysqldump process tries to write resolved the error.

It should also be adequate to figure out the specific uid & gid of Apache processes (check the User and Group config values in the Apache config file (e.g. xampp-home/apache/conf/httpd.conf) and make the output directory writeable by that uid or gid.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download