Tuesday, June 30, 2015

Names of MongoDB nodes are important for web application connections using connectionString parameter



The names given to the mongoDb nodes are very important. These names are used by the web application to establish connections. If we do rs.status() as a mongo client, we may see the following names highlighted in red:


rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2015-07-01T03:29:11.718Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb-node-3.novalocal:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1215292,
                        "optime" : Timestamp(1435130014, 6611),
                        "optimeDate" : ISODate("2015-06-24T07:13:34Z"),
                        "lastHeartbeat" : ISODate("2015-07-01T03:29:11.654Z"),
                        "lastHeartbeatRecv" : ISODate("2015-07-01T03:29:10.310Z"),
                        "pingMs" : 0,
                        "configVersion" : 3
                },
                {
                        "_id" : 1,
                        "name" : "mongodb-node-1:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2015-07-01T03:29:10.406Z"),
                        "lastHeartbeatRecv" : ISODate("2015-07-01T01:37:53.542Z"),
                        "pingMs" : 0,
                        "lastHeartbeatMessage" : "Failed attempt to connect to mongodb-node-1:27017; couldn't connect to server mongodb-node-1:27017 (10.0.0.15), connection attempt failed",
                        "configVersion" : -1
                },
                {
                        "_id" : 2,
                        "name" : "mongodb-node-2:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1831066,
                        "optime" : Timestamp(1435130014, 6611),
                        "optimeDate" : ISODate("2015-06-24T07:13:34Z"),
                        "electionTime" : Timestamp(1435714575, 1),
                        "electionDate" : ISODate("2015-07-01T01:36:15Z"),
                        "configVersion" : 3,
                        "self" : true
                }
        ],
        "ok" : 1
}

On the application side, we may see a connectionString parameter such as:

connectionString = 'mongodb://mongodb-node-3.novalocal:27017,mongodb-node-1:27017,mongodb-node-2:27017/cpi?replicaSet=rs0&connectTimeoutMS=1000'

However these names don't actually resolve to an IP address. So take it work, I've modified the /etc/hosts file on the web server as follows


10.0.1.x mongodb-node-2
10.0.1.y mongodb-node-1
10.0.1.z mongodb-node-3.novalocal




To test the mapping works, try the following command on the web server:



ping mongodb-node-2


Reference:
http://qnalist.com/questions/5216546/mongotimeoutexception-trying-to-connect-to-a-replica-set

No comments:

Post a Comment