Skip to content
Feb 10 2012 by Pravin Paratey

Setting up Replica sets in MongoDB

Are you ready to set up replica sets in under 5 minutes? Set your stopwatches. Ready, set, go!

Minute 0: Overview of the servers

We are going to use replica sets consisting of 3 nodes which looks something like this,

replica set with 3 nodes

Minute 1: Downloading and installing MongoDB

The latest instructions to download and install Mongo can be found here. For me, running on a CentOS machine, all I had to do was add the 10gen repository, followed by a sudo yum install mongo-10gen mongo-10gen-server. To add the 10gen repository, adding the following lines to /etc/yum.repos.d/10gen.repo

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
$ sudo yum install mongo-10gen mongo-10gen-server

Do this on all your servers.

Minute 2: Editing the configuration file

Next, edit the configuration file which is present at /etc/mongodb.conf and add the two lines to it

bind_ip = 192.168.1.1 # Public or Internal IP of the server. This will change with each server.
replSet = london # This string should be the same on all 3 servers

Again, this should be done on all servers.

Minute 3: Starting Mongo

Start Mongo on all servers by running

$ sudo service mongod start

On Debian systems, you will have to run the mongodb service instead,

$ sudo service mongodb start

Minute 4: Adding nodes to the primary

Pick any machine and enter the Mongo CLI by typing mongo IP.Of.Server.One. This will be your primary machine (until the cluster decides to elect another as its primary).

Next, run rs.initiate() on the machine. This will initialise the node to use replica sets. Then run rs.add("IP.Of.Server.Two") and rs.add("IP.Of.Server.Three") to add the other two nodes to the replica set.

$ mongo IP.Of.Server.One
> rs.initiate();
> rs.add("IP.Of.Server.Two");
> rs.add("IP.Of.Server.Three");
> rs.status();

That's it! We're done. That was easy, wasn't it?

Removing a member of the replica set

Removing a node is as easy as typing

> rs.remove("IP.Of.Server.To.Remove:port")

Get in touch

Email

skype twitter linkedin github amazon

Advert

Latest Articles

Cookie Mapping in Advertising
In this article, we will cover the process of cookie mapping (or cookie syncing) and how advertising companies use it to exchange information with websites and advertising platforms to serve relevant advertising to a user.
Building a url shortener in 10 mins
A 10 minute guide to building your own url shortener i.e. a service that lets you shorten a long url - http://pravin.insanitybegins.com to a url that takes up less number of characters - http://goo.gl/q3jEH
Going Text-mode
I spend a great deal of time working on the Terminal. Here, I list my must-have console applications for email, www browsing, and programming in Linux, Windows and Mac OSX.
Setting up Replica sets in MongoDB
A 5-minute guide to setting up replica sets on MongoDB. Covers downloading, installation on various flavours of linux, editing the config file and initiating replica sets!
Super quick Find & Replace
This article explores a number of ways of performing a find/replace and compares the various implementations for different sizes of the find/replace list and input text.
Set data structures & Implementation
Sets are an important concept and extremely useful in various computer science applications. We take a look at some of the ways a set data structure can be implemented.
Manage your wireless with nmcli
This article shows you how to use the command line tool nmcli to manage your wireless networks. If you prefer cli over gui, go ahead and take a look.
Binary heaps & Priority Queues
Heaps and Queues can be a powerful data structure. This article goes into the implementation of a binary heap and extends the data structure to act as a priority queue.
Recursive functions, Stack overflows and Python
Here we look at recursive functions in Python, and explore some ways of avoiding the maximum stack depth limit by using alternate functions like reduce().
Facebook puzzle - Find Sophie
Today we solve the FB engineering puzzle - Find Sophie. We start with a naive solution and improve the algorithm until we can pass the Facebook Puzzlebot. In closing, I leave you with open-ended questions on improving the algorithm further.