This repo can help you to run MongoDB replica faster then just using step to step leads from articles below. You can use this as quick to deploy test environment if you want to play with MongoDB replica in a sandbox.
This is an implementation of this article Create a MongoDB Replica Set
Additional links Build Database Clusters with MongoDB
mongo-keyfile
- cluster key should be added to all servers in the clusterDocker-configsrv
- docker config for mongo instances
call
openssl rand -base64 756 > mongo-keyfile
to create new key file
configsrv-mongod.conf
- mongod.conf file for replica set instances
- Build and run cluster
docker-compose up
- Connect to instance
docker-compose exec mongo-replica-1 bash
- Log in to mongo
mongo -u root -p --authenticationDatabase admin
default password isexample
- From the mongo shell, initiate the replica set:
rs.initiate()
- While still connected to the mongo shell, add the other hosts to the replica set:
- Three replica hosts config
rs.add("mongo-replica-2")
rs.add("mongo-replica-3")
- Two replicas and arbiter config
rs.add("mongo-replica-2")
rs.addArb("mongo-replica-3")
- In case you want to some server as master by default you can increase its priority
config = rs.config()
config.members[0].priority = 2
rs.reconfig(config)
Once all members have been added, check the configuration of your replica set:
rs.conf() This will display a replica set configuration object with information about each member as well as some metadata about the replica set.
If you need to do so in the future, you can also check the status of your replica set:
rs.status()