Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 1.67 KB

2-setup-mongodb-cluster.md

File metadata and controls

56 lines (39 loc) · 1.67 KB

Running MongoDB as a Cluster of Machines

Source: https://www.mongodb.com/resources/products/compatibilities/deploying-a-mongodb-cluster-with-docker

1. Create a Docker Network

docker network create mongoCluster

2. Start 3 Instances of MongoDB

docker run -d --rm -p 27018:27017 --name mongo1 --network mongoCluster mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo1
docker run -d --rm -p 27019:27017 --name mongo2 --network mongoCluster mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo2
 
docker run -d --rm -p 27020:27017 --name mongo3 --network mongoCluster mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo3

3. Initiate the Replica Set

docker exec -it mongo1 mongosh --eval "rs.initiate({
 _id: \"myReplicaSet\",
 members: [
   {_id: 0, host: \"mongo1\"},
   {_id: 1, host: \"mongo2\"},
   {_id: 2, host: \"mongo3\"}
 ]
})"

4. Test and Verify the Replica Set

docker exec -it mongo1 mongosh --eval "rs.status()"

You can stop one of the members of the cluster (mongo1) and try and read the data again.

docker stop mongo1

docker exec -it mongo2 mongosh --eval "rs.status()"

The result shows that one of the other members has been elected as the primary node. If mongo1 rejoins the cluster, it will be a secondary member.

docker run -d --rm -p 27018:27017 --name mongo1 --network mongoCluster mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo1

docker exec -it mongo1 mongosh --eval "rs.status()"