PHP(SSH2,隧道),我无法连接我的远程MySQL服务器

时间:2018-12-04 16:57:00

标签: php mysql mysqli tunnel ssh2

<?
   $server_ip = "my ubuntu ip";
   $server_port = "my ubuntu port";
   $server_user = 'my id';
   $user_pw = 'my pw';
   $connection = ssh2_connect($server_ip, $server_port);
   if (ssh2_auth_password($connection, $server_user, $user_pw)) {
     echo "Authentication Successful!<br>";
   } else {
     echo "Authentication failed...";
   }

   $mysql_ip = '10.41.12.71';
   $mysql_default_port = '3306';

   $tunnel = ssh2_tunnel($connection,$mysql_ip,$mysql_default_port);
   if($tunnel) {
       echo "tunnel created<br>";
   }else {
       echo "tunnel creation failed";
       die();
   }

   $mysql_user = 'root';
   $mysql_user_pw = 'tkakcy159*';
   $dbname = 'test';
  //  $dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);
   $dbconn = new mysqli($tunnel, $mysql_user, $mysql_user_pw, $dbname);
   if ($dbconn) {
    echo $dbname." connected<br>";
  } else {
    echo "DBConnection failed: " . $dbconn->connect_error;
  }

  // $insert_sql = " insert into test_table values('0001','wlsdhks0423'); ";
  // $result = mysqli_query($dbconn,$insert_sql);
  // if($result) {
    //  echo "data insert success<br>";
    // }
    // else {
  //   echo "data insert failed : ".mysqli_error($dbconn);
  // }

  $select_sql = " select * from test_table; ";
  $result = mysqli_query($dbconn,$select_sql);

  if($result) {
    echo "selected rows : ".mysqli_num_rows($result)."<br>";
    $row = mysqli_fetch_array($result);
    $key = $row['test_key'];
    $value = $row['test_value'];
    echo "key = ".$key."<br>value = ".$value;
  }else {
    echo mysqli_error($dbconn);
  }


   // phpinfo();
?>

这是我的代码。 mysql在ubuntu设备上。 而且我认为我可以访问ubuntu,但是mysql连接失败

此php的输出为

Authentication Successful!
tunnel created
test connected

如果我使用此行

$dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);

然后输出为

Authentication Successful!
tunnel created
DBConnection failed:

显而易见的是$ connection和$ tunnel没问题。 我已经测试过了。而且效果很好。

请帮助我!

1 个答案:

答案 0 :(得分:0)

这与postgresql问题完全相同,因为它们都是出于相同的原因而存在。你们都试图通过错误使用ssh2_tunnel来通过SSH隧道连接服务。

ssh2_tunnel返回resourcefile pointer