Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB replica set no primary, need to force new primary

Tags:

mongodb

We have a mongoDB replica set which has 3 nodes;

  1. Primary
  2. Secondary
  3. Arbiter

Somehow our replica set has ended up with 1 & 2 both being set as secondary members. I'm not sure how this has happened (we have had a server migration which one of the nodes runs on, but only one).

Anyway, I've been trying to re-elect a new primary for the replica set following the guide here

I'm unable to just use

rs.reconfig(cfg)

as it will only work if directed at the primary (which I don't have).

Using the force parameter

rs.reconfig(cfg, { force: true})

would appear to work but then when I requery the status of the replica set, both servers are still only showing as Secondary.

Why hasn't the force reconfig worked? Currently the database is locked out whatever I try.

like image 900
obaylis Avatar asked Apr 07 '14 16:04

obaylis


People also ask

How do I force a primary in MongoDB?

You can force a replica set member to become primary by giving it a higher members[n]. priority value than any other member in the set. Optionally, you also can force a member never to become primary by setting its members[n]. priority value to 0 , which means the member can never seek election as primary.

How many primaries can a replica set have?

One node is the primary node in a replica, and it gets all write operations. All other instances, such as secondaries, use the primary's actions to ensure they have the same data set. There can only be one primary node in a replica set.

How does MongoDB replica set work?

Replication in MongoDBA replica set is a group of mongod instances that maintain the same data set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

What is difference between replica set and sharding in MongoDB?

What is the difference between replication and sharding? Replication: The primary server node copies data onto secondary server nodes. This can help increase data availability and act as a backup, in case if the primary server fails. Sharding: Handles horizontal scaling across servers using a shard key.


1 Answers

1.Convert all nodes to standalone.

Stop mongod deamon and edit /etc/mongod.conf to comment replSet option.

Start mongod deamon.

2.Use mongodump to backup data for all nodes.

Reference from mongo docs:

https://docs.mongodb.com/manual/reference/program/mongodump/

3.Log into each node, and drop local database.

Doing this will delete replica set config on the node.

Or you can just delete a record in collection system.replset in local db, like it said here:

https://stackoverflow.com/a/31745150/4242454

4.Start all nodes with replSet option.

5.On the previous data node (not arbiter), initialize a new replica set.

6.Finally, reconfig replica set with rs.reconfig.

like image 102
Kevin_wyx Avatar answered Nov 15 '22 03:11

Kevin_wyx