How to connect Mongodb Atlas to Spring

感情迁移 提交于 2020-11-29 09:15:55

问题


I have a web application which is using Spring Boot to handle the backend logics. I'm trying to integrate mongodb to track some information about the users of this webapp. I created a database on mongodb Atlas and through the Mongo Shell the connection goes fine. The problem comes when I try to connect with Spring. Let me show you all the details

Inside Atlas, I added this IP Address (0.0.0.0/0 (includes your current IP address)) into Security > Network Address. In theory this should allow me to connect to the database from any IP address.

I then created a collection called "test".

If I click on my cluster and then on the connect button, it ask me with which modality I want to connect. I choose "Connect your application", and then I have to select the Driver and the Version. I choose respectively "Java" and "3.6 or later" (I'm not sure if it's the correct version, the alternatives are 3.4 or 3.3). And finally it shows me the connection string which is:

mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retryWrites=true&w=majority

To connect to Atlas with Spring I'm using this dependency

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
</dependency>

Inside the application.properties file I have these two lines to configure mongo.

spring.data.mongodb.host=mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retryWrites=true&w=majority
spring.data.mongodb.port=27017

Instead of the password I put for obvious reasons.

The only problem is that when I start Spring Boot I continue to receive this error message:

2020-02-25 16:31:25.605  INFO 41162 --- [=majority:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retrywrites=true&w=majority:27017

com.mongodb.MongoSocketException: mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retrywrites=true&w=majority: nodename nor servname provided, or not known
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.6.4.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongo-java-driver-3.6.4.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: java.net.UnknownHostException: mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retrywrites=true&w=majority: nodename nor servname provided, or not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_111]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_111]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_111]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_111]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_111]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_111]
    at java.net.InetAddress.getByName(InetAddress.java:1076) ~[na:1.8.0_111]
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongo-java-driver-3.6.4.jar:na]
    ... 5 common frames omitted

I don't know what to do in order to make it work. Am I missing something?

SOLUTION

As @barrypicker suggested, the problem was inside the properties file. Instead of using spring.data.mongodb.host I used spring.data.mongodb.uri. Now it works perfectly.

spring.data.mongodb.uri=mongodb+srv://admin:<password>@umadit-obxpb.mongodb.net/test?retryWrites=true&w=majority

even without spring.data.mongodb.port

来源:https://stackoverflow.com/questions/60398285/how-to-connect-mongodb-atlas-to-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!