首页 > 解决方案 > com.mongodb.MongoSocketException: No such host is known

问题描述

I am trying to connect to MongoDB Atlas DB from my spring boot app. I set the below listed properties in my application properties file. When I try to connect I see this exception "com.mongodb.MongoSocketException: No such host is known". However I am able to connect to this using mongodb compass from the same machine. Am I missing something?

spring.data.mongodb.authentication-database=
spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.username=
spring.data.mongodb.password=
spring.data.mongodb.database=

I am also able to connect when I use the below -

spring.data.mongodb.uri=

标签: javaspringmongodbspring-bootspring-data-mongodb

解决方案


Take a look at this: https://developer.mongodb.com/article/srv-connection-strings/

Atlas is giving you a connection string with the mongodb+srv syntax where the "host" part is actually a DNS SRV record that gives you a replica set, not a host. This is compatible with the spring.data.mongodb.uri parameter and of course with MongoDb compass.

However setting the spring.data.mongodb.host instead gives you the unknown host exception.


推荐阅读