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

标签 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没有问题。 我已经测试过它们。并且效果很好。

最佳答案

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

ssh2_tunnel 返回一个资源,也称为文件指针

关于php(ssh2,tunnel),我无法连接我的远程mysql服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53617955/

相关文章:

PHP 没有为特定用户显示 MySQL 表中的文本

python - MultiValueDictKeyError 在/addbook/

php - MySQL - 如何从 SELECT 查询中获取多个结果

php - 更新时尝试获取 CodeIgniter 中非对象的属性

PHP/MYSQL : How to create image galleries that rotate/change during each page refresh/load

PHP:如何使用 Twitter API 数据将推文中的 URL、提及和主题标签转换为链接?

添加第三个 INNER JOIN 后,mySQL 查询需要更长的时间

php - 计算具有相同 id 的行数 [PHP]

php - 在同一个查询中,MySql 可以对行进行计数并只获取第一行吗?

php - 过滤文件中的查询结果