PHP - mysqldump doesnt work - php

I am trying to execute
exec('mysqldump -u user -pPASSWORD db1 tbl1 > /etc/table1.sql' ,$output, $return_var);
and also I have tried,
exec('mysqldump -u user -pPASSWORD db1 tbl1 > /etc/table1.sql');
but the $output is empty, and $return_var is 2
I have change apache user's permissions in file /etc/sudoers
#User privilege specification
www-data ALL=(ALL:ALL) NOPASSWD:ALL
Also the following is running on cmd
mysqldump -u user -pPASSWORD db1 tbl1 > /etc/table1.sql
Can someone help me?

I would rather post this as a comment rather than an answer, but I suggest using popen as it might provide more helpful information why your command fails.
Full function signature:
popen (string $command, string $mode)
It works sorta like fopen, including the modes. In your case you will probably want to use 'r' as your mode. The function returns a handle like fopen would do, so you can fread it for the data. Don't forget to pclose the hande after you're done!
To capture any error output (the function returns the handle even if command fails), add 2>&1 (leading space included) after your command. You can probably remove that afterwards, all it does is redirecting errors to the output.
Source:
http://php.net/manual/en/function.popen.php

Related

mysqldump don't work with PHP, just into command line

I am trying to execute this command with php:
system ('mysqldump -u myUser myDbname | mysql -u myUser -A myDbBackupName');
This does not return a error, but does nothing.
The same command executed in server by command line works perfectly.
I am using .my.cnf and i configured the user to mysql, mysqldump and client.
I don't know what is happening. Can somebody help me?
I solved this issue. Put this 2>&1 in the end of the command to force return the output of method exec() or system() to facilit the debug.
The correct mysql command is without space between -p[password]. In fact the password is necessery, independent if you using the archive .my.cnf
Like this:
mysqldump -u user -ppassword myDbName | mysql -u user -ppassword myDbBackupName

Phpmyadmin "Error in processing request" when export table - Error code 500

I'm trying to use PhpMyAdmin v. 4.5.3.1 to access a DB on a localhost and export a table but it is not working.
I can access the DB, insert, search, etc. but when I click on "Export" tab it gives me this message:
I don't have this issue with PhpMyAdmin 4.2.6 using the same WAMP....
Does anyone knows how to fix it?
Thank you!
I think you should use mysqldump instead, when exporting data. From the command line:
mysqldump -uMYSQL-USER -h server -pMYSQL-USER database_name > /path-to-export
Or from a script:
$command = "mysqldump -uMYSQL-USER -h server -pMYSQL-USER database_name > /path-to-export/file.sql";
exec($command, $output, $return_var);
This can easily be automated.
You could fix this error by increasing memory limit to your requirement and restart httpd/apache service. I fixed it sometimes by increasing memory_limit. But now i prefer to use terminal commands only to handle it. Its better to always get habitual using terminal commands for doing such big operations in mysql. You get speed and more control over it as you are not dependent upon GUI based systems.
Use mysqldump in terminal to export data:
mysqldump -u root -p db_name > /home/dump.sql
Use mysqldump in terminal to export only schema without data:
mysqldump -u root -p db_name --no-data > /home/dump.sql

php exec mysqldump doesn't work

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.

when using shell_exec() function in PHP nothing happens

I am trying to execute the following command through PHP using shell_exec() function:
sudo -u USERNAME -pPASSWORD echo "SOME-STRING" > /dev/tcp/127.0.0.1/64444
but nothing happens (if I copy/paste it manually to the terminal it works). There is also nothing in the $output of the shell_exec() function.
So with that command I am trying to send a string over TCP through a different user with sudo privileges.
EDIT #1: The full PHP command looks like this:
shell_exec('sudo -u USERNAME -pPASSWORD echo "RLSET|79-192.168.1.33-0-1" > /dev/tcp/127.0.0.1/64444');
EDIT #2: I've also checked the phpinfo() function and safe_mode is off also the shell_exec() function is not on the disallowed functions list.
EDIT #4: My PHP PATH variable contains the following locations:
/usr/local/bin:/usr/bin:/bin
I'm lost here, any and all help would be appreciated.
Scape your quotes put your full command path:
shell_exec("sudo -u USERNAME -pPASSWORD echo \"FULL-PATH-COMMAND\" > /dev/tcp/127.0.0.1/64444")
maybe your binary (sudo, echo, ...) is not in the PATH env variable :
putenv('PATH', '/bin:/sbin:/usr/bin:/usr/sbin:') // etc...
Have you tried putting a space after -p?
So that should be
shell_exec('sudo -u USERNAME -p PASSWORD echo "RLSET|79-192.168.1.33-0-1" > /dev/tcp/127.0.0.1/64444');
I just tried some commands here on my own machine.
I tried
sudo -u USERNAME -p PASSWORD ls
and it works but when I tried
sudo -u USERNAME -pPASSWORD ls
It give me the usage information.

an alternative to mysqldump?

First of all, I am having serious problems with MYSQLDump, We have a dedicated server here for our main domain and I am running the following command:
mysqldump --opt -h localhost -u root -p ***** --all-databases > ~/var/www/vhosts/mydomain/httpdocs/db.sql
and I get nothing :(
But more importantly, I don't have root access to every server I have access to. But I do have database username and passwords. Surely there is a PHP only way of dumping the entire contents of a SQL database?
then why don't you use your user/password for the databases to do a per database dump as described i.e. here:
http://dev.mysql.com/doc/refman/5.1/de/mysqldump.html
mysqldump [options] --databases db_name1 [db_name2 db_name3...]
i just know two options to backup mysql-databases. One is to use mysqldump, the other one is to stop the mysql-server and backup the databasefiles. Doing dumps using PHP or whatever will last longer and cause much more trouble then just using mysqldump!
I was not aware there was a MySQL root.
Well, then that's the most likely cause of your problems, since you have this:
mysqldump --opt -h localhost -u root -p *****
^^^^^^^
The -u parameter expects a MySQL user and you are probably feeding it with systems' root user, which is something entirely different.
If you have a separate user for each database, I'm afraid you'll have to issue separate dumps.
Additionally, try to fetch error messages. You can redirect stderr to stdout by appending the 2>&1 operator to your command and you can grab output from shell_exec()'s return.
In the mysqldump command there is no space after -p and the password so your line should look like:
mysqldump --opt -h localhost -u root -p***** --all-databases > /var/www/vhosts/mydomain/httpdocs/db.sql

Categories