Friday, November 30, 2012

Configuring MongoDB Replication on RedHat Linux nodes

In this post I’ll look at configuring replication in MongoDB. In my setup I have 2 x RedHat Linux nodes that will act as the Primary and Secondary for my MongoDB replication set.

I began by downloading MongoDB on both the nodes of the replication set.

[root@isvx7 ~]# cd mongodb
[root@isvx7 ~]# wget ftp://ftp.muug.mb.ca/mirror/fedora/epel/5/x86_64/ganglia-3.0.7-1.el5.x86_64.rpm
[root@isvx7 mongodb]# ls
mongodb-linux-x86_64-2.2.0      mongodb-linux-x86_64-2.2.0.tar
[root@isvx7 mongodb]#

My MongoDB replication set will be called liverpool, and “anfield1” and “anfield2” will be the two dbpaths on each of the nodes respectively.

[root@isvx7 ~]# mkdir anfield1
[root@isvx3 ~]# mkdir anfield2

Next I started the mongod server passing it the replication set name, and the dbpath. I also use port 27001 and limited the oplogSize to 50

[root@isvx7 ~]# mongod --replSet liverpool --dbpath anfield1 --port 27001 --oplogSize 50
<some related configuration messages>
Fri Nov 30 16:13:36 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Nov 30 16:13:46 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

Next I went and did the same thing on the other node ie. isvx3

 [root@isvx3 ~]# mongod --replSet liverpool --dbpath anfield2 --port 27001 --oplogSize 50
<some related configuration messages>
Fri Nov 30 15:17:17 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Nov 30 15:17:27 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

Right now both these nodes know that they are part of the “liverpool” replication set, but that is about it. They don’t know who else is part of the same replication set.

So when I connect to the mongod server on isvx7, it tells me that is not the master or the secondary.

[root@isvx7 ~]# mongo --port 27001
MongoDB shell version: 2.2.0
connecting to: 127.0.0.1:27001/test
> db.isMaster()
{
        "ismaster" : false,
        "secondary" : false,
        "info" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)",
        "isreplicaset" : true,
        "maxBsonObjectSize" : 16777216,
        "localTime" : ISODate("2012-11-30T23:22:44.236Z"),
        "ok" : 1
}
>

This means that I would need to run replSetInitiate to initiate the replication set on the nodes, but before that we need to create the config file on node isvx7.

> cfg = { _id : "liverpool", members : [ { _id:0, host:"isvx7:27001" }, { _id:1, host:"isvx3:27001" } ] }
{
        "_id" : "liverpool",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "isvx7:27001"
                },
                {
                        "_id" : 1,
                        "host" : "isvx3:27001"
                }
        ]
}
>

Now we will initiate the replica set on node isvx7 with the config file that we created.

> rs.initiate(cfg)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
>

When we now do the db.isMaster() on isvx7, we see that it is the master/primary.

> db.isMaster()
{
        "setName" : "liverpool",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "isvx7:27001",
                "isvx3:27001"
        ],
        "primary" : "isvx7:27001",
        "me" : "isvx7:27001",
        "maxBsonObjectSize" : 16777216,
        "localTime" : ISODate("2012-11-30T23:51:11.561Z"),
        "ok" : 1
}
liverpool:PRIMARY>

Here is what I see when I try to start the shell from the secondary node.

[root@isvx3 ~]#  mongo --port 27001
MongoDB shell version: 2.2.0
connecting to: 127.0.0.1:27001/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
liverpool:SECONDARY>
liverpool:SECONDARY> db.isMaster()
{
        "setName" : "liverpool",
        "ismaster" : false,
        "secondary" : true,
        "hosts" : [
                "isvx3:27001",
                "isvx7:27001"
        ],
        "primary" : "isvx7:27001",
        "me" : "isvx3:27001",
        "maxBsonObjectSize" : 16777216,
        "localTime" : ISODate("2012-12-01T00:03:49.245Z"),
        "ok" : 1
}
liverpool:SECONDARY>

No comments: