1

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:password@127.0.0.1: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)
angelokh
  • 9,426
  • 9
  • 69
  • 139
  • On my own experience I've learned that MongoDB operates poorly on connections other than LAN. It is very sensitive to drop of packets and over Internet connection is not sufficient. – Kirill Slatin Mar 02 '15 at 06:39
  • I think you are trying to connect to remote db and your url contain `127.0.0.1`(localhost) as host instead you should use `remote ip`.Correct me if I am wrong. – singhakash Mar 02 '15 at 12:51
  • @singhakash I use ssh tunnel to remote server because remote server only allows ssh port not mongo db port. SSH tunnel is used to forward a local port to remote port thru SSH so I can still connect to the db from my localhost. – angelokh Mar 03 '15 at 03:06

2 Answers2

3

try it

ssh -N -f -L 27017:$remote_address:$remote_mongo_port $ssh_username@$ssh_address
Sapp
  • 46
  • 2
0

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

krisfremen
  • 177
  • 1
  • 2
  • 13
  • -R doesn't work. Once I use -R, my mongo client on my MAC can't no longer connect to remote server. I haven't tried my application yet. Do you think I can use -R and -L so my mongo client and my local application can work at the same time. – angelokh Mar 02 '15 at 07:42