[英]php exec mysql command - different version
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: 作为较大脚本的一部分,我尝试在php文件中运行以下命令:
$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. 该命令不起作用,如果我添加并回显$result_var
, $result_var
得到127而不是0,因此显然存在一些错误。 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. 也许值得一提:在mysqldump
上使用path没什么区别。 I did a which mysqldump
and the path in first command is correct. 我做了which mysqldump
和第一个命令中的路径是正确的。
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 -?
如果我在命令行中运行它,它只会抛出帮助文本,就好像我运行了mysql -?
What am I missing here? 我在这里想念什么? Are there any other parameters (options) that I need to add to the mysql command? 我还需要向mysql命令添加其他任何参数(选项)吗? And, if so, what has changed? 而且,如果是这样,发生了什么变化? This code was running just fine on another server... 这段代码在另一台服务器上运行得很好。
i can address the -r issue immediately. 我可以立即解决-r问题。 there is also a strong possibility that the -r issue is directly related to your second question. -r问题也很可能与您的第二个问题直接相关。
the -r switch on mysqldump is the short version for --result-file. mysqldump上的-r开关是--result-file的缩写。 this switch does two basic things: 此开关执行两项基本操作:
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. 如果您的源数据库中有utf-8,则可能是字符编码问题。
first check the charset of your centos machine 首先检查centos机器的字符集
cat /etc/sysconfig/i18n
see if it's utf-8. 看看是否是utf-8。 if it isn't that may be why the -r is required. 如果不是,那可能就是为什么需要-r的原因。
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. 如果您的源数据库是utf-8,而目标数据库不是,则这可以解释为什么插入无效。 you can try adding this to your import mysql call: 您可以尝试将其添加到导入mysql调用中:
--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 但是,如果未将mysql设置为处理utf-8,则可能会导致一组单独的错误
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. 我的问题是由php safe_mode引起的-在禁用safe_mode并为用户启用了正常的shell访问之后,现在一切正常。
Also, one minor detail - the command with mysql
requires password without space after -p
. 此外,还有一个小细节mysql
命令使用-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. 不是有效的命令,并且-p
必须跟密码,且不能带空格。 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... 我希望这可以帮助别人...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.