dongmaopan5738 2018-04-20 12:21
浏览 97
已采纳

在Linux中使用PHP / Shell脚本从浏览器重新启动MySql Server

I want to restart mysql service on click of the button using php.

I have developed application using php till now I tried below things and facing the problem

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  var_dump(exec("sh rst.sh"));
  exit;
}
?>

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

What I am getting is:

string(55) "Restarting mysql (via systemctl): mysql.service failed!" 

rest.sh:

#!/bin/bash
/etc/init.d/mysql restart

while executing above file from the commnad line it asks for the password.

any help will be appreciated.

Thanks

  • 写回答

3条回答 默认 最新

  • 普通网友 2018-04-20 12:44
    关注

    Granting the user that runs PHP/web server permissions to restart any service is generally a bed idea from security perspective not even taking about permission to manage all system daemons or full sudo.


    Once you have been warned, you have to make sure the user that is running the PHP script has the permission to run the restart command. As already stated, the issue has likely little to do with PHP. There are several ways from you to proceed here:

    • You can grant the PHP user the sudo permission to run the desired command and nothing else (how to do that is covered elsewhere) and invoke the command directly from PHP: sudo systemctl restart mysql.

    • Keep the rst.sh file, get it owned by root, writeable by noone else but root and set SUID bit on that file. This way you can invoke the script without sudo but the script will be run as root thanks to the SUID bit.

    #1 feels safer and simpler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?