php(ssh2,tunnel),我无法连接我的远程mysql服务器
<?
   $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. And I think I can access ubuntu, but mysql connection is failed

this php's output is

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

Authentication Successful!
tunnel created
DBConnection failed:

The obvious thing is that $connection and $tunnel has no problem. I had tested them. And works well.

please help me!

dp198879
dp198879 PHP的可能重复使用ssh2_tunnel连接到PostgreSQL
接近 2 年之前 回复
dpstir0081
dpstir0081 test_table有数据。我无法从mysqli获得错误消息
接近 2 年之前 回复

1个回答



这是postgresql问题的完全重复,因为它们都存在同样的原因。 您正在尝试通过错误地使用 ssh2_tunnel </ code>来通过SSH隧道连接服务。</ p>

ssh2_tunnel </ code>返回资源 </ code>又名文件指针</ code> </ p>
</ div>

展开原文

原文

This is an exact duplicate of the postgresql question because they both exist for the same reason. You're both trying to connect a service over a SSH Tunnel by using ssh2_tunnel incorrectly.

ssh2_tunnel returns a resource aka a file pointer

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问