2

I am working on backup and recovery of mySQL database using mysqldump. My code generates an sql file but its empty. Here's my complete code. Thanks a lot.

<?
   shell_exec("mysqldump -u root -p ilamdb > db/ilamdb.sql");
   echo "Back up complete.";
?>
5
  • There is probably an error. You've already redirected the stdout to a file; try redirecting the stderr to another file (2>anotherfile) so you can capture the error message and look at it afterwards.
    – Celada
    Commented Mar 8, 2013 at 12:19
  • I've tried placing the file outside the folder but it has the same result. The page will display the string, file on the folder but still empty. Commented Mar 8, 2013 at 12:25
  • OK, but that has nothing to do with what I suggested...
    – Celada
    Commented Mar 8, 2013 at 12:26
  • Can you explain much further what you've suggested? Commented Mar 8, 2013 at 12:29
  • You are already capturing the stdout of the command into a file (with > db/ilamdb.sql). I suggest that you also capture the stderr (with 2>anotherfile) so that you will be able to see the error message that the mysqldump command produces.
    – Celada
    Commented Mar 8, 2013 at 12:31

2 Answers 2

3

You used the option -p which tells mysqldump to ask you for a password – which of course only works in an interactive shell. You will have to specify your password directly:

shell_exec("mysqldump -u root --password=yourpassword ilamdb > db/ilamdb.sql");

If you don't use a password, simply leave out this parameter:

shell_exec("mysqldump -u root ilamdb > db/ilamdb.sql");
4
  • 1
    syntax should be mysqldump -uroot -pyourpassword ilamdb > db/ilamdb.sql
    – Ghigo
    Commented Mar 8, 2013 at 13:08
  • what if , if my mysql doesnt have a password? Commented Mar 8, 2013 at 17:39
  • If you don't have a password, leave out the -p in your code. Commented Mar 11, 2013 at 12:16
  • perfect: shell_exec("mysqldump -u root ilamdb > db/ilamdb.sql"); worked for me!
    – codelone
    Commented Oct 1, 2022 at 19:20
0

MySQL backup syntax should be as below

mysqldump -u --password= >File.dmp

Not the answer you're looking for? Browse other questions tagged or ask your own question.