Search code examples
mongodbreplicaset

How to connect MongoClient to secondary replica set?


I am trying to use the MongoClient GUI app to connect to a MongoDB replica set. This is the structure of my URL but it is not working:

mongodb://username:password@host-01:27017,host-02:27017,host-03:27017/mydb?authSource=mydb&replicaSet=rs0&ssl=true&sslValidate=false

I get an error similar to this:

Couldn't connect: server host-03:27017 received an error {"name":"MongoError","message":"read ECONNRESET"}

Solution

  • This was on a Mac. host-01, host-02 and host-03 were pointing to the wrong ip addresses in /etc/hosts

    The servers were responding to ping requests, but the wrong servers were responding. So make sure your servers are pointing to actual MongoDB nodes.