gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:35:32 25 4
gpt4 key购买 nike

<?
$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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com