Setup MongoDB Replica Set

Before embarking on setting the replica set make sure three machines are set as mentioned in previous MongoDB setup blog post.

Once everything is set make sure all three machines can communicate with each other. Mostly there will be a either a firewall issue that could be limiting the communication or bind_ip property in mongod.conf that might have been left un-commented. I had no success with setting bind_ip to specific ip. So had to comment it out. When commented, MongoDB listens to all the interfaces.

Also would recommend setting the /etc/hosts file to map s1, s2, s3 with specific ip addresses.

The process to check if all the machines are connected and communicating (s1, s2, s3)
Connect to s1 and then type the following command to confirm the connection.
Note: Port is not required if the set ports are default. You can also refer official MongoDB site on Replica Setup

ssh username@s1
mongo --host s2 --port 27017
mongo --host s3 --port 27017

Repeat the same test on s2 and s3. In total, 6 results should show successful connection. On Mac, rlimits size is limited and normally MongoDB complains about it but if you notice we have already set the parameter in mongodb.plist file to 1024. If its not set then mongo will display the warning as below. Also, make sure oplogSize is set high enough (Default on Mac is 183MB). Higher value ensures secondary can copy without becoming stale. This value is set mongod.conf file.

Sat Aug 24 11:25:01.871 [initandlisten] MongoDB starting : pid=332 port=27017 dbpath=/data/db-collection-location/ 64-bit host=address
Sat Aug 24 11:25:01.871 [initandlisten] 
Sat Aug 24 11:25:01.871 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Sat Aug 24 11:25:01.871 [initandlisten] db version v2.4.6

Now you are almost ready to have Mongo Replica working with two secondaries and one primary.

Do the following on the primary of your choice.
Connect to mongo on the primary you like. Note: This is still not the primary.

On shell type: mongo
Then type. Note: This takes some time. Have patience. At this point I would advice you
take a small break. [ ‘>’ is mongo prompt]

>rs.initiate()

On screen you will see something like

{
 "info2" : "no configuration explicitly specified -- making one",
 "me" : "s1:27017",
 "info" : "Config now saved locally. Should come online in about a minute.",
 "ok" : 1
 }
tarzan:STARTUPxx>

[‘>’ Mongo prompt again]. This means Mongo is ready for replica. This machine is not primary yet. In a second it will be assigned as primary. Mongo decides that.

Then type command rs.conf()

> rs.conf();
 {
     "_id" : "tarzan",
     "version" : 1,
     "members" : [
         {
             "_id" : 0,
             "host" : "s1:27017"
         }
     ]
 }

At this point you are ready to add the secondary host. If you have set hosts name in /etc/hosts file then just pass the host name or else pass ip address. Preferred way is to set the host name for convenience.

tarzan:PRIMARY> rs.add("s2")
tarzan:PRIMARY> rs.add("s3")

You will see a message for both saying

 { "ok" : 1 }

Now run rs.conf(); & run rs.status() command to see the list of host mapped and listed as secondary. Run the same command on host s2 and s3 to see almost the same result.

This entry was posted in MongoDB and tagged , , . Bookmark the permalink.

Leave a comment