Recently, I came across a problem with mongodb replica set where I was not able to connect to replicaSet using the replicaSet url. Probably many of you have already overcome this problem. But for some reason, I could not find a solution that exactly worked for me. In the process, I managed to find a solution that worked for me. Documenting here if it helps anyone else. I was using docker to setup the cluster. The steps to setup a mongodb replicaSet are quite straight forward
- Setup 3 mongo servers. (primary, secondary and the arbiter). In this docker setup, 3 containers are running an instance of mongo server each.
command: mongod --replSet rs1 --port 27022 --bind_ip_all
command: mongod --replSet rs1 --port 27023 --bind_ip_all
command: mongod --replSet rs1 --port 27021 --bind_ip_all
rs.initiate with your replicaset config. I am running that through a bash script here. This script runs inside docker on the same mongo servers container network
echo "Waiting for startup.."
until mongosh --host…