- Update
mongod.env
file with custom username and password - Run
docker compose up -d
- log on to mongo cli using below command
docker exec -it {docker-container-id} mongo -u "{your-username}" -p "{your-password}" --authenticationDatabase "admin"
- configure (initialize replicaset) on mongo-cli
rs.initiate({
"_id" : "rs0",
"members" : [
{"_id" : 0,"host" : "mongodb1:27017"},
{"_id" : 1,"host" : "mongodb2:27017"},
{"_id" : 2,"host" : "mongodb3:27017"}
]
});
- option step to setup primority primary node (You may need to restart docker-compose)
conf = rs.config();
conf.members[0].priority = 2;
rs.reconfig(conf);
use admin;
db.createUser({user: "cluster_admin",pwd: "password",roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { "role" : "clusterAdmin", "db" : "admin" } ]});
db.auth("cluster_admin", "password");
https://github.com/willitscale/learning-docker/tree/master/tutorial-12
https://www.youtube.com/watch?v=-XzMfd4XQak&t=1106s&ab_channel=WillitScale