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 their docker-compose file!
This will guide you through setting up a replica set in a docker environment using.
Thanks to https://gist.github.com/asoorm for helping with their docker-compose file!
| 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" ] |
| # 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", arbiterOnly: true } | |
| ] | |
| } | |
| ) | |
| exit |
| // 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 | |
| }) |
| 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; | |
| } | |
| } |