Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bachloxo/689492a6da52556f8c6be8d0f8f720b1 to your computer and use it in GitHub Desktop.
Save bachloxo/689492a6da52556f8c6be8d0f8f720b1 to your computer and use it in GitHub Desktop.

Revisions

  1. bachloxo revised this gist Apr 12, 2021. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions a-mongodb-replica-set-docker-compose-readme.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    Ourcase:
    - use setup2.sh
    - use setup.sh
    - make sure that network are same

    Origin:
    @@ -10,6 +10,4 @@ This will guide you through setting up a replica set in a docker environment usi
    - Mongoose
    - Mongoose Transactions

    Thanks to https://gist.github.com/asoorm for helping with their docker-compose file!

    Url connect mongodb: mongodb://127.0.0.1:27017/esc-api?replicaSet=rs0
  2. bachloxo revised this gist Apr 12, 2021. 4 changed files with 12 additions and 61 deletions.
    4 changes: 3 additions & 1 deletion a-mongodb-replica-set-docker-compose-readme.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,6 @@ This will guide you through setting up a replica set in a docker environment usi
    - Mongoose
    - Mongoose Transactions

    Thanks to https://gist.github.com/asoorm for helping with their docker-compose file!
    Thanks to https://gist.github.com/asoorm for helping with their docker-compose file!

    Url connect mongodb: mongodb://127.0.0.1:27017/esc-api?replicaSet=rs0
    21 changes: 6 additions & 15 deletions setup.sh
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@
    #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 "${MONGODB2}"
    echo "${MONGODB3}"

    echo "**********************************************" ${MONGODB1}
    echo "Waiting for startup.."
    @@ -22,30 +22,21 @@ done
    echo SETUP.sh time now: `date +"%T" `
    mongo --host ${MONGODB1}:27017 <<EOF
    var cfg = {
    "_id": "rs0",
    "protocolVersion": 1,
    "version": 1,
    "_id": "mongo-set",
    "members": [
    {
    "_id": 0,
    "host": "${MONGODB1}:27017",
    "priority": 2
    },
    {
    "_id": 1,
    "host": "${MONGODB2}:27017",
    "priority": 0
    },
    {
    "_id": 2,
    "host": "${MONGODB3}:27017",
    "priority": 0
    }
    ],settings: {chainingAllowed: true}
    };
    rs.initiate(cfg, { force: true });
    rs.reconfig(cfg, { force: true });
    rs.slaveOk();
    db.getMongo().setReadPref('nearest');
    db.getMongo().setSlaveOk();
    EOF
    rs.initiate(cfg);
    EOF
    42 changes: 0 additions & 42 deletions setup2.sh
    Original file line number Diff line number Diff line change
    @@ -1,42 +0,0 @@
    #!/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`

    echo "${MONGODB1}"
    echo "${MONGODB2}"
    echo "${MONGODB3}"

    echo "**********************************************" ${MONGODB1}
    echo "Waiting for startup.."
    until curl http://${MONGODB1}:27017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
    printf '.'
    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": "mongo-set",
    "members": [
    {
    "_id": 0,
    "host": "${MONGODB1}:27017",
    },
    {
    "_id": 1,
    "host": "${MONGODB2}:27017",
    },
    {
    "_id": 2,
    "host": "${MONGODB3}:27017",
    }
    ],settings: {chainingAllowed: true}
    };
    rs.initiate(cfg);
    EOF
    6 changes: 3 additions & 3 deletions transaction.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    async function transaction() {
    // Start the transaction.
    const session = await ModelA.startSession();
    const session = await connector.client.startSession();
    session.startTransaction();

    try {
    const options = { session };

    // Try and perform operation on Model.
    const a = await ModelA.create([{ ...args }], options);
    const a = await connector.collection('ModelA').insertOne({ ...args }, options);

    // If the first operation succeeds this next one will get called.
    await ModelB.create([{ ...args }], options);
    await connector.collection('ModelB').insertOne({ ...args }, options);

    // If all succeeded with no errors, commit and end the session.
    await session.commitTransaction();
  3. @DuongHieuMAI DuongHieuMAI revised this gist Jun 8, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions a-mongodb-replica-set-docker-compose-readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    Ourcase:
    - use setup2.sh
    - make sure that network are same

    Origin:

    This will guide you through setting up a replica set in a docker environment using.
    - Docker Compose
    - MongoDB Replica Sets
  4. @DuongHieuMAI DuongHieuMAI revised this gist Jun 8, 2020. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions setup2.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/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`

    echo "${MONGODB1}"
    echo "${MONGODB2}"
    echo "${MONGODB3}"

    echo "**********************************************" ${MONGODB1}
    echo "Waiting for startup.."
    until curl http://${MONGODB1}:27017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
    printf '.'
    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": "mongo-set",
    "members": [
    {
    "_id": 0,
    "host": "${MONGODB1}:27017",
    },
    {
    "_id": 1,
    "host": "${MONGODB2}:27017",
    },
    {
    "_id": 2,
    "host": "${MONGODB3}:27017",
    }
    ],settings: {chainingAllowed: true}
    };
    rs.initiate(cfg);
    EOF
  5. @harveyconnor harveyconnor revised this gist May 19, 2020. 2 changed files with 4 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    default:
    volumes:
    - ./scripts:/scripts
    entrypoint: [ "/scripts/setup.sh" ]
    entrypoint: [ "/scripts/setup.sh" ] # Make sure this file exists (see below for the setup.sh)
    depends_on:
    - mongo1
    - mongo2
    3 changes: 3 additions & 0 deletions docker.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # NOTE: This is the simplest way of achieving a replicaset in mongodb with Docker.
    # However if you would like a more automated approach, please see the setup.sh file and the docker-compose file which includes this startup script.

    # run this after setting up the docker-compose This will instantiate the replica set.
    # The id and hostname's can be tailored to your liking, however they MUST match the docker-compose file above.
    docker-compose up -d
  6. @harveyconnor harveyconnor revised this gist May 19, 2020. 2 changed files with 76 additions and 4 deletions.
    29 changes: 25 additions & 4 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    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: localmongo1
    @@ -7,9 +21,10 @@
    ports:
    - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false" ]
    volumes:
    - <VOLUME-DIR>:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
    - <VOLUME-DIR>/mongo/data1/db:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
    - <VOLUME-DIR>/mongo/data1/configdb:/data/configdb
    mongo2:
    hostname: mongo2
    container_name: localmongo2
    @@ -19,7 +34,10 @@
    ports:
    - 27018:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false" ]
    volumes:
    - <VOLUME-DIR>/mongo/data2/db:/data/db # Note the data2, it must be different to the original set.
    - <VOLUME-DIR>/mongo/data2/configdb:/data/configdb
    mongo3:
    hostname: mongo3
    container_name: localmongo3
    @@ -29,4 +47,7 @@
    ports:
    - 27019:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false" ]
    volumes:
    - <VOLUME-DIR>/mongo/data3/db:/data/db
    - <VOLUME-DIR>/mongo/data3/configdb:/data/configdb
    51 changes: 51 additions & 0 deletions setup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/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
    printf '.'
    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}:27017",
    "priority": 0
    },
    {
    "_id": 2,
    "host": "${MONGODB3}:27017",
    "priority": 0
    }
    ],settings: {chainingAllowed: true}
    };
    rs.initiate(cfg, { force: true });
    rs.reconfig(cfg, { force: true });
    rs.slaveOk();
    db.getMongo().setReadPref('nearest');
    db.getMongo().setSlaveOk();
    EOF
  7. @harveyconnor harveyconnor revised this gist Aug 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ rs.initiate(
    members: [
    { _id : 0, host : "mongo1:27017" },
    { _id : 1, host : "mongo2:27017" },
    { _id : 2, host : "mongo3:27017" }
    { _id : 2, host : "mongo3:27017", arbiterOnly: true }
    ]
    }
    )
  8. @harveyconnor harveyconnor revised this gist Jul 5, 2019. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    expose:
    - 27017
    ports:
    - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
    - <VOLUME-DIR>:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
    mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    expose:
    - 27017
    ports:
    - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
    - <VOLUME-DIR>:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
    mongo2:
    hostname: mongo2
    container_name: localmongo2
  9. @harveyconnor harveyconnor renamed this gist Jul 5, 2019. 1 changed file with 0 additions and 0 deletions.
  10. @harveyconnor harveyconnor revised this gist Jul 5, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion credits.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@ This will guide you through setting up a replica set in a docker environment usi
    - Mongoose
    - Mongoose Transactions

    Thanks to https://gist.github.com/asoorm for helping with his docker-compose file!
    Thanks to https://gist.github.com/asoorm for helping with their docker-compose file!
  11. @harveyconnor harveyconnor revised this gist Jul 5, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions credits.md
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,5 @@ This will guide you through setting up a replica set in a docker environment usi
    - MongoDB Replica Sets
    - Mongoose
    - Mongoose Transactions

    Thanks to https://gist.github.com/asoorm for helping with his docker-compose file!
  12. @harveyconnor harveyconnor revised this gist Jul 5, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions credits.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,6 @@
    This will guide you through setting up a replica set in a docker environment using.
    - Docker Compose
    - MongoDB Replica Sets
    - Mongoose
    - Mongoose Transactions
    Thanks to https://gist.github.com/asoorm for helping with his docker-compose file!
  13. @harveyconnor harveyconnor created this gist Jul 5, 2019.
    1 change: 1 addition & 0 deletions credits.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Thanks to https://gist.github.com/asoorm for helping with his docker-compose file!
    32 changes: 32 additions & 0 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    expose:
    - 27017
    ports:
    - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
    - <VOLUME-DIR>:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
    mongo2:
    hostname: mongo2
    container_name: localmongo2
    image: mongo
    expose:
    - 27017
    ports:
    - 27018:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    mongo3:
    hostname: mongo3
    container_name: localmongo3
    image: mongo
    expose:
    - 27017
    ports:
    - 27019:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    17 changes: 17 additions & 0 deletions docker.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # run this after setting up the docker-compose This will instantiate the replica set.
    # The id and hostname's can be tailored to your liking, however they MUST match the docker-compose file above.
    docker-compose up -d
    docker exec -it localmongo1 mongo

    rs.initiate(
    {
    _id : 'rs0',
    members: [
    { _id : 0, host : "mongo1:27017" },
    { _id : 1, host : "mongo2:27017" },
    { _id : 2, host : "mongo3:27017" }
    ]
    }
    )

    exit
    14 changes: 14 additions & 0 deletions mongoose.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // If on a linux server, use the hostname provided by the docker compose file
    // e.g. HOSTNAME = mongo1, mongo2, mongo3

    // If on MacOS add the following to your /etc/hosts file.
    // 127.0.0.1 mongo1
    // 127.0.0.1 mongo2
    // 127.0.0.1 mongo3
    // And use localhost as the HOSTNAME
    mongoose.connect('mongodb://<HOSTNAME>:27017,<HOSTNAME>:27018,<HOSTNAME>:27019/<DBNAME>', {
    useNewUrlParser : true,
    useFindAndModify: false, // optional
    useCreateIndex : true,
    replicaSet : 'rs0', // We use this from the entrypoint in the docker-compose file
    })
    26 changes: 26 additions & 0 deletions transaction.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    async function transaction() {
    // Start the transaction.
    const session = await ModelA.startSession();
    session.startTransaction();

    try {
    const options = { session };

    // Try and perform operation on Model.
    const a = await ModelA.create([{ ...args }], options);

    // If the first operation succeeds this next one will get called.
    await ModelB.create([{ ...args }], options);

    // If all succeeded with no errors, commit and end the session.
    await session.commitTransaction();
    session.endSession();
    return a;
    } catch (e) {
    // If any error occured, the whole transaction fails and throws error.
    // Undos changes that may have happened.
    await session.abortTransaction();
    session.endSession();
    throw e;
    }
    }