Created
January 25, 2021 13:51
-
-
Save alissonfpmorais/3268eae0e9159419bfdcbf75e6ae3c28 to your computer and use it in GitHub Desktop.
Setup mongodb replica set with docker-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.1' | |
services: | |
mongo-setup: | |
container_name: mongo-setup | |
image: mongo | |
restart: on-failure | |
networks: | |
default: | |
volumes: | |
- ./scripts:/scripts | |
entrypoint: [ "/scripts/setup.sh" ] | |
depends_on: | |
- mongo1 | |
- mongo2 | |
- mongo3 | |
mongo1: | |
hostname: mongo1 | |
container_name: mongo1 | |
image: mongo | |
expose: | |
- 27017 | |
ports: | |
- 27017:27017 | |
networks: | |
default: | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false" ] | |
volumes: | |
- mongodata1:/data/db | |
- mongodatacfg1:/data/configdb | |
mongo2: | |
hostname: mongo2 | |
container_name: mongo2 | |
image: mongo | |
expose: | |
- 27018 | |
ports: | |
- 27018:27018 | |
networks: | |
default: | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27018" ] | |
volumes: | |
- mongodata2:/data/db | |
- mongodatacfg2:/data/configdb | |
mongo3: | |
hostname: mongo3 | |
container_name: mongo3 | |
image: mongo | |
expose: | |
- 27019 | |
ports: | |
- 27019:27019 | |
networks: | |
default: | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27019" ] | |
volumes: | |
- mongodata3:/data/db | |
- mongodatacfg3:/data/configdb | |
volumes: | |
mongodata1: | |
mongodatacfg1: | |
mongodata2: | |
mongodatacfg2: | |
mongodata3: | |
mongodatacfg3: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#MONGODB1=`ping -c 1 mongo1 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1` | |
#MONGODB2=`ping -c 1 mongo2 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1` | |
#MONGODB3=`ping -c 1 mongo3 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1` | |
MONGODB1=mongo1 | |
MONGODB2=mongo2 | |
MONGODB3=mongo3 | |
echo "**********************************************" ${MONGODB1} | |
echo "Waiting for startup.." | |
until curl http://${MONGODB1}:27017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do | |
echo '.' | |
sleep 1 | |
done | |
# echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1 | |
# echo "Started.." | |
echo SETUP.sh time now: `date +"%T" ` | |
mongo --host ${MONGODB1}:27017 <<EOF | |
var cfg = { | |
"_id": "rs0", | |
"protocolVersion": 1, | |
"version": 1, | |
"members": [ | |
{ | |
"_id": 0, | |
"host": "${MONGODB1}:27017", | |
"priority": 2 | |
}, | |
{ | |
"_id": 1, | |
"host": "${MONGODB2}:27018", | |
"priority": 0 | |
}, | |
{ | |
"_id": 2, | |
"host": "${MONGODB3}:27019", | |
"priority": 0 | |
} | |
],settings: {chainingAllowed: true} | |
}; | |
rs.initiate(cfg, { force: true }); | |
rs.reconfig(cfg, { force: true }); | |
rs.setSlaveOk(); | |
db.getMongo().setReadPref('nearest'); | |
db.getMongo().setSlaveOk(); | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alissonfpmorais: I am using this on macOS Big Sur. I am trying to connect to the replica set using MongoDB Compass but I am getting the following error:

Any possible workaround to solve this? If I don't supply the replica set rs0, I am able to connect to Primary database on 27017.