Maintenance of mongodb replica set (1)
1. Modify the priority of each member in the replica set:
Shard1:PRIMARY > conf=rs.conf ()
{
"_ id": "shard1"
"version": 3
"protocolVersion": NumberLong (1)
"members": [
{
"_ id": 0
"host": "mongo01-jp:27027"
"arbiterOnly": false
"buildIndexes": true
"hidden": false
"priority": 1
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
{
"_ id": 1
"host": "mongo02-jp:27027"
"arbiterOnly": false
"buildIndexes": true
"hidden": false
"priority": 1
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
{
"_ id": 2
"host": "mongo03-jp:27027"
"arbiterOnly": true
"buildIndexes": true
"hidden": false
"priority": 1
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
]
"settings": {
"chainingAllowed": true
"heartbeatIntervalMillis": 2000
"heartbeatTimeoutSecs": 10
"electionTimeoutMillis": 10000
"catchUpTimeoutMillis": 2000
"getLastErrorModes": {
}
"getLastErrorDefaults": {
"w": 1
"wtimeout": 0
}
"replicaSetId": ObjectId ("5850deb0205cd94104cd9a38")
}
}
Shard1:PRIMARY > conf.members [0] .priority = 100
one hundred
Shard1:PRIMARY > conf.members [1] .priority = 90
ninety
Shard1:PRIMARY > rs.reconfig (conf)
{"ok": 1}
Shard1:PRIMARY > rs.conf ()
{
"_ id": "shard1"
"version": 4
"protocolVersion": NumberLong (1)
"members": [
{
"_ id": 0
"host": "mongo01-jp:27027"
"arbiterOnly": false
"buildIndexes": true
"hidden": false
"priority":
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
{
"_ id": 1
"host": "mongo02-jp:27027"
"arbiterOnly": false
"buildIndexes": true
"hidden": false
"priority": 90
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
{
"_ id": 2
"host": "mongo03-jp:27027"
"arbiterOnly": true
"buildIndexes": true
"hidden": false
"priority": 1
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
]
"settings": {
"chainingAllowed": true
"heartbeatIntervalMillis": 2000
"heartbeatTimeoutSecs": 10
"electionTimeoutMillis": 10000
"catchUpTimeoutMillis": 2000
"getLastErrorModes": {
}
"getLastErrorDefaults": {
"w": 1
"wtimeout": 0
}
"replicaSetId": ObjectId ("5850deb0205cd94104cd9a38")
}
}
Shard1:PRIMARY >
With regard to the priority range from 0 to 1000 (version 3.2 and above), the higher the priority, the more likely it is to become a primary;. If the priority is 0, the node is not eligible to participate in the primary election.
If you want a node not to participate in the election of primary, there are two ways: 1, set the priority of the node to 0fie2, and rs.freeze (30) temporarily freeze 30s when cluster elects primary.
If you want to reduce this primary node to the secondary state, you can execute rs.stepDown ().
Delete members and re-add members in the replica set
Shard2:PRIMARY > rs.status ()
{
"set": "shard2"
Date: ISODate ("2016-12-14T06:12:11.986Z")
"myState": 1
"term": NumberLong (1)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
"appliedOpTime": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
"durableOpTime": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
}
"members": [
{
"_ id": 0
"name": "mongo02-jp:27028"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 1128
"optime": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2016-12-14T06:12:06Z")
ElectionTime: Timestamp (1481695445, 2)
ElectionDate: ISODate ("2016-12-14T06:04:05Z")
"configVersion": 4
"self": true
}
{
"_ id": 1
"name": "mongo01-jp:27028"
"health": 1
"state": 2
"stateStr": "SECONDARY"
Uptime: 464
"optime": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1481695926, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2016-12-14T06:12:06Z")
OptimeDurableDate: ISODate ("2016-12-14T06:12:06Z")
LastHeartbeat: ISODate ("2016-12-14T06:12:10.206Z")
LastHeartbeatRecv: ISODate ("2016-12-14T06:12:11.202Z")
"pingMs": NumberLong (0)
"syncingTo": "mongo02-jp:27028"
"configVersion": 4
}
{
"_ id": 2
"name": "mongo03-jp:27028"
"health": 1
"state": 7
"stateStr": "ARBITER"
Uptime: 439
LastHeartbeat: ISODate ("2016-12-14T06:12:10.205Z")
LastHeartbeatRecv: ISODate ("2016-12-14T06:12:10.125Z")
"pingMs": NumberLong (0)
"configVersion": 4
}
]
"ok": 1
}
Shard2:PRIMARY > rs.remove ("mongo01-jp:27028")
{"ok": 1}
Shard2:PRIMARY > rs.remove ("mongo03-jp:27028")
{"ok": 1}
Shard2:PRIMARY > rs.status ()
{
"set": "shard2"
Date: ISODate ("2016-12-14T06:13:18.227Z")
"myState": 1
"term": NumberLong (1)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1481695994, 1)
"t": NumberLong (1)
}
"appliedOpTime": {
Ts: Timestamp (1481695994, 1)
"t": NumberLong (1)
}
"durableOpTime": {
Ts: Timestamp (1481695994, 1)
"t": NumberLong (1)
}
}
"members": [
{
"_ id": 0
"name": "mongo02-jp:27028"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 1195
"optime": {
Ts: Timestamp (1481695994, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2016-12-14T06:13:14Z")
ElectionTime: Timestamp (1481695445, 2)
ElectionDate: ISODate ("2016-12-14T06:04:05Z")
"configVersion": 6
"self": true
}
]
"ok": 1
}
Shard2:PRIMARY > rs.add ("mongo03-jp:27028")
{"ok": 1}
Shard2:PRIMARY > rs.addArb ("mongo01-jp:27028")
{"ok": 1}
Shard2:PRIMARY > rs.status ()
{
"set": "shard2"
Date: ISODate ("2016-12-14T06:39:40.718Z")
"myState": 1
"term": NumberLong (1)
"heartbeatIntervalMillis": NumberLong (2000)
"optimes": {
"lastCommittedOpTime": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
"appliedOpTime": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
"durableOpTime": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
}
"members": [
{
"_ id": 0
"name": "mongo02-jp:27028"
"health": 1
"state": 1
"stateStr": "PRIMARY"
"uptime": 2777
"optime": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2016-12-14T06:39:36Z")
ElectionTime: Timestamp (1481695445, 2)
ElectionDate: ISODate ("2016-12-14T06:04:05Z")
"configVersion": 9
"self": true
}
{
"_ id": 1
"name": "mongo03-jp:27028"
"health": 1
"state": 2
"stateStr": "SECONDARY"
"uptime": 1034
"optime": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
"optimeDurable": {
Ts: Timestamp (1481697576, 1)
"t": NumberLong (1)
}
OptimeDate: ISODate ("2016-12-14T06:39:36Z")
OptimeDurableDate: ISODate ("2016-12-14T06:39:36Z")
LastHeartbeat: ISODate ("2016-12-14T06:39:38.838Z")
LastHeartbeatRecv: ISODate ("2016-12-14T06:39:39.594Z")
"pingMs": NumberLong (0)
"syncingTo": "mongo02-jp:27028"
"configVersion": 9
}
{
"_ id": 2
"name": "mongo01-jp:27028"
"health": 1
"state": 7
"stateStr": "ARBITER"
"uptime": 1128
LastHeartbeat: ISODate ("2016-12-14T06:39:39.053Z")
LastHeartbeatRecv: ISODate ("2016-12-14T06:39:36.708Z")
"pingMs": NumberLong (0)
"configVersion": 9
}
]
"ok": 1
}