尝试通过PHP的SSH TUNNEL在防火墙中连接远程mongodb

时间:2019-06-23 07:02:25

标签: php mongodb ssh-tunnel

我正在尝试通过php代码中的ssh隧道在防火墙下连接远程mongodb服务器,但事实并非如此。

我通过php ssh2_tunnel包进行了尝试,并执行了mongodb插入查询。

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
require_once (__DIR__ . '/../vendor/autoload.php');
require_once('/root/test/test_db_helper.php');

$helper        = new TestDBHelper();

$ssh_conn = ssh2_connect($ssh_host, 22);
if ($ssh_conn) {
    print "connection successful"."\n";
} else {
    print "connection failed"."\n";
    die();
}

$ssh_auth = ssh2_auth_password($ssh_conn, $ssh_username, $ssh_pwd);
if ($ssh_conn) {
    print "authentication successful"."\n";
} else {
    print "authentication failed"."\n";
    die();
}

$ssh_tunnel = ssh2_tunnel($ssh_conn, $ssh_host, 27017);
if ($ssh_tunnel) {
    print "tunnel successful"."\n";
    $dbConnection   = $helper->createConnection();
    $database   = 'test_db';
    $option     = ["multi" => true, "upsert" => true];
    $db         = $dbConnection->selectDatabase($database);
    $collection = 'test';
    $namespace  = $database.'.'.$collection;
    $coll       = $db->selectCollection($collection);
    list($manager,$writeConcern,$bulk) = $helper->createBulkConnection();
    $filter = ['test' => 1];
    $update = [
                    '$set' => [
                        'data.'.'00:00:00' => [
                           'data' => 'value'
                        ]
                    ]
                ];
    $bulk->update($filter, $update, $option);
    $manager->executeBulkWrite($namespace, $bulk, $writeConcern);
    print "data inserted";
} else {
    print "tunnel failed"."\n";
    die();
}

预期将在db中插入数据的输出。

收到错误消息: 致命错误:未捕获的MongoDB \ Driver \ Exception \ ConnectionTimeoutException:未找到合适的服务器(设置了serverSelectionTryOnce):[无法解析/root/test/test.php中的/ server_name /]第46

MongoDB \ Driver \ Exception \ ConnectionTimeoutException:未找到合适的服务器(设置了serverSelectionTryOnce):/root/test/test.php中的[解析/ server_name /失败] 46

0 个答案:

没有答案