如何通过Java驱动程序正确连接到Atlas M0(Free Tier)集群?

尝试使用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); 

在这种情况下,错误是:

 java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with 'mongodb://' at com.mongodb.ConnectionString.(ConnectionString.java:203) at com.mongodb.MongoClientURI.(MongoClientURI.java:176) at com.mongodb.MongoClientURI.(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) 

当程序以不使用+srv MongoDB 3.6或更高版本的代码段开头时:

 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.(ClusterBinding.java:75) at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(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文件中我有依赖:

   org.mongodb mongo-java-driver 3.6.0  

此外,当我启动mongo我的数据库被添加到此地址mongodb://127.0.0.1:27017 ,但我添加了群集的路径,而不是为此。 也许我需要写具体集群的路径或?

Ofc,我有admin-user。 另外,我可以通过Compass连接到我的集群和shell。 mongod进程已启动。 当我在IDE中运行时,此错误仅出现。 这可能是同样的问题。

有谁知道如何解决这个错误? 我感谢任何帮助。

这里似乎有一些问题

第一

3.6.0不是实际加载到应用程序类路径中的Mongo驱动程序库; 我怀疑你以前是用旧版本测试的,最近更新了POM? 您之前使用的是3.2.0版。

我怎么知道这个?

我开始挖掘代码, 在版本3.6.0中 ,您提供的错误消息远不及第203行。此外,您可以看到上面链接的代码支持+srv

回顾之前的版本,我终于在第203行发现了错误,然后发布了3.2.0版本 。

长话短说,尝试做Maven清理,并重建。

如果项目刷新没有帮助,请重新启动Eclipse以获取新的依赖项。

第二

MongoTimeoutException:等待服务器后30000毫秒后超时

这很可能是防火墙/访问控制组配置问题,因为防火墙阻止数据包到达您的Atlas集群。

请参阅向白名单添加地址 。

解决了! 那么,我做了什么:

1)我只尝试通过driver3.6 or later连接到层集群并编写mongodb+srv://user:@cluster0-ox90k.mongodb.net/test?retryWrites=true ,我总是收到错误: Connection strings must start with 'mongodb://'

2)好的,我删除了片段+srv并以同样的方式写了mongodb://user:@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 or earlier写了像mongodb://user:@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.6+驱动程序,你需要编写而不是格式连接(以避免我上面的问题)

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

这个变种(感谢Kevin Adistambha )

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

注意:密码需要写成不像mongodb://user:@... ,只是格式为mongodb://user:mypassword@...没有大括号<>

其他信息:如果您遇到java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients使用更新的驱动程序(例如3.7.1或其他)尝试更改编辑配置 。 希望它可以帮到某人。