douniwan_0025 2014-12-05 09:06
浏览 133

如何使用PHP页面中的shell脚本执行MySQL命令?

I want to execute a shell script from php page which will execute a MySQL command.

To do this I followed the way shown here: https://stackoverflow.com/a/8055745/2117868

Here is my sqlscript.sh

#!/bin/sh
sudo wget -t 50 -O /tmp/update.sql http://example.com/update.sql
if [ $? -eq 0 ]; then
    mysql -h "localhost" -u "root" "-pXXXXXXXX" "database-name" < "/tmp/update.sql"
    if [ $? -eq 0 ]; then
        sudo rm /tmp/update.sql
        echo "200"
    else
        echo "502"
    fi
else
    echo "404"
fi

And my php page is runscript.php

<?php
    shell_exec("sudo /path/to/script/sqlscript.sh");
?>

Now, when I'm calling the sqlscript.sh from the server console or php page runscript.php it works perfectly and returns 200 as expected.

But when I take MYSQL user and password in ~/.my.cnf so I don't have to put it on the command-line at all:

[client]
user = root
password = XXXXXXXX

And added credentials in sudoer to execute this script without password.

User_Alias WWW_USER = www-data
Cmnd_Alias WWW_COMMANDS_SQL = /path/to/script/sqlscript.sh
WWW_USER ALL = (ALL) NOPASSWD: WWW_COMMANDS_SQL

Here is my sqlscript.sh

#!/bin/sh
sudo wget -t 50 -O /tmp/update.sql http://example.com/update.sql
if [ $? -eq 0 ]; then
    mysql -h "localhost" "database-name" < "/tmp/update.sql"
    if [ $? -eq 0 ]; then
        sudo rm /tmp/update.sql
        echo "200"
    else
        echo "502"
    fi
else
    echo "404"
fi

Now, when I'm calling the sqlscript.sh from the server console it works perfectly and returns 200 as expected. But if I call it from the php page

http://example.com/runscript.php

it returns 502 as it couldn't execute the SQL command.

Update:

Here is the Apache error.log

--2014-12-05 10:51:55--  http://example.com/update.sql
Resolving example.com (example.com)... 162.144.71.XXX
Connecting to example.com (example.com)|162.144.71.XXX|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 146 [text/x-sql]
Saving to: `/tmp/update.sql'

     0K                                                       100% 10.9M=0s

2014-12-05 10:51:55 (10.9 MB/s) - `/tmp/update.sql' saved [146/146]

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

So, please help me to solve the issue.

  • 写回答

2条回答 默认 最新

  • doucang2831 2015-02-26 03:54
    关注

    Have you tried this?

    In your shell script, instead of

    mysql -h "localhost" "database-name" < "/tmp/update.sql"
    

    use

    mysql -h "localhost"-u root -ppassword "database-name" < "/tmp/update.sql"
    

    Basically, try to pass MySQL username and password inside your shell script. Of course you can put them into variables/configs/whatever to obsecure it

    MySQL 28000 error showed that your script was able to attempt to login as a root but because there was no parameter password, it failed

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)