A simple mongodb cluster with 1 config server, 2 shard servers and 1 router.
Generate keyfile for replica set.
openssl rand -base64 741 > ./data/replica-keyfile
chmod 600 ./data/replica-keyfile
First of all, comment out the --keyFile
option in compose.yml
.
docker compose up -d mongodb-config-1
docker exec -it mongodb-config-1 mongosh
Init replica set in config server.
rs.initiate({
_id: "config_rs",
configsvr: true,
members: [
{ _id : 0, host : "192.168.1.38:30101" },
]
})
rs.status()
Add superuser to config server in admin database.
docker exec -it mongodb-config-1 mongosh
use admin
db.createUser({
user: "root",
pwd: "password",
roles: [{"role":"root","db":"admin"}],
})
Uncomment the --keyFile
option in compose.yml
and restart config server.
docker compose up -d
Init replica set in shard server.
// on mongodb-shard-1
rs.initiate({
_id: "shard1_rs",
members: [
{ _id : 0, host : "192.168.1.38:30201" },
]
})
Add shard to router.
// on mongodb-router
sh.addShard("shard1_rs/192.168.1.38:30201")