Last active
February 17, 2024 10:30
-
-
Save ftmoose/2b4481d99669994e0767af3e79b3e869 to your computer and use it in GitHub Desktop.
Revisions
-
ftmoose revised this gist
Jun 8, 2021 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,30 +18,30 @@ until curl http://${MONGODB1}:${MONGODB1_PORT}/serverStatus\?text\=1 2>&1 | grep sleep 1 done echo setupReplicaSet.sh time now: `date +"%T" ` mongo --host ${MONGODB1}:${MONGODB1_PORT} <<EOF var cfg = { "_id": "${REPLICA_SET_NAME}", "members": [ { "_id": 0, "host": "${MONGODB1}:${MONGODB1_PORT}", "priority": 2 }, { "_id": 1, "host": "${MONGODB2}:${MONGODB2_PORT}", "priority": 0 }, { "_id": 2, "host": "${MONGODB3}:${MONGODB3_PORT}", "priority": 0 } ] }; rs.initiate(cfg); rs.reconfig(cfg, {force: true}); rs.secondaryOk(); rs.conf(); EOF -
ftmoose revised this gist
Jun 8, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,7 @@ Add the following to your `/etc/hosts` file in order to connect to the replica s ## Start up ```bash $ sudo chmod 755 ./scripts/setupReplicaSet.sh $ sudo docker-compose.yml up -d ``` -
ftmoose revised this gist
Jun 8, 2021 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,8 @@ services: container_name: mongo-setup image: mongo restart: on-failure networks: default: volumes: - ./scripts:/scripts # Ensure the './scripts' directory exists with the 'setupReplicaSet.sh' file entrypoint: ["/scripts/setupReplicaSet.sh"] # Call the 'setupReplicaSet.sh' script on container init -
ftmoose revised this gist
Jun 8, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ -
ftmoose revised this gist
Jun 8, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ .keep -
ftmoose renamed this gist
Jun 8, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ftmoose renamed this gist
Jun 8, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ftmoose renamed this gist
Jun 8, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ftmoose created this gist
Jun 8, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ ## Directory Structure ``` app-root -> (src) -> docker-compose.yml -> scripts -> setupReplicaSet.sh ``` ## Map Hostnames Add the following to your `/etc/hosts` file in order to connect to the replica set locally ``` 127.0.0.1 mongo1 127.0.0.1 mongo2 127.0.0.1 mongo3 ``` ## Start up ```bash $ sudo docker-compose.yml up -d ``` ## Connection URI Based on the hostnames and ports in the `docker-compose.yml` and `setupReplicaSet.sh` files ``` mongodb://mongo1:27017,mongo2:27018,mongo3:27019/<DB-NAME-HERE>?replicaSet=rs0 ``` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ version: "3.1" # Our containers services: # Temporary container used to initialize the replica set mongo-setup: container_name: mongo-setup image: mongo restart: on-failure networks: default volumes: - ./scripts:/scripts # Ensure the './scripts' directory exists with the 'setupReplicaSet.sh' file entrypoint: ["/scripts/setupReplicaSet.sh"] # Call the 'setupReplicaSet.sh' script on container init depends_on: # Wait for the 3 DB containers to spin up first - mongo1 - mongo2 - mongo3 mongo1: hostname: mongo1 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file container_name: mongo1 image: mongo expose: - 27017 ports: - 27017:27017 networks: default: restart: always volumes: - ./mongo-volumes/mongo1/data/db:/data/db # Create a volume to persist data entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27017"] mongo2: hostname: mongo2 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file container_name: mongo2 image: mongo expose: - 27018 ports: - 27018:27018 networks: default: restart: always volumes: - ./mongo-volumes/mongo2/data/db:/data/db # Create a volume to persist data entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27018"] mongo3: hostname: mongo3 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file container_name: mongo3 image: mongo expose: - 27019 ports: - 27019:27019 networks: default: restart: always volumes: - ./mongo-volumes/mongo3/data/db:/data/db # Create a volume to persist data entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27019"] This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ #!/bin/bash REPLICA_SET_NAME=rs0 MONGODB1=mongo1 MONGODB1_PORT=27017 MONGODB2=mongo2 MONGODB2_PORT=27018 MONGODB3=mongo3 MONGODB3_PORT=27019 echo "********** setupReplicaSet.sh **********" echo ${MONGODB1} until curl http://${MONGODB1}:${MONGODB1_PORT}/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do printf '.' sleep 1 done echo Running setupReplicaSet.sh now: `date +"%T" ` mongo --host ${MONGODB1}:${MONGODB1_PORT} <<EOF var config = { "_id": "${REPLICA_SET_NAME}", "members": [ { "_id": 0, "host": "${MONGODB1}:${MONGODB1_PORT}" "priority": 2 }, { "_id": 1, "host": "${MONGODB2}:${MONGODB2_PORT}" "priority": 0 }, { "_id": 2, "host": "${MONGODB3}:${MONGODB3_PORT}" "priority": 0 }, ] }; rs.initiate(config); rs.reconfig(config, {force: true}); rs.secondaryOk(); rs.conf(); EOF