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

Spring Boot连接MongoDB请求时报连接超时错误求助

Spring Boot连接MongoDB:启动正常但请求时连接超时被拒绝

我运行一个连接MongoDB的Spring Boot应用,启动时能正常连接数据库,但使用Insomnia发起请求时抛出连接超时错误。有趣的是,应用在Docker容器中运行完全正常。我已经尝试过更换驱动版本、检查防火墙、添加自动配置,但都无效。

错误日志

2023-05-24T10:46:37.023+02:00  INFO 21568 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-24T10:46:37.024+02:00  INFO 21568 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-24T10:46:37.025+02:00  INFO 21568 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2023-05-24T10:46:37.207+02:00  INFO 21568 --- [nio-8080-exec-1] org.mongodb.driver.cluster               : Cluster description not yet available. Waiting for 30000 ms before timing out
2023-05-24T10:47:07.222+02:00 ERROR 21568 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.3.80:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information}}]] with root cause

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.3.80:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information}}]

配置代码

MongoConfig类

@Configuration
public class MongoConfig {

    @Value("${mongo.host}")
    private String connectionString;

    @Value("${mongo.db}")
    private String db;

    @Value("${mongo.collection}")
    private String collection;

    @Bean
    public MongoCollection<Document> collection() {
        MongoClient mongoClient = create(connectionString);
        return mongoClient.getDatabase(db).getCollection(collection);
    }
}

application.yml配置文件

mongo:
  host: ${DB_URL:mongodb://"myIpAddress":27017}
  username:
  pass:
  db: db
  collection: musica
server.port: 8080
server:
  error:
    include-message: always
    include-binding-errors: always
    include-stacktrace: never
    include-exception: false

解决方案建议

  • 修正连接串格式:配置文件中mongo.host的默认值包含多余双引号,应改为mongodb://myIpAddress:27017。双引号会导致连接串解析错误,本地运行时使用了错误的地址,而Docker中可能通过DB_URL环境变量传入了正确的无引号地址,因此Docker运行正常。
  • 验证MongoDB监听地址:检查MongoDB配置文件(mongod.conf)中的net.bindIp参数,确保其设置为0.0.0.0(允许所有IP访问)或包含本地机器的IP。默认情况下MongoDB仅监听127.0.0.1,会导致本地应用无法远程连接。
  • 测试本地网络连通性:在本地终端执行mongosh mongodb://myIpAddress:27017,确认能否直接连接MongoDB服务,排除网络层面的问题。
  • 改用Spring Boot自动配置:移除自定义的MongoConfig,使用Spring Boot官方的MongoDB自动配置,配置参数改为spring.data.mongodb.uri、spring.data.mongodb.database等标准项,避免手动创建MongoClient时出现配置遗漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 18:05:41