dongmian5325 2018-12-04 16:56
浏览 236

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!

  • 写回答

1条回答 默认 最新

  • douge3113 2018-12-04 19:40
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图