[JAVA-2211] Connect to ReplicaSet through SSH tunneling Created: 08/Jun/16  Updated: 11/Sep/19  Resolved: 20/Sep/16

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Enrico Candino [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I've a remote ReplicaSet and I'm trying to connect through an SSH tunnel, but after the discovery of the cluster, since the localhost is not mapped, it's going to remove itself and fail the connection to the server.

Should I map something in the hosts file?

Some logs that will probably explain the situation:

INFO org.mongodb.driver.connection - Opened connection [connectionId

{localValue:1, serverValue:606}

] to localhost:28888
INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:28888, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion

{versionList=[3, 2, 3]}

, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=30898970, setName='datalyticsReplSet', canonicalAddress=mongo1.example.com:27017, hosts=[mongo1.example.com:27017, mongo3.example.com:27017, mongo2.example.com:27017], passives=[], arbiters=[], primary='mongo1.example.com:27017', tagSet=TagSet

{[]}

, electionId=7fffffff000000000000000f, setVersion=8}
INFO org.mongodb.driver.cluster - Discovered cluster type of REPLICA_SET
INFO org.mongodb.driver.cluster - Adding discovered server mongo1.example.com:27017 to client view of cluster
INFO org.mongodb.driver.cluster - Adding discovered server mongo3.example.com:27017 to client view of cluster
INFO org.mongodb.driver.cluster - Adding discovered server mongo2.example.com:27017 to client view of cluster
INFO org.mongodb.driver.cluster - Server localhost:28888 is no longer a member of the replica set. Removing from client view of cluster.
INFO org.mongodb.driver.cluster - Canonical address mongo1.example.com:27017 does not match server address. Removing localhost:28888 from client view of cluster



 Comments   
Comment by Jeffrey Yemin [ 08/Jun/16 ]

Hi Enrico,

I've never tried this, but you might be able to do this if you put each replica set member on a different port, e.g., in your replica set configuration, use:

mongo1.example.com:27017
mongo2.example.com:27018
mongo3.example.com:20719

and in /etc/hosts:

mongo1.example.com 127.0.0.1
mongo2.example.com 127.0.0.1
mongo3.example.com 127.0.0.1

and then set up port forwarding to mongo1.example.com:27017 on 127.0.0.1:27017, mongo2.example.com:27018 on 127.0.0.1:27018, and mongo3.example.com:27019 on 127.0.0.1:27019.

Also, if you are willing to connect directly to a single known member of the set, you can do this by using one of the MongoClient constructors that takes a single ServerAddress instead of a List<ServerAddress>. These constructors make direct connections to servers and don't attempt any discovery of the other replica set members, which is what's getting you into trouble.

Another option, of course, is to set up a VPN, but I assume that's not an option for you.

Generated at Thu Feb 08 08:56:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.