Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

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?

like image 351
Towhid Avatar asked Aug 30 '14 17:08

Towhid


2 Answers

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.

like image 149
Sammaye Avatar answered Oct 01 '22 21:10

Sammaye


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.

like image 45
Markus W Mahlberg Avatar answered Oct 01 '22 20:10

Markus W Mahlberg