Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to execute mysqldump from PHP script

I am trying to create periodic backups (poor man's cron) of my database using mysqldump with exec() 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";
exec($command);

When I run the PHP script, I get no SQL dump in the path mentioned in $backup_file_location but if I execute the same $command 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.

Edit 1:

The value of $mysqldump_location is /Applications/XAMPP/xamppfiles/bin/mysqldump

The value of $backup_file_location is /Applications/XAMPP/xamppfiles/htdocs/app5/data/sqldumps/sql_data.sql

/app5/ is the folder in while I am developing my app.

Edit 2: Possible duplicate suggestion does not apply since the issue here was not on how to dump SQL backups. The key issue here was that the backup using mysqldump was working through terminal, but not through PHP's exec() function.

like image 452
OrangeRind Avatar asked Nov 26 '25 08:11

OrangeRind


1 Answers

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.

like image 64
Bill Karwin Avatar answered Nov 27 '25 23:11

Bill Karwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!