Replica Sets Part 1: Master-Slave is so 2009

Replica sets are really cool and can be customized out the wazoo, so I’ll be doing a couple of posts on them (I have three written so far and I think I have a few more in there). If there’s any replica-set-related topic you’d like to see covered, please let me know and I’ll make sure to get to it.

This post shows how to do the “Hello, world” of replica sets. I was going to start with a post explaining what they are, but coding is more fun than reading. For now, all you have to know is that they’re master-slave with automatic failover.

Make sure you have version 1.5.7 or better of the database before trying out the code below.

Now, try killing the primary server. Wait a couple seconds and you’ll see the other (non-arbiter) server be elected primary.

Once there’s a new primary, restart the mongod you just killed. You’ll see it join in the fray, though not become master (there’s already a master, so it won’t rock the boat). After a few seconds, kill the current master. Now the old master will become master again!

It’s pretty fun to play with this, bringing them up and down and watching the mastership go back and forth (or maybe I’m easily amused).

Inserting and querying data

By default, slaves are for backup only, but you can also use them for queries (reads) if you set the “slave ok” flag. Connect to each of the servers and set this flag:

It should be noted that it appears that you can’t have a / in your replica name. Everything looks alright until you try to initialize your set it and it always complains that it can’t find any servers (even if you’re on one of them as you try to configure it!)

Hi kristina1,I am using 64-bit MongoDB and i had done testing with 64-bit MongoDB.The testing details as follows:- 1)I gave one million files as backup to my server using mongodb as my Database Storage. 2)My backup data size is “22.8-GB”. 3)The mongodb size is “1.95GB”. Now, my question, is there any settings available to compress the DB-Size Before configuring the backup to the server?.Please help me folks…Advance Thanks,

Hey guys, not sure why I didn’t see this comment before. If you have questions, the mailing list (groups.google.com/d/topic/mongodb-user/) is a good place to get help. @ericklind:disqus in the error you pasted, it looks like you’re missing a : in your host string: “localhost:27018”.

Hi,
i tried with multiple systems instead of single system. i can able to done replicaset. this scenario i am using ip to specify systems but i need to do without hardcode anything like specific ip or system name.. how it is possible to make this replication…? please help me…

In general you’re going to want to use hostnames so you can change the IPs underneath. You have to configure a set somewhere, so you need some things hardcoded. You might want to ask on the list (https://groups.google.com/forum/#!forum/mongodb-user) if you have specific configuration requirements.

I am going through your blog on MongoDB, it is really excellent and more informative with simple easy steps. I became a fan of your blog. i have configured 3 shards and added to cluster. I do not know how to add members to each shard as primary member, secondary member and Arbiter. I have mentioned below what I have done for your review. Could you please help me to add members as said.