How to connect to Atlas M0 (Free Tier) cluster correctly via Java driver?
Solved it! So, what I've done:
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
I always get an error: Connection strings must start with 'mongodb://'
.
2) Okay, I deleted the snippet +srv
and wrote the same way
mongodb://user:<PASSWORD>@cluster0-ox90k.mongodb.net/test?retryWrites=true
and get again the error:
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}}]
So, I wrote via driver3.4 or earlier like
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
and finally it solved.
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:[email protected]/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);
another variant using MongoClients.create()
(as of the 3.7 release), and as mentioned here:
MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:[email protected]/test?retryWrites=true");
Note: the password need to write not like mongodb://user:<mypassword>@...
,
just in format mongodb://user:mypassword@...
without braces <>
.
There seem to be a few issues here
First
3.6.0 is not the Mongo driver library that was actually loaded into your application classpath; I suspect that you were previously testing with an old version, and recently updated the POM? You were previously using version 3.2.0.
How do I know this?
I started digging through the code, and at version 3.6.0, the error message you provided is nowhere near line 203. And also, you can see that the above linked code has support for the +srv
.
Browing back through previousl releases, I finally found that error massge on line 203, back at release 3.2.0.
Long story short, trying doing a Maven clean, and rebuild.
Relaunch Eclipse to pick up new dependencies if a project refresh does not help.
Second
MongoTimeoutException: Timed out after 30000 ms while waiting for a server
This one is highly likely a firewall / access control group configuration issue, in that the firewall is blocking the packets from reaching your Atlas cluster.
See adding addresses to the whitelist.
One more important note: in this string:
MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:[email protected]/test?retryWrites=true");
test ==> is a Db name, before making this connection, DB should exist.