<?
$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问题的完全重复,因为它们的存在原因相同。你们都试图通过SSH隧道错误地使用ssh2_tunnel
来连接服务。
ssh2_tunnel
返回resource
,也就是file pointer
ssh2_tunnel((返回一个套接字对象,这意味着您可以像使用标准套接字函数一样操作这个套接字对象。
我有这样的代码:
fwrite($ssh_tunnel, $db_password);
while (!feof($ssh_tunnel)) {
echo fgets($ssh_tunnel, 1280);
}
我得到这样的结果:
a 5.5.5-10.3.34-MariaDB-cll-lve��=^Aile5x����}.hBNEM5_^:Gmysql_native_password!��#08S01Got packets out of order
我认为fwrite
可以向我们创建的隧道发送一些东西,所以我目前正在考虑应该向哪个隧道发送数据。