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
, 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
makes no difference. I did a
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
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 rn 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.
this may be a charcter encoding issue if you have utf-8 in your source db.
first check the charset of your centos machine
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:
although, if your mysql isn’t set up to handle utf-8 that may cause a whole separate set of errors