snippetMinor
How to set a mongodb node to return as the primary of a replication set?
Viewed 0 times
theprimaryreturnnodemongodbreplicationhowset
Problem
There is this question here:
How to force a mongod to become primary in a replica set?
But it was not the answer I was looking for.
and there is the question below, which could be related but it is not the same.
How to force a delayed member to become primary in case of failure in mongodb
and this question which is nearly a duplicate, but deals with version 2.6.11 and mine is version 3.0 and 3.2.
Plus I don't want to use force : 1 unless I need to.
Force a Member to Become Primary in mongodb
I have been struggling to put in practice the majority described in the answer there, maybe with an example or clarification, therefore this question.
I had my replication working fine, 3 nodes, node1 is the primary.
when I run the following command
I get this result:
```
{
"set" : "Krishna",
"date" : ISODate("2016-04-26T14:59:40.263Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "jpb01275:37001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1022,
"optime" : Timestamp(1461678841, 1),
"optimeDate" : ISODate("2016-04-26T13:54:01Z"),
"electionTime" : Timestamp(1461682464, 1),
"electionDate" : ISODate("2016-04-26T14:54:24Z"),
"configVersion" : 239068,
"self" : true
},
{
"_id" : 1,
"name" : "jpb01275:37002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1022,
"optime" : Timestamp(1461678841, 1),
"optimeDate" : ISODate("2016-04-26T13:54:01Z"),
"lastHeartbeat" : ISODate("2016-04-26T14:59:40.206Z"),
"lastHeartbeatRecv" : ISODate("2016-04-26T14:59:40.179Z"),
"pingMs" : 0,
"configVersion" : 2
How to force a mongod to become primary in a replica set?
But it was not the answer I was looking for.
and there is the question below, which could be related but it is not the same.
How to force a delayed member to become primary in case of failure in mongodb
and this question which is nearly a duplicate, but deals with version 2.6.11 and mine is version 3.0 and 3.2.
Plus I don't want to use force : 1 unless I need to.
Force a Member to Become Primary in mongodb
I have been struggling to put in practice the majority described in the answer there, maybe with an example or clarification, therefore this question.
I had my replication working fine, 3 nodes, node1 is the primary.
when I run the following command
//-----------------------------
//check the status of the replication
//-----------------------------
rs.status()I get this result:
```
{
"set" : "Krishna",
"date" : ISODate("2016-04-26T14:59:40.263Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "jpb01275:37001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1022,
"optime" : Timestamp(1461678841, 1),
"optimeDate" : ISODate("2016-04-26T13:54:01Z"),
"electionTime" : Timestamp(1461682464, 1),
"electionDate" : ISODate("2016-04-26T14:54:24Z"),
"configVersion" : 239068,
"self" : true
},
{
"_id" : 1,
"name" : "jpb01275:37002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1022,
"optime" : Timestamp(1461678841, 1),
"optimeDate" : ISODate("2016-04-26T13:54:01Z"),
"lastHeartbeat" : ISODate("2016-04-26T14:59:40.206Z"),
"lastHeartbeatRecv" : ISODate("2016-04-26T14:59:40.179Z"),
"pingMs" : 0,
"configVersion" : 2
Solution
Typically, you don't. Thinking of your nodes in terms of primary or secondary is the wrong way to approach it. Since standard data bearing nodes should have the same dimensions, it is better to think of them as replica set members, one of which gets elected to primary. Since the drivers are replica set aware and know which the current primary is and you are supposed to give more than one server in your connection string like
there is no advantage in setting a fixed replica set primary.
If you really have to do it, you need to do an
on the current primary and repeat that until your chosen node becomes primary, within 90 seconds (the first numeric value set). For details, see the documentation of
mongodb://jpb01275:37001,jpb01275:37002/?replicaSet=Krishnathere is no advantage in setting a fixed replica set primary.
If you really have to do it, you need to do an
rs.stepDown(90,30)on the current primary and repeat that until your chosen node becomes primary, within 90 seconds (the first numeric value set). For details, see the documentation of
rs.stepDown().Code Snippets
mongodb://jpb01275:37001,jpb01275:37002/?replicaSet=Krishnars.stepDown(90,30)Context
StackExchange Database Administrators Q#136621, answer score: 5
Revisions (0)
No revisions yet.