MongoDB replica sets   Leave a comment

Here I will explain how to create replica sets in mongo db. It is expected that the reader has some basic understanding of mongo db and nosql. If not download mongo db from http://www.mongodb.org/ and try out some basic tutorials before starting on this one :

1. Start mongo instances to be part of a replica set:

  • Create three directories for eg. C:\data\db1, C:\data\db2 and C:\data\db3. These are gonna be the data directories for our mongo db replica sets.
  • Go to your mongo db bin directory and execute the command : mongod –port <portno> –dbpath <path_to_data_directory> –replSet <replica_set_name>
  • For eg. C:\mongodb\mongodb-win32-x86_64-2.0.5\bin>mongod –port 40000 –dbpath c:/data/db1 –replSet rs0 which will start a mongo db on port 40000 and make it part of replSet ‘rso’
  • Similarly start two other mongod instances as part of replica set ‘rs0’ using the command on different ports and with different data directories.

mongod –port 40001 –dbpath c:/data/db2 –replSet rs0

mongod –port 40002 –dbpath c:/data/db3 –replSet rs0

2. Configure the replica configuration.

  • Once we started the three instances we need to configure them so that one runs as the primary and the other as slaves and the three instances replicate between each other.
  • Open a mongo shell using the command mongo localhost:40000.
  • Enter rs.initiate() to initiate a replica set.
  • Add the other two dbs to the replica set using the commands:

rs.add(“D-SJC-00535987:40001”);

rs.add(“D-SJC-00535987:40002”);

  • Execute rs.status() to check.  You should see something like this:
{
“set” : “rs0”,
“date” : ISODate(“2012-11-29T01:32:11Z”),
“myState” : 1,
“members” : [
{
“_id” : 0,
“name” : “D-SJC-00535987:40000”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“optime” : {
“t” : 1354152592000,
“i” : 1
},
“optimeDate” : ISODate(“2012-11-29T01:29:52Z”),
“self” : true
},
{
“_id” : 1,
“name” : “D-SJC-00535987:40001”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 144,
“optime” : {
“t” : 1354152592000,
“i” : 1
},
“optimeDate” : ISODate(“2012-11-29T01:29:52Z”),
“lastHeartbeat” : ISODate(“2012-11-29T01:32:09Z”),
“pingMs” : 1
},
{
“_id” : 2,
“name” : “D-SJC-00535987:40002”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 139,
“optime” : {
“t” : 1354152592000,
“i” : 1
},
“optimeDate” : ISODate(“2012-11-29T01:29:52Z”),
“lastHeartbeat” : ISODate(“2012-11-29T01:32:10Z”),
“pingMs” : 1
}
],
“ok” : 1
}
  • On the primary system you should see output like :
Wed Nov 28 17:29:49 [rsHealthPoll] replSet member D-SJC-00535987:40001 is now in
state STARTUP2
Wed Nov 28 17:29:51 [rsHealthPoll] replSet member D-SJC-00535987:40001 is now in
state RECOVERING
 

3. Test that replication is working

  • Connect to the primary mongo db using mongo localhost:<port_no_of_primary>.You should see a prompt like “PRIMARY>” signifying that this db is the primary of the replica set. If you are not sure as to which of your dbs are primary enter the command rs.status().
  • Enter a test record using the command:

db.test.insert(

{

_id: 1,
name: { first: ‘Salil’, last: ‘Surendran’ },
profession: ‘Software Developer’,
hobbies: [‘Volleyball’,’Table Tennis’,’Chess’,’Hiking’]

}

)

  •  Connect to the secondary using mongo localhost:<port_of_any_of_the_secondary>. You should see a prompt like “SECONDARY>”
  • Enter the command ‘rs.slaveOK()’ and you will see an output like

    not master and slaveok=false

  • Now execute the command ‘db.test.find()’ and you should see the record inserted in the primary in the secondary:

{
“_id”: 1,
“name”: {
“first”: “Salil”,
“last”: “Surendran”
},
“profession”: “Software Developer”,
“hobbies”: [“Volleyball”,
“Table Tennis”,
“Chess”,
“Hiking”]
}

  • Repeat the same command after connecting to the other secondary and you should see the record replicated on that db too. This shows that the replication is working properly.

 

Advertisements

Posted November 30, 2012 by salilsurendran in Technology

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: