Reputation: 711
I'm trying to connect to MongoDB with MongoDB Compass 1.20.4
My connection string is:
mongodb://localhost:27017/?replicaSet=rs0
Here is my MongoDB docker setup:
version: '3'
services:
mongo0:
hostname: mongo0
container_name: mongo0
image: mongo
ports:
- 27017:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo1:
hostname: mongo1
container_name: mongo1
image: mongo
ports:
- 27018:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongosetup:
hostname: mongosetup
container_name: mongosetup
image: mongo
depends_on:
- mongo0
- mongo1
volumes:
- ./scripts/:/scripts
restart: "no"
entrypoint: [ "bash", "/scripts/mongo-setup.sh" ]
mongo-setup.sh
#!/bin/bash
sleep 10
mongo --host mongo0:27017 <<EOF
var config={"_id":"rs0","members":[{"_id":0,"host":"mongo0:27017"},{"_id":1,"host":"mongo1:27017"}]};
rs.initiate(config);
EOF
Here is what I see in my docker logs:
mongo0 | 2020-02-02T14:42:35.114+0000 I NETWORK [listener] connection accepted from 172.27.0.1:55494 #31 (4 connections now open)
mongo0 | 2020-02-02T14:42:35.115+0000 I NETWORK [conn31] received client metadata from 172.27.0.1:55494 conn31: { driver: { name: "nodejs", version: "3.4.0" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "19.3.0" }, platform: "'Node.js v10.2.0, LE (unified)", application: { name: "MongoDB Compass" } }
mongo0 | 2020-02-02T14:42:35.120+0000 I NETWORK [conn31] end connection 172.27.0.1:55494 (3 connections now open)
Prior to using replica set it connected just fine, also Studio 3T works perfectly with this setup. Would appreciate any help
Upvotes: 0
Views: 880
Reputation: 711
Read Preference
in MongoDB Compass to Primary Preferred
fixed the connection issue. I tweaked my mongo-setup.sh
to always make set at port 27017 PRIMARY (set priority option):
#!/bin/bash
# https://docs.mongodb.com/manual/tutorial/force-member-to-be-primary/
sleep 10
mongo --host mongo0:27017 <<EOF
var config={"_id":"rs0","members":[{"_id":0,"host":"mongo0:27017","priority":1},{"_id":1,"host":"mongo1:27017", "priority":0.5}]};
rs.initiate(config);
EOF
echo 'Finished mongo setup'
Links for reference:
Replica Set Elections
Force a Member to Become Primary
Upvotes: 1