Skip to content

Instantly share code, notes, and snippets.

@kokorolx
Created August 29, 2024 02:25
Show Gist options
  • Save kokorolx/76c03c09a83f207bd7bad6e2acda7f78 to your computer and use it in GitHub Desktop.
Save kokorolx/76c03c09a83f207bd7bad6e2acda7f78 to your computer and use it in GitHub Desktop.
Setup mongodb replicaset with docker compose
openssl rand -base64 756 > mongo-keyfile
chmod 400 mongo-keyfile

Explanation:

Keyfile (mongo-keyfile): The keyfile is mounted into each container at /etc/mongo-keyfile. MongoDB uses this file to authenticate communication between replica set members.

  • --keyFile: This option specifies the location of the keyfile within the container.
  • --auth: This option enables authentication, requiring users to authenticate before they can connect to the MongoDB instance.

Start the Docker Containers

docker-compose up -d

Exec to mongo1 to set replica

docker exec -it mongo1 mongosh --username root --password example --authenticationDatabase admin

use admin

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

rs.status() // it should show replicaset information, including members

Test

docker exec -it mongo1 mongosh --username root --password example --authenticationDatabase admin

use mydatabase

db.mycollection.insertOne({ name: "Alice", age: 30 })
db.mycollection.insertOne({ name: "Bob", age: 25 })
db.mycollection.insertOne({ name: "Charlie", age: 35 })

db.mycollection.find().pretty()

check data in the replica mongo2, it should show same data as the mongo1

docker exec -it mongo1 mongosh --username root --password example --authenticationDatabase admin
use mydatabase

db.mycollection.find().pretty()
version: '3.8'
services:
mongo1:
image: mongo:latest
container_name: mongo1
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_REPLICA_SET_NAME: "rep1"
volumes:
- mongo1_data:/data/db
- ./mongo-keyfile:/etc/mongo-keyfile
command: ["mongod", "--replSet", "rep1", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile", "--auth"]
mongo2:
image: mongo:latest
container_name: mongo2
restart: always
ports:
- 27018:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_REPLICA_SET_NAME: "rep1"
volumes:
- mongo2_data:/data/db
- ./mongo-keyfile:/etc/mongo-keyfile
command: ["mongod", "--replSet", "rep1", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile", "--auth"]
mongo3:
image: mongo:latest
container_name: mongo3
restart: always
ports:
- 27019:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_REPLICA_SET_NAME: "rep1"
volumes:
- mongo3_data:/data/db
- ./mongo-keyfile:/etc/mongo-keyfile
command: ["mongod", "--replSet", "rep1", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile", "--auth"]
volumes:
mongo1_data:
mongo2_data:
mongo3_data:
rJR2QfOUzCs74hJGjJfvXEIiUSku4NmptDMNra7w78eXvUoPa/6KuBY2Af9RCmow
ahsvpAL1XbR25nyNWGdV5e6ne6HMW1N6GiYaI6Hss8bgUGjpX2HRBop8PBHgdyxP
Z5Q3aYTEKhTlpFvSF9qEu58pzT+3z5o4p810REv7ItwxENtdutdqyWAhdNqbePAF
MsaJRs/c+9JEH9EhXZvMU7yRKsoRQFNFcLinIO3Zke4RNQChx4NpJP39r0AyQ1cW
81OlUvN9b0yni2aKX4Pw78JiN/dhwOuLg2/2V+5tHsr7KmBRSP2peCcgkV2e0xYt
chowV+2AIqQlfHhQ0mqzf0F/Gw1re1A3t5CgpjstjFxjxcBvXkY+P9qRnar8+wxB
j0yAtTDG2a1qkQd82DnTletC844bjXvFWtvI71vHmswk/DjGHz+ApBUxdqLfbAcc
NqypeDZaWy1dvJmLkqmtqX15Rj243hz0G1l+2nsjBKXnByXkkulukijho5stIWXq
+gw9AKLoUayfSd8bQJ6A05ta9nr3UU8xpKAzVIlCWRggyKyNVq+altQ26yceIdXn
qlO66UzOPvWlYp5j1os2HDGkR0ErN+exxX85sIkuOWYZH3+U4UdkKZQ2XAWGZO2J
h7hqJfqND3EQkgNbQtXw2lHE3kIF1wAWsGZPCJrCZELlEuNe1yJQqKFViyOJa0X/
4f1nW25mZ9zymszrlGn1fcPoGqP6QdaM8j303oVyH7didHmRPWAKiUqQ3+RKCplC
FI/RvR116eTeYnjyER5J/NIdoiZc/pD9UQvwu48z2AFjYi1a7ywfzAefIxF6AIfZ
YldVykHC6qWOQyffBysx6+X5fUIjCoB63HXvMJ5nI13HpArXIxILU4aDZwWssFjJ
zm+RdaFCp0US8T8wpqO0dkG1aws+zpi/TrZ2jG3AAH/0fbbtqVmMZ4MHRx2cbkM5
JVgj8ijSa2x7mBoMCI7cwxeStuoACRiulINL2Qx0SAtRvN1J
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment