You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala MongoDB驱动无法连接本地Docker部署的MongoDB副本集

MongoDB副本集Scala驱动连接被拒绝问题排查

我在本地Docker部署了MongoDB副本集,命令行和MongoDB Compass都能正常连接,但使用最新5.1.2版本的Scala MongoDB驱动连接时,出现Connection Refused错误,无法定位原因。

连接代码

val settings: MongoClientSettings = MongoClientSettings.builder()
    .applyConnectionString(new ConnectionString("mongodb://user:password@ac-mongo-node10:50004,ac-mongo-node20:50005,ac-mongo-node30:50006/ac_app?authSource=admin&replicaSet=ac-dev"))
    .applyToConnectionPoolSettings(t => t.applySettings(ConnectionPoolSettings.builder().maxSize(10).build()))
    .build()

val client = MongoClient(settings)

val db = client.getDatabase("ac_app")
val resourceCol: MongoCollection[Document] = db.getCollection("resources")

异常日志

19:00:12.123 [main] INFO org.mongodb.driver.cluster - Adding discovered server ac-mongo-node10:50004 to client view of cluster
19:00:12.162 [main] DEBUG org.mongodb.driver.connection - Connection pool created for ac-mongo-node10:50004 using options maxIdleTimeMS=0, minPoolSize=0, maxPoolSize=10, maxConnecting=2, waitQueueTimeoutMS=120000
19:00:12.169 [main] INFO org.mongodb.driver.cluster - Adding discovered server ac-mongo-node20:50005 to client view of cluster
19:00:12.173 [main] DEBUG org.mongodb.driver.connection - Connection pool created for ac-mongo-node20:50005 using options maxIdleTimeMS=0, minPoolSize=0, maxPoolSize=10, maxConnecting=2, waitQueueTimeoutMS=120000
19:00:12.173 [main] INFO org.mongodb.driver.cluster - Adding discovered server ac-mongo-node30:50006 to client view of cluster
19:00:12.176 [main] DEBUG org.mongodb.driver.connection - Connection pool created for ac-mongo-node30:50006 using options maxIdleTimeMS=0, minPoolSize=0, maxPoolSize=10, maxConnecting=2, waitQueueTimeoutMS=120000
19:00:12.177 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=ac-mongo-node30:50006, type=UNKNOWN, state=CONNECTING}, {address=ac-mongo-node20:50005, type=UNKNOWN, state=CONNECTING}, {address=ac-mongo-node10:50004, type=UNKNOWN, state=CONNECTING}]
19:00:12.215 [cluster-ClusterId{value='669675e4c50e87196d639075', description='null'}-ac-mongo-node10:50004] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server ac-mongo-node10:50004
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:124)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:128)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:278)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
    at sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:301)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252)
    ... 7 common frames omitted
19:00:12.219 [cluster-ClusterId{value='669675e4c50e87196d639075', description='null'}-ac-mongo-node10:50004] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=ac-mongo-node30:50006, type=UNKNOWN, state=CONNECTING}, {address=ac-mongo-node20:50005, type=UNKNOWN, state=CONNECTING}, {address=ac-mongo-node10:50004, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]

排查方向及解决办法

  • 主机名解析问题:
    检查Scala应用所在环境(本地主机或容器)的hosts文件,确认ac-mongo-node10等节点主机名已经映射到对应容器的宿主机IP。命令行和Compass能连接,可能是它们的hosts里有映射,但应用环境没有。
  • 副本集配置与端口映射不匹配:
    副本集初始化时,每个节点的地址必须是宿主机可访问的IP/主机名+映射后的端口,不能用容器内部地址。同时启动MongoDB容器时要设置bindIp=0.0.0.0允许外部连接,端口映射要正确(比如-p 50004:27017)。
  • 连接字符串参数调整:
    临时添加directConnection=true到连接字符串测试,若能连接则说明副本集发现机制有问题;如果没启用SSL,添加ssl=false避免驱动尝试SSL连接失败。
  • 网络与防火墙检查:
    确认Scala应用所在环境的防火墙没有阻止50004/50005/50006端口访问,Docker容器网络模式设置正确(bridge模式要做端口映射,host模式需确保端口未被占用)。

内容的提问来源于stack exchange,提问作者Shwetanka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 02:15:04