简体   繁体   English

php(ssh2, tunnel), 我无法连接远程 mysql 服务器

[英]php(ssh2, tunnel), I can't connect my remote mysql server

<?
   $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();
?>

This is my code.这是我的代码。 mysql is on ubuntu device. mysql 在 ubuntu 设备上。 And I think I can access ubuntu, but mysql connection is failed我想我可以访问 ubuntu,但是 mysql 连接失败

this php's output is这个 php 的 output 是

Authentication Successful!
tunnel created
test connected

and if i use this line如果我使用这条线

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

then output is那么 output 是

Authentication Successful!
tunnel created
DBConnection failed:

The obvious thing is that $connection and $tunnel has no problem.显而易见的是 $connection 和 $tunnel 没有问题。 I had tested them.我测试过它们。 And works well.并且效果很好。

This is an exact duplicate of the postgresql question because they both exist for the same reason. 这与postgresql问题完全相同,因为它们都是出于相同的原因而存在。 You're both trying to connect a service over a SSH Tunnel by using ssh2_tunnel incorrectly. 你们都试图通过不正确地使用ssh2_tunnel通过SSH隧道连接服务。

ssh2_tunnel returns a resource aka a file pointer ssh2_tunnel返回resource也就是file pointer

The ssh2_tunnel() return a socket object which means you can operate this socket object just like you use standard socket function. ssh2_tunnel() 返回套接字 object,这意味着您可以像使用标准套接字 function 一样操作此套接字 object。

I have such code:我有这样的代码:

    fwrite($ssh_tunnel, $db_password);
    while (!feof($ssh_tunnel)) {
          echo fgets($ssh_tunnel, 1280);
    }

and I get such result:我得到这样的结果:

a 5.5.5-10.3.34-MariaDB-cll-lve��=^Aile5x����}.hBNEM5_^:Gmysql_native_password!��#08S01Got packets out of order

I think fwrite can send something to such tunnel we have created, so I am currently figuring out what data should be sent to.我认为fwrite可以向我们创建的隧道发送一些东西,所以我目前正在弄清楚应该发送什么数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM