我有一个链接到远程mongo服务器的ssh隧道。我尝试在Play服务器中使用带有mongo-java-driver的本地隧道。它会抛出超时错误。但我可以使用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)
答案 0 :(得分:2)
试试吧
ssh -N -f -L 27017:$remote_address:$remote_mongo_port $ssh_username@$ssh_address
答案 1 :(得分:0)
尝试使用-R而不是-L,-L使本地端口转发到远程端口,该端口可以在您要连接的服务器上读取。另一方面,-R将远程端口转发到本地端口,这正是我猜你要做的。
ssh -f -R 127.0.0.1:27018:$remote_ip:$remote_mongo_port $ remote_user \ @ $ remote_host -N