Simple MongoDB Replica Set example

V. K.
3 min readFeb 13, 2018

--

Here you’ll find super simple MongoDB Replica Set (replication) example based on docker.

Prerequisites

You have to install docker on your machine.

Preparation

For this example we’ll use minimal Replica Set configuration based on using next nodes: primary (1 node), secondary (1 node) and arbiter (1 node, this node required for election process).

Please open in terminal 4 separated tabs, 3 for nodes described earlier and and 1 more for executing commands. All nodes will work in interactive mode and will print some internal information, it’s very helpful for understanding how MongoDB works under the hood.

Run Docker containers

Please execute in tab 1 next command:

docker network create --driver bridge xnet

It’ll create docker network which will be used for all our nodes.

Please execute in tab 1 (primary node):

docker run -it --rm --net=xnet -p 27017:27017 \
--hostname xmongo-primary-1 --name xmongo-primary-1 \
mongo:latest --port 27017 --replSet xmongo

Please execute in tab 2 (secondary node):

docker run -it --rm --net=xnet -p 27018:27018 \
--hostname xmongo-secondary-1 --name xmongo-secondary-1 \
mongo:latest --port 27018 --replSet xmongo

Please execute in tab 3 (arbiter node):

docker run -it --rm --net=xnet -p 27019:27019 \
--hostname xmongo-arbiter-1 --name xmongo-arbiter-1 \
mongo:latest --port 27019 --replSet xmongo

As result you’ll have 3 tabs for our nodes in interactive mode and you’ll see all logs from MongoDB.

Now, please execute in tab 4 next command for configure our Replica Set:

docker exec -it xmongo-primary-1 mongo --port 27017 --eval '
rs.initiate({"_id": "xmongo", "members": [
{"_id": 0, "host": "xmongo-primary-1:27017", "priority": 10},
{"_id": 1, "host": "xmongo-secondary-1:27018"},
{"_id": 2, "host": "xmongo-arbiter-1:27019", "arbiterOnly": true}
]})
'

And now please wait few seconds… You can see output in logs for all our nodes tabs and can find information what exactly MongoDB is doing ring now…

IMPORTANT: You can go to next step only when you find:

[rsBackgroundSync] sync source candidate: xmongo-primary-1:27017

in tab for secondary node.

Congratulations!!! Thats it!!! You, already have MongoDB Replica Set!!! 🎉🙂

Test

Please execute in tab 4 this command to insert document into primary node:

docker exec -it xmongo-primary-1 \
mongo --port 27017 --eval 'db.test.insert({c:200})'

And now please execute in tab 4 this command to find our document from secondary node:

docker exec -it xmongo-secondary-1 \
mongo --port 27018 --eval 'db.setSlaveOk();db.test.find()'

As result you’ll see:

{ "_id" : ObjectId("5a828abd27c4acb50e3a7964"), "c" : 200 }

It works!!!

Conclusion

You already have super simple MongoDB Replica Set and now you can play with it, you can make some chaos and turn off primary, investigate election process, bring primary back and to see what will happen and so on and so forth.

And please don’t forger it isn’t flawless production ready example, it’s just super simple minimalistic Replica Set for testing, experimenting, tweaking, digging, studying etc.

--

--