Trying to connect Atlas cluster via Java driver using MongoDB version 3.6.
So, I'm writting like:
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)
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)
In POM file I have dependency:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.0</version>
</dependency>
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, 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.