参考:https://blog.51cto.com/caiyuanji
副本集维护:
副本集成员最多12个成员,其中只有7个成员拥有投票权。这是为了减少 心跳请求的网络流量和选举话费的时间。心跳每2秒发送一次。
仲裁者唯一的作用就是参与选举,仲裁者并不保存数据,也不会为客户端提供服务。成员一旦以仲裁者的身份加入副本集中,它就永远只能是仲裁者,无法将仲裁者重新配置为非仲裁者,反之亦然。最多只能有一个仲裁者每个副本集中。
修改副本集成员的优先级:
conf=rs.conf() #将rs.conf的值定义给conf
test:PRIMARY> conf.members[0].priority=3
3
test:PRIMARY> conf.members[1].priority=2
2
#这里[0]并不是rs.status()里的id号,而是谁在第一位谁就是0,从0开始。
test:PRIMARY> rs.reconfig(conf)
test:PRIMARY> rs.conf()
{
"_id" : "test",
"version" : 2,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "zhuzhu:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "gougou:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 90,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5c2da4fc134c17137419c065")
}
}
主从切换成功
关于优先级范围从0 至 1000 (3.2以上版本) ,优先级越大,越可能成为primary;若优先级为0,则该节点无资格参与primary的选举。
如果希望某节点不参与primary的选举有两种方法: 1、将该节点优先级设置为0;2、rs.freeze(30) 在cluster选举primary时,将该节点暂时冻结30S。
如果希望将本primary节点降为secondary状态,可以执行 rs.stepDown() 。
在副本集中添加和删除成员:
test:PRIMARY> rs.status() #查看副本集状态
{
"set" : "test",
"date" : ISODate("2019-01-07T03:21:06.774Z"),
"myState" : 1,
"term" : NumberLong(13),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"appliedOpTime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"durableOpTime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1546831238, 1),
"members" : [
{
"_id" : 0,
"name" : "zhuzhu:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1830,
"optime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"optimeDate" : ISODate("2019-01-07T03:21:03Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1546830032, 1),
"electionDate" : ISODate("2019-01-07T03:00:32Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "gougou:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1251,
"optime" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"optimeDurable" : {
"ts" : Timestamp(1546831263, 1),
"t" : NumberLong(13)
},
"optimeDate" : ISODate("2019-01-07T03:21:03Z"),
"optimeDurableDate" : ISODate("2019-01-07T03:21:03Z"),
"lastHeartbeat" : ISODate("2019-01-07T03:21:05.954Z"),
"lastHeartbeatRecv" : ISODate("2019-01-07T03:21:05.060Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "zhuzhu:27017",
"syncSourceHost" : "zhuzhu:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"operationTime" : Timestamp(1546831263, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1546831263, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:PRIMARY> rs.remove("gougou:27017") #移除成员
{
"ok" : 1,
"operationTime" : Timestamp(1546831380, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1546831380, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:PRIMARY> rs.add("gougou:27017") #添加成员
{
"ok" : 1,
"operationTime" : Timestamp(1546831434, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1546831434, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:PRIMARY> rs.conf() #查看副本集的优先级
{
"_id" : "test",
"version" : 4,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "zhuzhu:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "gougou:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5c2da4fc134c17137419c065")
}