Skip to content

Instantly share code, notes, and snippets.

@ftmoose
Last active February 17, 2024 10:30
Show Gist options
  • Save ftmoose/2b4481d99669994e0767af3e79b3e869 to your computer and use it in GitHub Desktop.
Save ftmoose/2b4481d99669994e0767af3e79b3e869 to your computer and use it in GitHub Desktop.

Revisions

  1. ftmoose revised this gist Jun 8, 2021. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions setupReplicaSet.sh
    Original 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 Running setupReplicaSet.sh now: `date +"%T" `
    echo setupReplicaSet.sh time now: `date +"%T" `
    mongo --host ${MONGODB1}:${MONGODB1_PORT} <<EOF
    var config = {
    var cfg = {
    "_id": "${REPLICA_SET_NAME}",
    "members": [
    {
    "_id": 0,
    "host": "${MONGODB1}:${MONGODB1_PORT}"
    "host": "${MONGODB1}:${MONGODB1_PORT}",
    "priority": 2
    },
    {
    "_id": 1,
    "host": "${MONGODB2}:${MONGODB2_PORT}"
    "host": "${MONGODB2}:${MONGODB2_PORT}",
    "priority": 0
    },
    {
    "_id": 2,
    "host": "${MONGODB3}:${MONGODB3_PORT}"
    "host": "${MONGODB3}:${MONGODB3_PORT}",
    "priority": 0
    },
    }
    ]
    };
    rs.initiate(config);
    rs.reconfig(config, {force: true});
    rs.initiate(cfg);
    rs.reconfig(cfg, {force: true});
    rs.secondaryOk();
    rs.conf();
    EOF
  2. ftmoose revised this gist Jun 8, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original 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
    ```

  3. ftmoose revised this gist Jun 8, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion docker-compose.yml
    Original 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
    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
  4. ftmoose revised this gist Jun 8, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion MONGODB REPLICA SET DOCKER CONFIGURATION
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    .keep
  5. ftmoose revised this gist Jun 8, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions MONGODB REPLICA SET DOCKER CONFIGURATION
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    .keep
  6. ftmoose renamed this gist Jun 8, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. ftmoose renamed this gist Jun 8, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. ftmoose renamed this gist Jun 8, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. ftmoose created this gist Jun 8, 2021.
    27 changes: 27 additions & 0 deletions README.md
    Original 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
    ```
    63 changes: 63 additions & 0 deletions docker-compose.yml
    Original 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"]
    47 changes: 47 additions & 0 deletions setupReplicaSet.sh
    Original 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