Testing a three-member mongodb replica set

  01 Dec 2014


My purpose of this article is to create local mongodb replica set with three member just for testing to see how it works. ( if you want production deployment, please checkout official document at http://docs.mongodb.org/manual/tutorial/deploy-replica-set/ )

Replication is a great thing for scaling read, i.e. you have copies data to read from. In Mongodb, it’s used a replica set to archieve replication by running multiple mongod instances to have the same data set. But replica set can have only one primary node to recieve all write operation, others are secondary. ( it’s used Ark, which is Consensus algorithm, as a election protocol to choose a primary )

We name replica set as rs0 ( so, its member are rs0-0, rs0-1 and rs0-2 ). To do so, first, we have to create db directories for each member (for --dbpath), like so:

mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2    

Then, start mongod instance by using –smallfiles and –oplogSize (recommended for testing) for all members.

mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128
mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128

Note that, you can use other available ports if these are already in used. After you have checked those instances are running.

Choose one (by specifying the port) and connect to it via mongo shell, for instance, choose the first one:

mongo --port 27017

Issuing the rs.initiate() in mongo shell to intial the replica set.

root@8gbtestes:~# mongo --port 27017
MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:27017/test
> rs.initiate()
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "8gbtestes:27017",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
>

In mongo shell, create replica set config object. for me, I use following:

rsconf = {
           _id: "rs0",
           members: [
                      {
                       _id: 0,
                       host: "8gbtestes:27017"
                      }
                    ]
         }

And don’t forget to use your hostname (perhaps, localhost) and proper port at rsconf['members'][0]['host'], then pass the config to intiate like so:

> rs.initiate( rsconf )
{
        "_id" : "rs0",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "8gbtestes:27017"
                }
        ]
}

Then, connect the the primary to add more members:

root@8gbtestes:~# mongo --port 27017
MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:27017/test
rs0:PRIMARY> rs.add("8gbtestes:27018")
{ "ok" : 1 }
rs0:PRIMARY> rs.add("8gbtestes:27019")
{ "ok" : 1 }

Now we have a good replica set and we can check its status with rs.status() any time.

Additionally, you can verify if the replica set is working by creating a new database and insert some data to it :)

Create database greetings and insert data:

rs0:PRIMARY> use greetings
switched to db greetings
rs0:PRIMARY>db.greetings.insert( {text: "hello world", emoji: ":)"} )

Then check with db.printSlaveReplicationInfo(). Also, in order to see your data is really safe, you can simply shutdown the primary and reconfigure those surving members.

rs0:PRIMARY> use greetings
switched to db greetings
rs0:PRIMARY> db.greetings.find().pretty()
{
        "_id" : ObjectId("547c30c17392576bfbd41d33"),
        "text" : "hello world",
        "emoji" : ":)"
}
{
        "_id" : ObjectId("547c36807392576bfbd41d34"),
        "text" : "hello world2",
        "emoji" : ":D"
}
comments powered by Disqus