您的位置:首页 > 运维架构 > Linux

php 操作linux下的mysql开启关闭

2016-08-26 19:16 399 查看
<?php  

header('content-type:text/html;charset=utf8');

$status = $_GET['status'];

$host=“写自己的”';//被控制的linux的ip  

 

$user='写自己的';//用户名  

 

$passwd='写自己的';//密码  

 

// 链接远程服务器  

 

$connection = ssh2_connect($host, 22);  

 

if (!$connection) die('connection to '.$host.':22 failed');  

 

//echo 'connection OK<br/>';  

 

// 获取验证方式并打印  

 

$auth_methods = ssh2_auth_none($connection, $user);  

//print_r( $auth_methods.'<br/>');  

 

if (in_array('password', $auth_methods ))  

{  

 

    // 通过password方式登录远程服务器  

 

    if (ssh2_auth_password($connection, $user, $passwd))  

 

    {  

 

       //echo $user.' login OK<br/>';  

 

        $stream = ssh2_exec($connection, "pwd"); // 执行php  

 

        stream_set_blocking($stream, true); // 获取执行pwd后的内容  

         /*$stream = ssh2_exec($connection, "ls");

           echo 'pwd: '.stream_get_contents($stream).'<br/>';*/

          //$stream = ssh2_exec($connection, "ls");

          //echo 'pwd: '.stream_get_contents($stream).'<br/>';

          if($status == 0){

                   $stream = ssh2_exec($connection, "service mysqld stop"); // 执行php  

          }else if($status == 1){

                  $stream = ssh2_exec($connection, "service mysqld start"); // 执行php  

          }

          $c = @mysql_connect($host,'root','root');

          //var_dump(@mysql_connect($host,'root','root'));die;

          if($c){

              $a = 1;

          }else{

              $a = 0;

          }

          //$stream  =  ssh2_exec ( $connection ,  'shutdown -h -now' );

        //if ($stream === FALSE) die("pwd failed");  

         

 

    }  

 

    else  

 

    {  

 

        die( $user.' login Failed<br/>');  

 

    }

 }

 ?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

 <head>

     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

     <title>Document</title>

 </head>

 <body>

     <table>

         <tr>

             <td>主机昵称</td>

             <td>IP地址</td>

             <td>mysql服务状态</td>

             <td>操作</td>

         </tr>

         <tr>

             <td><?php echo $user;?></td>

             <td><?php echo $host;?></td>

             <td><?php

                 if($a == 1){

                     echo "开启";

                 }else{

                     echo "关闭";

                 }

             ?> </td>

             <td>

                 <?php

                 if($a == 1){

                     echo "<a href='yuekao.php?status=0'>关闭</a>";

                 }else{

                     echo "<a href='yuekao.php?status=1'>开启</a>";

                 }

             ?>

             </td>

         </tr>

     </table>

 </body>
 </html>

注意事项:

1. linux为centos6.4(如果为centos7的话只需更改启动和关闭的语法)

2.记得为linux下mysql的远程赋权
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: