Héctor
Héctor

Reputation: 26084

Unable to connect to MongoDB Atlas using Vert.x MongoClient

I'm trying to connect to a MongoDB database hosted in MongoDB Atlas using Vert.x MongoClient:

I'm creating MongoClient with this code (Kotlin):

MongoClient.create(vertx, jsonObjectOf(
  "connection_string" to "mongodb+srv://<username>:<password>@<cluster_address>/my_db"
))

(with real values, obviously)

But I'm unable to perform any operation agains database. I get a connection timeout 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=REPLICA_SET, servers=[{address=<server1_address>, type=UNKNOWN, state=CONNECTING}, {address=<server2_address>, type=UNKNOWN, state=CONNECTING}, {address=<server3_address>, type=UNKNOWN, state=CONNECTING}]

If I try to connect using mongosh (CLI tool) and MongoDB Compass using the same connection string, it works fine.

I have tried confguring the client with parameters (hosts, ports, etc...) instead of using a connection string and I get this error when I make a query:

MongoSocketReadException: Prematurely reached end of stream

Upvotes: 1

Views: 487

Answers (1)

H&#233;ctor
H&#233;ctor

Reputation: 26084

After 11 days struggling with this, I have just solved it.

I have included "trustAll" : true option when creating MongoClient and it's working now. It's not the best solution, but anyway...

Upvotes: 2

Related Questions