Thanks to https://gist.github.com/asoorm for helping with his docker-compose file!
Forked from harveyconnor/a-mongodb-replica-set-docker-compose-readme.md
Created
July 22, 2021 18:01
-
-
Save asepabdulsofyan/f659bfe41cb9821830592ee3e81b8467 to your computer and use it in GitHub Desktop.
MongoDB Replica Set / docker-compose / mongoose transaction with persistent volume
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 characters
| 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" ] |
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 characters
| # 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 |
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 characters
| // 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 | |
| }) |
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 characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment