I'm trying to connect with php to a mongodb replica set over a ssh tunnel, but it fails when performing the query.
This is how I connect:
$MongoDBHosts = ['127.0.0.1:27018','127.0.0.1:27019','127.0.0.1:27020',];
$MongoDBUri = 'mongodb://'.implode(',', $MongoDBHosts).'/';
$MongoDBUriOptions = ['username' => 'USERNAME','password' => 'PASSWORD','replicaSet' => 'REPLICASET_NAME','authSource' => 'MYAUTHSOURCE',];
$MongoDBName = 'MyDB';
$mongoConnection = new MongoDB\Client($MongoDBUri,$MongoDBUriOptions);
$mongoDBLink = $mongoConnection->selectDatabase($MongoDBName);
I also have 3 ssh tunnels to each one of the nodes in this way:
ssh -p 1989 node01 -L 27018:127.0.0.1:27017 -N
ssh -p 1989 node02 -L 27019:127.0.0.1:27017 -N
ssh -p 1989 node03 -L 27020:127.0.0.1:27017 -N
And this is the final code that performs the query:
$documentId = new \MongoDB\BSON\ObjectId($documentId);
$document = $mongoDBLink->selectCollection('MYCOLLECTION')->findOne(['_id' => $documentId]);
print_r($document);
This is the error output:
PHP Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (
serverSelectionTryOnce
set): [connection timeout calling ismaster on '172.191.49.187:27017'] [connection timeout calling ismaster on '172.191.49.194:27017'] [connection timeout calling ismaster on '172.191.49.206:27017'] in /XXXX/vendor/mongodb/mongodb/src/Collection.php:546 Stack trace:0 /XXXX/vendor/mongodb/mongodb/src/Collection.php(546): MongoDB\Driver\Manager->selectServer(Object(MongoDB\Driver\ReadPreference))
1 /XXXX/code.php(8): MongoDB\Collection->findOne(Array)
2 {main} thrown in /XXXX/vendor/mongodb/mongodb/src/Collection.php on line 546
Final data, the connection to the replica set work well for nodes inside the same private network (instead of 127.0.0.1:PORT, I use the private ip from each node in replica set).
This code works perfectly through the ssh tunnel if I remove the MongoDBUriOption "repliatSet" option, and only leave one node in the MongoDBHosts array.
Any idea what could I be doing wrong?
Thanks in advance, Marco.