简体   繁体   中英

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. 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.

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

The ssh2_tunnel() return a socket object which means you can operate this socket object just like you use standard socket function.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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