dongyun9120 2014-06-05 19:27
浏览 62
已采纳

shell即使安全模式关闭,MySQL也无法正常工作

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?

  • 写回答

1条回答 默认 最新

  • doudiewen9435 2014-06-05 19:32
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路