angelokh
angelokh

Reputation: 9428

Local application connects Mongo through ssh tunnel is not working

I have a ssh tunnel that linked to a remote mongo server. I tried to use this local tunnel with mongo-java-driver in Play server. It throws time-out error. But I can use mongo command line to connect to this tunnel and operate this database without issues.

This is the tunnel command:

ssh -f -L 127.0.0.1:27018:$remote_ip:$remote_mongo_port $remote_user\@$remote_host -N

This is the mongo.url I used:

mongodb.default.uri="mongodb://acct:[email protected]:27018/mydb"

However, it throws this error while trying to connect.

Caused by: com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=primaryPreferred}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]} after 9999 ms
    at com.mongodb.BaseCluster.getServer(BaseCluster.java:87)
    at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654)
    at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39)
    at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503)
    at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451)
    at com.mongodb.DBTCPConnector.authenticate(DBTCPConnector.java:624)
    at com.mongodb.DBApiLayer.doAuthenticate(DBApiLayer.java:195)
    at com.mongodb.DB.authenticateCommandHelper(DB.java:763)
    at com.mongodb.DB.authenticate(DB.java:719)
    at com.mongodb.casbah.MongoDB.authenticate(MongoDB.scala:86)
    at se.radley.plugin.salat.SalatPlugin$MongoSource$$anonfun$3$$anonfun$apply$1.apply(SalatPlugin.scala:33)
    at se.radley.plugin.salat.SalatPlugin$MongoSource$$anonfun$3$$anonfun$apply$1.apply(SalatPlugin.scala:32)
    at scala.Option.map(Option.scala:145)
    at se.radley.plugin.salat.SalatPlugin$MongoSource$$anonfun$3.apply(SalatPlugin.scala:32)
    at se.radley.plugin.salat.SalatPlugin$MongoSource$$anonfun$3.apply(SalatPlugin.scala:31)
    at scala.Option.flatMap(Option.scala:170)
    at se.radley.plugin.salat.SalatPlugin$MongoSource.connection(SalatPlugin.scala:31)
    at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:136)

Upvotes: 1

Views: 2353

Answers (2)

Sapp
Sapp

Reputation: 46

try it

ssh -N -f -L 27017:$remote_address:$remote_mongo_port $ssh_username@$ssh_address

Upvotes: 3

krisfremen
krisfremen

Reputation: 179

Try using -R instead of -L, -L makes a local port forward to a remote port which can be read on the server which you're connecting to. -R on the other hand forwards a remote port to a local port, which is what I'm guessing you're trying to do.

ssh -f -R 127.0.0.1:27018:$remote_ip:$remote_mongo_port $remote_user\@$remote_host -N

Upvotes: 0

Related Questions