Towhid
Towhid

Reputation: 2084

How to force a mongod to become primary in a replica set?

1) I have 3 mongodbs running in a replica set

mongod --fork --logpath a.log --smallfiles --oplogSize 50 --port 27001 --dbpath data/z1 --replSet z
mongod --fork --logpath b.log --smallfiles --oplogSize 50 --port 27002 --dbpath data/z2 --replSet z
mongod --fork --logpath c.log --smallfiles --oplogSize 50 --port 27003 --dbpath data/z3 --replSet z

2) Now 27001 and 27002 are down.

What do I need to do to make 27003 primary without restarting 27001 and 27002?

Upvotes: 9

Views: 39913

Answers (2)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

Here is how you can do it.

The really easy way

Restart the other two instances after deleting the contents of their dbpath.

The easy, but insecure way

Assuming you can not get the other nodes back online (which reminds me a bit of a homework for M102)

replset:SECONDARY> oldConf = rs.conf()
[...]
replset:SECONDARY> conf = {
... 
... _id:oldConf._id,
... version: oldConf.version + 1,
... members: [ oldConf.members[2] ]}
[...]
replset:SECONDARY> rs.reconfig(conf,{force:true})

Note: Instead of putting 2 at oldConf.members[2], you need to put in the position of the instance running on port 27013 in oldConfig.members, starting from 0. After you sent the rs.reconfig command, it may take a few seconds for the last node to come up as primary.

The sensible way

Get up two other nodes with the according --replSet parameter and use the procedure described above to add them by adding them to the members array of conf.

Upvotes: 4

Sammaye
Sammaye

Reputation: 43884

You have to reconfig the replica set: http://docs.mongodb.org/manual/tutorial/reconfigure-replica-set-with-unavailable-members/

Namely you have to actually go onto that member and run a rs.reconfig to remove those two dead members from the set since you have a majority of the members offline.

Upvotes: 4

Related Questions