- What is it?
- Are there any prerequisites?
- How do I run the Replica Set?
- How do I access the Mongo Shells for each Instance?
- How does it work?
- Robo 3T
- Thanks / Further Reading
This docker-compose
setup starts a local mongo replica set with 3 instances running on:
- mongo1:30001
- mongo2:30002
- mongo3:30003
• Docker Compose
• Docker
• The following in your /etc/hosts
file:
127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
Simples:
docker-compose up -d
docker exec -it mongo1 sh -c "mongo --port 30001"
docker exec -it mongo2 sh -c "mongo --port 30002"
docker exec -it mongo3 sh -c "mongo --port 30003"
- Starts three instances of Mongo
- On the first instance it runs the following Mongo Shell command:
rs.initiate(
{
_id : 'my-replica-set',
members: [
{ _id : 0, host : "mongo1:30001" },
{ _id : 1, host : "mongo2:30002" },
{ _id : 2, host : "mongo3:30003" }
]
}
)
- This causes all 3 instances to join the replica set named
my-replica-set
and start talking to each other - One is elected to become the
PRIMARY
and the other two becomeSECONDARY
instances - The Docker healthcheck config is used to cause the initialisation of the replica set. More info in the further reading links.
I used Robo 3T to test it locally and used the following config for the connection: