我有一个链接到远程mongo服务器的ssh隧道。我试着在Play服务器中使用这个带有mongojava驱动程序的本地隧道。它抛出超时错误。但我可以使用mongo命令行连接到这个隧道并操作这个数据库,而不会出现问题。
这是隧道命令:
ssh -f -L 127.0.0.1:27018:$remote_ip:$remote_mongo_port $remote_user@$remote_host -N
这是我使用的mongo.url:
mongodb.default.uri="mongodb://acct:password@127.0.0.1:27018/mydb"
但是,它在尝试连接时抛出此错误。
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)
试用
ssh -N -f -L 27017:$remote_address:$remote_mongo_port $ssh_username@$ssh_address
尝试使用-R而不是-L,-L将本地端口转发到可以在您连接的服务器上读取的远程端口。-R另一方面将远程端口转发到本地端口,我猜您正在尝试这样做。
ssh-f-R 127.0.0.1:27018:$remote_ip:$remote_mongo_port$remote_user\@$remote_host-N