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 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?