I am trying hard to connect to my MongoDB Replica Set but nothing seems to work.
I have deployed Replica set with 3 node, each on a separate AWS Availability Zone but in the same Region.
Below is the code I am using to connect to Replica Set.
mongoURL := "mongodb://myuser:password@<publicIP>:27017,<publicIP>:27017,<publicIP>:27017/?replicaSet=replica01&authSource=admin"
clientOptions := options.Client().ApplyURI(mongoURL)
clientOptions = clientOptions.SetMaxPoolSize(100) //100 is default driver setting
log.Println("Connection String: " + clientOptions.GetURI())
client, err := mongo.Connect(ctx, clientOptions)
I have also created DNS mapping and used that also, but that's also not working.
Below is the connection URL
mongodb://adminuser:[email protected]:27017,rs2.domain.com:27017,rs3.domain.com:27017/?replicaSet=replica01&authSource=admin
Robo3T
When I am trying to connect Replica Set with Robo3T using public IP I get the following error
Cannot connect to replica set "IP-RS"[public-ip:27017]. Set's primary is unreachable.
Reason: No member of the set is reachable. Reason: Connect failed
When I use sub-domain name to connect to replica set, I get the following error
Cannot connect to replica set "SRV Replica"[rs1.domain.com:27017]. Set's primary is unreachable.
Reason: Failed to initialize MongoWorker. Reason: connect failed
Here is the solution.
The problem was the hostname. I used the hostname while configuring the replica set.
Here is my configuration
Solution
I changed the
hostname
withprivate-ip
.Please note that, since my 3 replica nodes resides in the same AWS region but in different availability zones, so I used
private ip
to make connection between nodes.MongoDB conf setting on all 3 nodes
Hostentry on all the 3 nodes
Your each machine's name should match with the name defined in
/etc/hosts
fileReplica Status
Go Connection String