如何将 Mongodb Atlas 连接到 Spring [英] How to connect Mongodb Atlas to Spring

查看:56
本文介绍了如何将 Mongodb Atlas 连接到 Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,它使用 Spring Boot 来处理后端逻辑.我正在尝试集成 mongodb 以跟踪有关此 web 应用程序用户的一些信息.我在 mongodb Atlas 上创建了一个数据库,并通过 Mongo Shell 连接正常.当我尝试与 Spring 连接时,问题就出现了.让我告诉你所有的细节

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

在 Atlas 中,我将此 IP 地址(0.0.0.0/0(包括您当前的 IP 地址))添加到安全">网络地址"中.理论上,这应该允许我从任何 IP 地址连接到数据库.

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.

然后我创建了一个名为test"的集合.

I then created a collection called "test".

如果我点击我的集群,然后点击连接按钮,它会询问我想要连接的模式.我选择连接您的应用程序",然后我必须选择驱动程序和版本.我分别选择Java"和3.6 或更高版本"(我不确定它是否是正确的版本,替代方案是 3.4 或 3.3).最后它向我显示了连接字符串:

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

要使用 Spring 连接到 Atlas,我正在使用此依赖项

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

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

在 application.properties 文件中,我有这两行来配置 mongo.

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.

唯一的问题是,当我启动 Spring Boot 时,我继续收到此错误消息:

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?

解决方案

正如@barrypicker 所建议的,问题出在属性文件中.我使用 spring.data.mongodb.uri 而不是使用 spring.data.mongodb.host.现在它完美运行.

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

即使没有 spring.data.mongodb.port

even without spring.data.mongodb.port

推荐答案

好吧,我认为您要连接到 Mongo Atlas,您的 application.properties 应该有 spring.data.mongodb.uri 而不是 spring.data.mongodb.host.spring.data.mongodb.uri: mongodb://:@:/我认为这可能有效`

Well, I think your to connect to Mongo Atlas your application.properties should have spring.data.mongodb.uri instead of spring.data.mongodb.host.spring.data.mongodb.uri: mongodb://<user>:<passwd>@<host>:<port>/<dbname> I think this may work `

这篇关于如何将 Mongodb Atlas 连接到 Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆