I already tried to find similar questions, but those do not seem to resolve my issue. My issue is very similar, but my configuration is already good as per this related issues:
Cannot initialize a mongodb replica set on Docker
MongoDB replica set initiate() failing on Docker
https://dev.to/mattdark/deploy-a-mongodb-cluster-with-docker-compose-4ieo
I want to set up locally a MongoDB cluster using Docker.
I have the following docker-compose.yaml
file:
version: '3.8'
services:
mongo_replica_1:
container_name: mongo_replica_1
hostname: mongo_replica_1
image: mongo:7.0.4
ports:
- 27017:27017
restart: always
entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_1', '--replSet', 'dbrs', '--dbpath', '/data/db']
volumes:
- ./.volumes/mongo/replica1:/data/db
- ./.volumes/mongo/replica1/configdb:/data/configdb
networks:
- dashboard_network
mongo_replica_2:
container_name: mongo_replica_2
hostname: mongo_replica_2
image: mongo:7.0.4
ports:
- 27018:27017
restart: always
entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_2', '--replSet', 'dbrs', '--dbpath', '/data/db']
volumes:
- ./.volumes/mongo/replica2:/data/db
- ./.volumes/mongo/replica2/configdb:/data/configdb
networks:
- dashboard_network
mongo_replica_3:
container_name: mongo_replica_3
hostname: mongo_replica_3
image: mongo:7.0.4
ports:
- 27019:27017
restart: always
entrypoint: ['mongod', '--bind_ip', 'localhost,mongo_replica_3', '--replSet', 'dbrs', '--dbpath', '/data/db']
volumes:
- ./.volumes/mongo/replica3:/data/db
- ./.volumes/mongo/replica3/configdb:/data/configdb
networks:
- dashboard_network
mongo_launcher:
container_name: mongo_launcher
image: mongo:7.0.4
restart: on-failure
depends_on:
- mongo_replica_1
- mongo_replica_2
- mongo_replica_3
networks:
- dashboard_network
volumes:
- ./scripts/mongo-setup.sh:/scripts/mongo-setup.sh
entrypoint: ['sh', '/scripts/mongo-setup.sh']
networks:
dashboard_network:
driver: bridge
This is my mongo-setup.sh
file:
#!/bin/bash
MONGODB_REPLICA_1=mongo_replica_1
MONGODB_REPLICA_2=mongo_replica_2
MONGODB_REPLICA_3=mongo_replica_3
echo "************ [ Waiting for startup ] **************" ${MONGODB_REPLICA_1}
until mongosh --host ${MONGODB_REPLICA_1}:27017 --eval "printjson(db.serverStatus())"; do
printf '.'
sleep 1
done
echo "************ [ Startup completed ] **************" ${MONGODB_REPLICA_1}
mongosh --host ${MONGODB_REPLICA_1}:27017 <<EOF
var configuration = {
"_id": "dbrs",
"protocolVersion": 1,
"version": 1,
"members": [
{
"_id": 1,
"host": "${MONGODB_REPLICA_1}:27017",
"priority": 3
},
{
"_id": 2,
"host": "${MONGODB_REPLICA_2}:27018",
"priority": 2
},
{
"_id": 3,
"host": "${MONGODB_REPLICA_3}:27019",
"priority": 1
}
],
"settings": {
"chainingAllowed": true
}
};
rs.initiate(configuration);
rs.secondaryOk();
db.getMongo().setReadPref('nearest');
EOF
Then I try to connect to the DB with string mongodb://localhost:27017
but it fails: connect ECONNREFUSED 127.0.0.1:27017, connect ECONNREFUSED ::1:27017
So I tried to inspect my mongo_launcher
container:
MongoServerError: replSetInitiate quorum check failed because not all proposed set members responded affirmatively: mongo_replica_2:27018 failed with Error connecting to mongo_replica_2:27018 (172.24.0.2:27018) :: caused by :: onInvoke :: caused by :: Connection refused, mongo_replica_3:27019 failed with Error connecting to mongo_replica_3:27019 (172.24.0.3:27019) :: caused by :: onInvoke :: caused by :: Connection refused
test> DeprecationWarning: .setSecondaryOk() is deprecated. Use .setReadPref("primaryPreferred") instead
Setting read preference from "primary" to "primaryPreferred"
And inspected one of the replica set container:
{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}}