Your shell_exec
is this:
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
And your command is this:
mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql
The reason that command works when you are in the shell is the binary path to mysql
is part of your user login profile.
To see what I mean, login to the shell as yourself and then type echo $PATH
and what you will see is a list of search paths the shell uses to figure out where binaries you are attempting to run are located.
But when you attempt to run a script via shell_exec()
the Apache server user running PHP is making the sell call. And that user typically does not have binary paths set. So you need to provide the full path to mysql
which might be:
/usr/bin/mysql
Or:
/usr/local/bin/mysql
The best solution is from the shell use the which
command like so:
which mysql
And then take the full path provided and adjust your shell_exec()
command as follows; using /usr/bin/
for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 mydatabase < db.sql');
Also, where is db.sql
actually located? You would have to prepend the full path to that MySQL script like this as well; using /full/path/to/this/
for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 my database < /full/path/to/this/db.sql');