如何通过Java驱动程序正确连接到Atlas M0(免费套餐)集群? [英] How to connect to Atlas M0 (Free Tier) cluster correctly via Java driver?

查看:75
本文介绍了如何通过Java驱动程序正确连接到Atlas M0(免费套餐)集群?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 MongoDB 3.6版通过Java驱动程序连接Atlas集群.

所以,我的写法是:

 MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");
 MongoClient mongoClient = new MongoClient(uri);

在这种情况下,错误是:

In this case the error is:

java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with 'mongodb://'
    at com.mongodb.ConnectionString.<init>(ConnectionString.java:203)
    at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:176)
    at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:158)
    at project.Bot.check(Bot.java:30)
    at project.Bot.onUpdateReceived(Bot.java:104)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309)

当程序使用MongoDB 3.6版或更高版本(不带+srv)以片段开头时:

When the program starts with snippet using MongoDB version 3.6 or later without +srv:

MongoClientURI uri = new MongoClientURI("mongodb://admin1:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);

我遇到一个错误:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=cluster0.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: cluster0.mongodb.net}, caused by {java.net.UnknownHostException: cluster0.mongodb.net}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:201)
    at com.mongodb.operation.CountOperation.execute(CountOperation.java:206)
    at com.mongodb.operation.CountOperation.execute(CountOperation.java:53)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo$2.execute(Mongo.java:759)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:185)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:170)
    at project.Bot.check(Bot.java:36)
    at project.Bot.onUpdateReceived(Bot.java:103)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309)

在POM文件中,我具有依赖性:

In POM file I have dependency:

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.6.0</version>
        </dependency>

此外,当我启动mongo时,我的数据库已添加到该地址mongodb://127.0.0.1:27017,但是我为此添加了到集群的路径.也许我需要写具体集群的路径吗?

Also, when I'm starting mongo my database is added to this address mongodb://127.0.0.1:27017, but I added path to the cluster not for this. Maybe I need to write path to concrete cluster or?

Ofc,我有管理员用户.另外,我可以通过Compass连接到我的集群,也可以从Shell连接. mongod进程开始.仅当我在IDE中运行时,才会出现此错误.可能在此处.

Ofc, I have admin-user. In addition, I can connect via Compass to my cluster and from shell. mongod process is started. This error appears only, when I'm running in IDE. Same issue probably here.

有人知道如何解决此错误吗?感谢您的帮助.

Does anyone know how to solve this error? I appreciate any help.

推荐答案

解决了! 所以,我所做的:

Solved it! So, what I've done:

1)我只尝试通过 driver3.6 连接到层集群,并写了

1) I tried only to connect to tier cluster via driver3.6 and wrote

mongodb+srv://user:<PASSWORD>@cluster0-ox90k.mongodb.net/test?retryWrites=true

我总是收到错误消息:Connection strings must start with 'mongodb://'.

I always get an error: Connection strings must start with 'mongodb://'.

2)好吧,我删除了代码段+srv并以相同的方式编写

2) Okay, I deleted the snippet +srv and wrote the same way

mongodb://user:<PASSWORD>@cluster0-ox90k.mongodb.net/test?retryWrites=true

并再次得到错误:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=cluster0-ox90k.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: cluster0-ox90k.mongodb.net}, caused by {java.net.UnknownHostException: cluster0-ox90k.mongodb.net}}]

因此,我是通过 driver3.4或更早版本(如

mongodb://user:<PASSWORD>@cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

最后解决了.

已更新:如果要使用驱动程序3.7+,则需要编写而不是格式连接(并避免出现上述问题)

Updated: if you want to use drivers 3.7+, you need to write instead of format connection (and to avoid my issues above)

MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);

使用MongoClients.create()的另一个变体 (自3.7版本开始),并如

another variant using MongoClients.create() (as of the 3.7 release), and as mentioned here:

   MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");

注意:密码需要类似于mongodb://user:<mypassword>@...一样写,

Note: the password need to write not like mongodb://user:<mypassword>@...,

格式为mongodb://user:mypassword@...

不带括号<>.

这篇关于如何通过Java驱动程序正确连接到Atlas M0(免费套餐)集群?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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