简体   繁体   中英

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=

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM