php exec mysql command - different version - php

I've been messing with this for the past two days and am still trying to figure it out...
As a part of a larger script, I have the following command that I'm trying to run in a php file:
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql");
This command doesn't work and if I add and echo $result_var, I get 127 instead of 0, so there's obviously some error. After some research, I've managed to come up with a slightly different command and it works just fine:
$export = exec('mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products -r "/path/to/www/directory/sync/products.sql"');
My first question - what's the difference? Why doesn't the first option work?
Maybe worth mentioning: using path on mysqldump makes no difference. I did a which mysqldump and the path in first command is correct.
My second question - I'm trying to run the following command to import a table into my database:
$import = exec('/usr/bin/mysql -h localhost -u MY_USERNAME -p MY_PASS DATABASE_NAME < /path/to/www/directory/sync/products.sql')
But it doesn't work, nothing happens. If I run it in command line, it just throws out the help text, as if I ran mysql -?
What am I missing here? Are there any other parameters (options) that I need to add to the mysql command? And, if so, what has changed? This code was running just fine on another server...

i can address the -r issue immediately. there is also a strong possibility that the -r issue is directly related to your second question.
the -r switch on mysqldump is the short version for --result-file. this switch does two basic things:
it stops \n line endings from being converted by \r\n on windows. if your doing this on a windows box, this may be the reason why -r is required.
it forces the dump to happen even if there are errors. this may explain why the dump you've created isn't being inserted properly. did you inspect your dump file to make sure it's okay?
so, not a solution exactly, but maybe a help getting you looking in the right direction: inspect your dump file and see if there are problems with it.
edit
this may be a charcter encoding issue if you have utf-8 in your source db.
first check the charset of your centos machine
cat /etc/sysconfig/i18n
see if it's utf-8. if it isn't that may be why the -r is required.
then test your two dbs to see what their charsets are
select schema_name as 'database', default_character_set_name as 'charset', default_collation_name as 'default_collation' from information_schema.schemata;
if your source db is utf-8 and your target db isn't, this may explain why the insert doesn't work. you can try adding this to your import mysql call:
--default-character-set=utf-8
although, if your mysql isn't set up to handle utf-8 that may cause a whole separate set of errors

Answering my own question again...
My problem was caused by php safe_mode - after disabling safe_mode and enabling normal shell access for the user, everything is now working just fine.
Also, one minor detail - the command with mysql requires password without space after -p. In other words,
$import = exec('/usr/bin/mysql -h localhost -u MY_USERNAME -p MY_PASS DATABASE_NAME < /path/to/www/directory/sync/products.sql')
is not a valid command and -p must be followed by the password without space. Valid command would be:
$import = exec('/usr/bin/mysql -h localhost -u MY_USERNAME -pMY_PASS DATABASE_NAME < /path/to/www/directory/sync/products.sql')
I hope this helps someone...

Related

Php shell_exec unable to run mysql commands?

Referencing this post as well(How to create database using php shell_exec and sql command line)
I've followed the accepted answer in that post, but I am curious as to why the shell_exec is unable to run mysql code. It merely returns NULL.
The user already has sudo access, when i copy the following command line into the console log manually it works.
$cmd = escapeshellcmd('sudo mysql -u root -e "create database somedb"');
$test = shell_exec($cmd);
var_dump($test);
Edit 1:
Updated [username], if root may cause some issues
You will need to use the full path to mysql
Type whereis mysql to find it.
It will most likely be /usr/bin/mysql
Change your command to:
$cmd = escapeshellcmd('sudo /usr/bin/mysql -u [username] -e "create database somedb"');
You may need full path to sudo .. same principles will apply.

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.

shell_exec with MySQL not working even though safe mode is off

I have a string of script which working in terminal but does not work when I use it in PHP with shell_exec().I know a lot of questions similar to this question has been asked already but in my case the problem I am facing is that I have already tried the proposed solutions I found. Below is my simple code.
<?php
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
echo "<pre>$output</pre>";
?>
So far this is the best solution I have found.
Does anyone knows what might be wrong?
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');

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