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

vjrehmav  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(546)
<?
   $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没有问题。我已经测试过了。而且效果很好。
请帮帮我!

vwoqyblh

vwoqyblh1#

这与postgresql问题完全相同,因为它们的存在原因相同。你们两个都试图通过ssh隧道通过 ssh2_tunnel 不正确。 ssh2_tunnel 返回一个 resource 又名a file pointer

相关问题