My system: Ubuntu 11.10, LAMP Stack.
Issue:
I run the following in terminal and it does the back up correctly.
mysqldump -u root dbBugTracker > BAK/dbw.sql
But I include it in my php code like the following and it does NOT work.
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql');
Tips:
- I tried putting a second parameter in exec but nothing is shown except the word Array. I print it out but nothing in it.
- The file dbw.sql is actually created as a result of the exec function but it is 0 bytes.
- I tried with the full path and without for mysql and the same result is seen. i.e., 0 bytes.
- The folder BAK is within my project folder and I even gave it 777 permissions.
- Even tried different file names and databases but the result is the same.
I appreciate any inputs on this. Thank!
MORE INFO:
I added 2>&1 to the exec line and NOW the file contains some text but NOT the DB dump. This is an error and I have no idea how to deal with this 🙁
Here’s the error
mysqldump: Got error: 1045: Access denied for user ‘root’@’localhost’ (using password: NO) when trying to connect
So this is what the output file (dbw.sql) now contains.
Once again, it works fine when I run the dump from terminal.
You’re running that dump command as a different user while on the command line. You are running it as Apache (I assume) when using exec(). Try adding a password parameter to the exec command, or creating an php-specific user in your db with appropriate privileges.
UPDATE:: As I guessed, you are not able to use the root user while executing this dump using PHP. So, create a new user.
First, login to your database from the command line. If you are the root user, don’t bother with using -u root:
Now that you’re logged in, go ahead and create a new user for Apache to use:
Go ahead and logout of mysql:
Next, let’s re-work your original code a bit…
Now, to execute the file, run this from the command line:
Hopefully you can adapt this pseudo-code. Best of luck!