Pawan
Pawan

Reputation: 32331

Mongodb replica Set initially starting in SECONDARY Mode

I have made a mongodb replicaSet with one primary , secondary and an arbiter.

The problem i am facing is , when i start mongodb server initially , it's being started in secondary mode

That is Intially when i type mongo on terminal its displaying as

ubsc:SECONDARY>

I have created a replica set using

config = {with all my server details }
rs.reconfig(config)

I have also tried using the rs.add(serverhoststr) option also .

This is below my mongo shell outputs and the log file

ubsc:PRIMARY> rs.status()
{
    "set" : "ubsc",
    "date" : ISODate("2013-10-31T19:09:26Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "mongoA:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2210,
            "optime" : Timestamp(1383246016, 1),
            "optimeDate" : ISODate("2013-10-31T19:00:16Z"),
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "mongoB:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 576,
            "optime" : Timestamp(1383246016, 1),
            "optimeDate" : ISODate("2013-10-31T19:00:16Z"),
            "lastHeartbeat" : ISODate("2013-10-31T19:09:26Z"),
            "lastHeartbeatRecv" : ISODate("2013-10-31T19:09:24Z"),
            "pingMs" : 0,
            "syncingTo" : "192.168.2.67:27017"
        },
        {
            "_id" : 2,
            "name" : "mongoC:27019",
            "health" : 1,
            "state" : 10,
            "stateStr" : "ARBITER",
            "uptime" : 550,
            "lastHeartbeat" : ISODate("2013-10-31T19:09:26Z"),
            "lastHeartbeatRecv" : ISODate("2013-10-31T19:09:25Z"),
            "pingMs" : 0
        }
    ],
    "ok" : 1
}

mongodb log

Fri Nov  1 01:22:00.981 [initandlisten] MongoDB starting : pid=13177 port=27017 dbpath=/data/mongodb 64-bit host=MLDev5
Fri Nov  1 01:22:00.982 [initandlisten] db version v2.4.6
Fri Nov  1 01:22:00.982 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
Fri Nov  1 01:22:00.982 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Fri Nov  1 01:22:00.982 [initandlisten] allocator: tcmalloc
Fri Nov  1 01:22:00.982 [initandlisten] options: { config: "mongod1.conf", dbpath: "/data/mongodb", fork: "true", logpath: "/data/mongodb/mongodb.log", nojournal: "true", port: 27017, replSet: "ubsc", rest: "true" }
Fri Nov  1 01:22:01.000 [initandlisten] waiting for connections on port 27017
Fri Nov  1 01:22:01.004 [websvr] admin web console waiting for connections on port 28017
Fri Nov  1 01:22:01.008 [rsStart] replSet I am MongoA:27017
Fri Nov  1 01:22:01.009 [rsStart] replSet STARTUP2

rs.conf

ubsc:PRIMARY> rs.conf()
{
    "_id" : "ubsc",
    "version" : 10,
    "members" : [
        {
            "_id" : 0,
            "host" : "mongoA:27017"
        },
        {
            "_id" : 1,
            "host" : "mongoB:27018"
        },
        {
            "_id" : 2,
            "host" : "mongoC:27019",
            "arbiterOnly" : true
        }
    ]
}

Please let me know why initially the server is being getting started in Secondary mode ??

Sorry to trouble you all , but couldn't find any solution for this

Upvotes: 0

Views: 3931

Answers (2)

Asya Kamsky
Asya Kamsky

Reputation: 42352

There is no problem to solve - this is just how replica sets work.

A node cannot become a PRIMARY without there being an election. So every node (other than the ARBITER) comes up and becomes a SECONDARY before one of them can become a PRIMARY (via an election).

So this is normal, and expected, and is not a problem, therefore there is no solution.

Upvotes: 6

Rafa Viotti
Rafa Viotti

Reputation: 10522

You haven't set priorities on any of the replica set members, so MongoDB is free to choose which one will be primary. In your case, MongoA. MongoC cannot become primary since it is an arbiter.

If you wan't to force a member to be primary, increase its priority. The default priority is one.

To ensure MongoA is primary:

var cfg = rs.conf()
cfg.members[0].priority = 2
cfg.members[1].priority = 1
rs.reconfig(cfg)

To ensure MongoB is primary:

var cfg = rs.conf()
cfg.members[0].priority = 1
cfg.members[1].priority = 2
rs.reconfig(cfg)

http://docs.mongodb.org/manual/tutorial/force-member-to-be-primary/

Upvotes: 2

Related Questions