I am trying to take mysqldump of database through php, i am able to take database dump through command line this way only.
d:
cd "d:\wamp\bin\mysql\mysql5.5.24\bin"
mysqldump.exe
mysqldump --user=user --password=password --host=localhost db_name > "D:\dump\test.sql"
when trying to execute this command from php this way, i get a empty dump file.
$cmd = 'd: cd "d:/wamp/bin/mysql/mysql5.5.24/bin"mysqldump.exe>mysqldump --user=user --password=password --host=localhost db_name > "D:\dump\test.sql"';
exec($cmd);
or this way
$cmd = 'd:/wamp/bin/mysql/mysql5.5.24/bin>mysqldump.exe>mysqldump --user=user --password=password --host=localhost db_name > D:\dump\test.sql';
exec($cmd);
or this way
$cmd = 'd:/wamp/bin/mysql/mysql5.5.24/bin>mysqldump --user=user --password=password --host=localhost db_name > D:\dump\test.sql';
exec($cmd);
I get only empty dump file.
i have gone through a lot of questions here like this, and this and lot more, but none of that solution is working for me.
Please see and suggest any possible way to do it.
Thanks.
You seems to use Windows, the way to create multi-command lines is to use the '&' operator (more info)
Example :
$cmd = 'd: & cd "d:/wamp/bin/mysql/mysql5.5.24/bin" & mysqldump.exe --user=user --password=password --host=localhost db_name > "D:\dump\test.sql"';
exec($cmd);
Also, double-check your paths because you are using d:/wamp/... and later D:\dump\test.sql, it seems that your command use slash and antislash and only \ should work.
At last, you can debug your command using a second argument to exec :
$output = array();
exec($cmd, $output);
var_dump($output);
Related
I'm trying to run a mysqldump from my php file using exec(). File is located in my website's www directory and I'm accessing it from browser. But, no matter what I try, I'm simply not getting ANY results and no sql file is created.
Here's my command:
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql");
exec() is enabled. sync directory permissions are 777.
If I use the same command, but add $output, $return_var like this:
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql", $output, $return_var);
$output is an empty array and $return_var echoes 127.
Path to mysqldump is correct:
root#server [~]# which mysqldump
/usr/bin/mysqldump
root#server [~]#
If I run the command from console, it works just fine and the new file gets created in my sync directory.
Can someone please tell me what I'm doing wrong and what I'm missing here? The script used to work just fine until we moved to a new server...
I've been searching all over the place and have stumbled upon something that turned out to solve my problem.
Instead of using
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql");
I am now using this and THIS WORKS:
$export = exec('mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products -r "/path/to/www/directory/sync/products.sql"');
Using path to mysqldump doesn't make a difference. Using options (in this case -r) solved it for me, although I don't really understand why the original solution didn't work.
Maybe worth mentioning - I noticed that I can't import a database using the standard mysql -u -p db < table.sql - running that command just shows me help instructions, as if I ran mysql -?. I'm not sure if this is specific to me and my server, but maybe the solution above helps someone in a similar situation.
I try to save my database in PHP but not work its work only with Shell
With shell (work good)
mysqldump --user=root --password= --host=localhost site > C:\xamppp\htdocs\site\sql\tests.sql
With php (file is created but empty)
system('mysqldump --user=root --password= --host=localhost site > C:\xamppp\htdocs\site\sql\test.sql', $result);
for $result i have 1
I work with XAMP on Windows 7, i test exec and system but its same
Thanks you
$return_var = NULL;
$output = NULL;
$command = "mysqldump -u mysql-user -h your_host -pmysql-pass database_name > /directory_path/file.sql";
exec($command, $output, $return_var);
or you can refer to this answer mysqldump via PHP
Use 2>&1
<?php system("mysqldump --user=root --password= --host=localhost site > C:\xamppp\htdocs\site\sql\test.sql 2>&1", $result); ?>
I have run mysqldump on a test database using the bash command line and it works fine:
C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump --opt -u root -p membersite > c:\\wamp\\www\\php-backup\\membersitebackupfile1.sql
When I put the same line in a PHP file located in "c:\wamp\www\php-backup\dump.php" it produces a file with 0 bytes.
<?php
$command = "C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump --opt -u root -p membersite > c:\\wamp\\www\\php-backup\\membersitebackupfile1.sql";
exec($command);
?>
If you are using wamp then try:
$cmd = 'c: & cd "C:/wamp/bin/mysql/mysql5.6.17/bin" & mysqldump.exe --user=root --password=root --host=localhost databasename> "C:\Directory\SQL.sql"';
exec($cmd);
I created a script to copy a remote database using mysqldump inside a SSH connection. Then i ported to my PHP script so i can better manage several servers.
My problem is when the copy fails... I get no error code from the command.
This is the command:
ssh -p22 -i mykey.key -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null backup#hostname "mysqldump -hlocalhost -udbuser --databases db" > myfile.sql
It works =)
PHP code with simplified systax:
$comando = 'ssh -i mykey.key backup#hostname "mysqldump --databases db" > myfile.sql';
$ret_text = exec($comando, $output, $ret);
// Doesnt matter if command fails or sucess, result is?
//$ret = empty
//$output = empty
//$ret_text = empty
I need to know when the copy failed, does anyone know how to get a remote error code?
Thanks
neubert Solution Works! Thanks...
I ended with:
$comando = 'ssh -i mykey.key backup#hostname "mysqldump --databases db 2>&1\" 2>&1 > myfile.sql';
I keep getting empty files generated from running
$command = 'mysqldump --opt -h localhost -u username -p \'password\' dbname > \'backup 2009-04-15 09-57-13.sql\'';
command($command);
Anyone know what might be causing this? My password has strange characters in it, but works fine with connecting to the db.
I've ran exec($command, $return) and outputted the $return array and it is finding the command. I've also ran it with mysqldump > file.sql and the file contains
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
So it would seem like the command is working.
Remove the space between -p and the password. If it didn't work, try to remove the quotes from the password
from MySQL documentation:
If you use the short option form (-p), you cannot have a space between the option and the password.
however, it is fine to have space with -h and -u options
I believe there are no spaces between -u and the actual username.
host: localhost user: peter password: pwd
would become:
-hlocalhost -upeter -ppwd
This is how I have done it - output is with maximum gzip compression:
<?php exec("/usr/bin/mysqldump --opt --host=MYSQLHOSTNAME --user=MYSQLUSER --password=PASSWORD DATABASENAME | gzip -v -9 >DATABASENAME.". date("Y-m-d_H-i-s") . ".sql.gz");?>
To put it in plain english, make sure to use the following options (all of them).
--user=USERNAME
--host=localhost
--password=****
The next non-option phrase should be your database name. If the command is followed by another non-option phrase, it will be treated as table names.
$command="mysqldump --xml --host=localhost --user=USERNAME --password=***** DBNAME > XMLTABLE.xml";
system($command);
$command = 'C:\xampp\mysql\bin\mysqldump --opt --user=root --host=localhost --password="password" my_db'.' > '.$backupdate.$sql_file_name; exec($command);
I faced the same issue and got it fixed by quoting the password. For example --password="yourpassword".
I had empty files too using mysqldump.
I run WampServer PHP7 under Windows 10.
system('mysqldump .... ') ;
Doen't work.
I had to add the full path (or add an Environment variable) :
system('C:\wamp64\bin\mysql\mysql5.7.9\bin\mysqldump.exe ...') ;
You have to specify full path to mysqldump:
// Linux:
$command = '/usr/bin/mysqldump --opt -h localhost -u username -p \'password\' dbname > \'backup 2009-04-15 09-57-13.sql\'';
// Windows:
$command = 'c:\mysql\bin\mysqldump --opt -h localhost -u username -p \'password\' dbname > \'backup 2009-04-15 09-57-13.sql\'';