Skip to content

Instantly share code, notes, and snippets.

@AlaeddineMessadi
Last active March 3, 2022 21:11
Show Gist options
  • Save AlaeddineMessadi/f98da31f1cefa0cf920f8026bcf73fdb to your computer and use it in GitHub Desktop.
Save AlaeddineMessadi/f98da31f1cefa0cf920f8026bcf73fdb to your computer and use it in GitHub Desktop.
MongoDB Replication Set with Configuration file and scripts in docker-compose
version: "3"
services:
mongo1:
hostname: mongo1
container_name: mongo1
image: mongo:5.0.6
expose:
- 27017
ports:
- 27011:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
networks:
- replset-network
volumes:
- ./data/db1:/data/db
- ./init.sh:/scripts/init.sh
mongo2:
hostname: mongo2
container_name: mongo2
image: mongo:5.0.6
expose:
- 27017
ports:
- 27012:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
networks:
- replset-network
volumes:
- ./data/db2:/data/db
mongo3:
hostname: mongo3
container_name: mongo3
image: mongo:5.0.6
expose:
- 27017
ports:
- 27013:27017
restart: always
networks:
- replset-network
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
volumes:
- ./data/db3:/data/db
networks:
replset-network:
driver: bridge
#!/bin/bash
mongo <<EOF
var config = {
"_id": "rs0",
"version": 1,
"members": [
{
"_id": 1,
"host": "mongo1:27011",
"priority": 1
},
{
"_id": 2,
"host": "mongo2:27012",
"priority": 2
},
{
"_id": 3,
"host": "mongo3:27013",
"priority": 3
}
]
};
rs.initiate(config, { force: true });
rs.status();
EOF
#!/bin/bash
docker-compose --file docker-compose.yml up -d
sleep 7
docker exec mongo1 /scripts/init.sh
@AlaeddineMessadi
Copy link
Author

If you are familiar with this Error from Prisma or other ORM and you need to use Replication Set Mongodb instead of a standalone cluster. then this is good to go with.

 (node:1298) UnhandledPromiseRejectionWarning: Error: 
Invalid `prisma.book.create()` invocation in
/Users/AlaeddineMessadi/PrismaProjects/prisma-mongodb/index.ts:12:22

   5 async function main() {
   6     await prisma.$connect()
   7 
  8     await prisma.book.create(
  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (IllegalOperation): This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.)" } })
    at cb (/Users/AlaeddinMessadi/codeprisma-mongodb/node_modules/@prisma/client/runtime/index.js:38707:17)
    at async PrismaClient._request (/Users/AlaeddinMessadi/code/prisma-mongodb/node_modules/@prisma/client/runtime/index.js:40853:18)

@AlaeddineMessadi
Copy link
Author

As there will be an election to choose which member will be PRIMARY, mongo1 has the highest chance to be the Primary server.
therefore you have to check first with one is the PRIMARY then use it in the database connection string

mongodb://localhost:27011/mydatabse?replicaSet=rs0&readPreference=primary&directConnection=true&ssl=false

localhost:27011 is the host:port of mongo1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment