Tuesday, May 28, 2013

Learn deploying a Replica Set - MongoDB

Today, I read a bit on MongoDB: The Definitive Guide book and found out more about deploying a Replica Set. Last time, I learned about MongoDB that I tested master - slave (just basic).
Anyway, I was interested in deploying a replica set.
PRIMARY = centos
SECONDARY = centostest1
First, I connected to a primary node starting mongodb and set something.
[surachart@centos mongo]$ bin/mongod -replSet  surachart --rest --fork --logpath /tmp/mongodb-centos.log
about to fork child process, waiting until server is ready for connections.
forked process: 3989
all output going to: /tmp/mongodb-centos.log

child process started successfully, parent exiting
[surachart@centos mongo]$
[surachart@centos mongo]$ ps -aef | grep 3989
500       3989     1  2 14:33 ?        00:00:02 bin/mongod -replSet surachart --rest --fork --logpath /tmp/mongodb-centos.log
500       4038  1545  0 14:35 pts/0    00:00:00 grep 3989
[surachart@centos mongo]$ tail /tmp/mongodb-centos.log
Tue May 28 14:35:18.690 [FileAllocator] allocating new datafile /data/db/local.ns, filling with zeroes...
Tue May 28 14:35:18.690 [FileAllocator] creating directory /data/db/_tmp
Tue May 28 14:35:18.715 [FileAllocator] done allocating datafile /data/db/local.ns, size: 16MB,  took 0.004 secs
Tue May 28 14:35:18.716 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
Tue May 28 14:35:18.720 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0.004 secs
Tue May 28 14:35:18.723 [initandlisten] waiting for connections on port 27017
Tue May 28 14:35:18.723 [websvr] admin web console waiting for connections on port 28017
Tue May 28 14:35:18.727 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue May 28 14:35:18.727 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
Tue May 28 14:35:28.729 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
[surachart@centos mongo]$
[surachart@centos mongo]$ bin/mongo
MongoDB shell version: 2.4.3
connecting to: test
> config = { "_id" : "surachart", "members" : [ { "_id" : 0, "host" : "centos:27017" } ] }
{
        "_id" : "surachart",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "centos:27017"
                }
        ]
}
> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.conf()
{
        "_id" : "surachart",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "centos:27017"
                }
        ]
}
surachart:PRIMARY>
Then, I connected to a secondary node starting mongodb.
[surachart@centostest1 mongo]$ bin/mongod -replSet  surachart --rest --fork --logpath /tmp/mongodb-centostest1.log
about to fork child process, waiting until server is ready for connections.
forked process: 2844
all output going to: /tmp/mongodb-centostest1.log
child process started successfully, parent exiting
[surachart@centostest1 mongo]$ ps -aef |grep 2844
500       2844     1 12 14:37 ?        00:00:07 bin/mongod -replSet surachart --rest --fork --logpath /tmp/mongodb-centostest1.log
500       2890  1577  0 14:38 pts/0    00:00:00 grep 2844
[surachart@centostest1 mongo]$ tail /tmp/mongodb-centostest1.log
Tue May 28 14:38:18.845 [FileAllocator] done allocating datafile /data/db/local.ns, size: 16MB,  took 0.014 secs
Tue May 28 14:38:18.846 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
Tue May 28 14:38:18.862 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0.009 secs
Tue May 28 14:38:18.864 [initandlisten] waiting for connections on port 27017
Tue May 28 14:38:18.865 [websvr] admin web console waiting for connections on port 28017
Tue May 28 14:38:18.868 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue May 28 14:38:18.868 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
Tue May 28 14:38:28.869 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue May 28 14:38:38.870 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue May 28 14:38:48.871 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
[surachart@centostest1 mongo]$
After I made sure a secondary node that could start and no any issues. I come back to a primary node and added new node (a secondary node).
surachart:PRIMARY> rs.add("centostest1:27017")
{ "ok" : 1 }
surachart:PRIMARY> rs.conf()
{
        "_id" : "surachart",
        "version" : 2,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "centos:27017"
                },
                {
                        "_id" : 1,
                        "host" : "centostest1:27017"
                }
        ]
}
surachart:PRIMARY>
surachart:PRIMARY>
and also checked on a secondary node.
[surachart@centostest1 mongo]$ bin/mongo
MongoDB shell version: 2.4.3
connecting to: test
surachart:SECONDARY>
surachart:SECONDARY>
surachart:SECONDARY>
It looked good. Finally, I tested it by inserting data.
On Primary node:
surachart:PRIMARY> db.test.save( { a: 1 } )
surachart:PRIMARY> db.test.save( { b: 2 } )
surachart:PRIMARY> db.test.find()
{ "_id" : ObjectId("51a45f6b9f2bc22d6f79b98e"), "a" : 1 }
{ "_id" : ObjectId("51a45f709f2bc22d6f79b98f"), "b" : 2 }
On Secondary node:
surachart:SECONDARY> db.test.save( { 3: 2 } )
not master
surachart:SECONDARY> db.test.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
So, used "rs.slaveOk()" on a secondary node to allow read operations.
surachart:SECONDARY> rs.slaveOk
function (value) { return db.getMongo().setSlaveOk(value); }
surachart:SECONDARY> rs.slaveOk()
surachart:SECONDARY> db.test.find()
{ "_id" : ObjectId("51a45f6b9f2bc22d6f79b98e"), "a" : 1 }
{ "_id" : ObjectId("51a45f709f2bc22d6f79b98f"), "b" : 2 }
I could read data and done to begin about deploying a replica set on MongoDB.
Another test - Demote a primary to a secondary.
On centos:
surachart:PRIMARY> rs.stepDown()
Tue May 28 15:26:37.327 DBClientCursor::init call() failed
Tue May 28 15:26:37.329 JavaScript execution failed: Error: error doing query: failed at src/mongo/shell/query.js:L78
Tue May 28 15:26:37.330 trying reconnect to 127.0.0.1:27017
Tue May 28 15:26:37.331 reconnect 127.0.0.1:27017 ok
surachart:SECONDARY>
surachart:SECONDARY>
Meanwhile, on centostest1.
surachart:SECONDARY>
surachart:SECONDARY>
surachart:PRIMARY>
surachart:PRIMARY>
surachart:PRIMARY> rs.status()
{
        "set" : "surachart",
        "date" : ISODate("2013-05-28T08:30:51Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "centos:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3093,
                        "optime" : {
                                "t" : 1369729404,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-28T08:23:24Z"),
                        "lastHeartbeat" : ISODate("2013-05-28T08:30:51Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "syncingTo" : "centostest1:27017"
                },
                {
                        "_id" : 1,
                        "name" : "centostest1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 3177,
                        "optime" : {
                                "t" : 1369729404,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-28T08:23:24Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
surachart:PRIMARY>
and and... read more on document...

1 comment:

Sujit Shakya said...

Thank you for this post. Really helped me. Thanks :)