【MongoDB 高可用篇】MongoDB 副本集成员配置

目录

1 调整副本集成员的优先级

2 阻止辅助成员变为主成员

3 配置隐藏的副本集成员

4 配置延迟的副本集成员

5 配置非投票副本集成员

6 辅助成员转换为仲裁成员

6.1 使用原端口进行转换

6.2 使用新端口进行转换


一个MongoDB副本集群中,包含主成员、辅助成员和仲裁成员,成员的相关配置不相同,则其承担的角色也不同,本篇就副本集中成员的相关配置做介绍。

1 调整副本集成员的优先级

设置副本集成员的优先级,其实就是设置priority选项,该选项的默认值是1,其值大小可以是0到1000之间的浮点数,该值越高,则优先级越高,越可能发起选举,并在投票选举中获胜。若要阻止成员竞选为主成员,请将其优先级设为0,对于隐藏成员和延迟成员,其优先级为0。下面将演示如何设置成员的优先级(将192.168.56.105的优先级设置为2):

1)查看副本集的配置

rep1:SECONDARY> rs.conf()
{
"_id" : "rep1",
"version" : 9,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.56.102:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.56.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.56.104:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "192.168.56.105: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("5d949d0840e7ee7b725ee3c6")
}
}

2)设置192.168.56.105的优先级设置为2

rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[3].priority=2
2
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570199944, 1),
"signature" : {
"hash" : BinData(0,"O3myCF0IpfaCTOnESLt3ve6g0wg="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570199944, 1)
}

2 阻止辅助成员变为主成员

副本集中,默认情况下,所有的辅助成员都可以通过选举变为主成员。可以通过设置priority选项提高辅助成员变为主成员的可能性,也可以将其设置为不能变为主成员。若辅助成员不能变为主成员,那么它也不会触发选举。阻止辅助成员在故障切换时变为主成员,可将辅助成员的priority设置为0,具体如下:

rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[1].priority=0
0
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570200448, 1),
"signature" : {
"hash" : BinData(0,"WFcKaR5cA0Z6tnhvUGOogKvtdzY="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570200448, 1)
}

3 配置隐藏的副本集成员

隐藏成员是副本集的一部分,但是它不能变为主成员,并且对客户端应用来说是不可见的,隐藏成员可以进行选举。隐藏成员最常见的用途是支持延迟成员,如果仅仅是防止成员成为主成员,则将其优先级配置为0即可。如果配置辅助成员为隐藏成员,设置priority选项为0,并且设置hidden选项为true,具体如下:

rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[1].priority=0
0
rep1:PRIMARY> cfg.members[1].hidden=true
true
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570200962, 1),
"signature" : {
"hash" : BinData(0,"/gBHCyXfHmPMY1t6NzFDUP36iDE="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570200962, 1)
}

4 配置延迟的副本集成员

配置成员为延迟成员,需设置priority选项为0,hidden选项为true,并且设置slaveDelay的值为延迟的秒数。具体配置如下:

rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[0].priority=0
0
rep1:PRIMARY> cfg.members[0].hidden=true
true
rep1:PRIMARY> cfg.members[0].slaveDelay=3600
3600
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570201472, 1),
"signature" : {
"hash" : BinData(0,"IGaYScsCCVRhxxZTZWLvNypw/Y0="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570201472, 1)
}

5 配置非投票副本集成员

对于非投票成员,可用于对主成员读操作的分流,通过设置votes选项和priority选项为0。具体如下:

rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[0].priority=0
0
rep1:PRIMARY> cfg.members[0].votes=0
0
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570202395, 1),
"signature" : {
"hash" : BinData(0,"dt0+p4kXSt7Uha2hq+uUCO4bDOQ="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570202395, 1)
}

6 辅助成员转换为仲裁成员

如果辅助成员不再需要用于存放数据,但需要它用于投票选举主成员,那么可以将其转换为仲裁成员,方法有两种,分别是:

  • 使用原端口用于仲裁成员的端口;
  • 使用新端口用于仲裁成员的端口;

6.1 使用原端口进行转换

1)关闭辅助成员

rep1:SECONDARY> db.shutdownServer()
2019-10-04T23:53:55.651+0800 I NETWORK [js] DBClientConnection failed to receive message from localhost:27017 - HostUnreachable: Connection closed by peer
server should be down...
2019-10-04T23:53:55.655+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-04T23:53:55.656+0800 I NETWORK [js] reconnect localhost:27017 failed failed
>

2)移除辅助成员

rep1:PRIMARY> rs.remove('192.168.56.102:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570204479, 1),
"signature" : {
"hash" : BinData(0,"M6ES27JcvC5eX2//+gf+VD8+e6c="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570204479, 1)
}

3)执行rs.conf()验证辅助成员已删除

rep1:PRIMARY> rs.conf()
{
"_id" : "rep1",
"version" : 22,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "192.168.56.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.56.104:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "192.168.56.105:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"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("5d949d0840e7ee7b725ee3c6")
}
}

4)删除数据目录(这里重命名操作)

[mongod@pri ~]$ mv /u01/data/db /u01/data/db_old

5)创建新的数据目录

[mongod@pri ~]$ mkdir /u01/data/db

6)重启mongod实例

[mongod@pri ~]$ mongod -f /u01/data/conf/mongod.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 6385
child process started successfully, parent exiting

7)增加仲裁成员

rep1:PRIMARY> rs.addArb('192.168.56.102:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570204786, 1),
"signature" : {
"hash" : BinData(0,"AhtUNpWLngIE6Y5n36S9MlI2h28="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570204786, 1)
}

8)验证仲裁成员已添加

{
"_id" : 4,
"host" : "192.168.56.102:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}

6.2 使用新端口进行转换

1)创建临时数据目录

[mongod@pri ~]$ mkdir /u01/data/db_temp

2)在新端口启动新mongod实例

[mongod@pri ~]$ mongod --port 27018 --dbpath /u01/data/db_temp --replSet rep1 --bind_ip localhost,192.168.56.102 --keyFile /u01/data/conf/keyfile

3)连接到当前主库,执行命令

rep1:PRIMARY> rs.addArb('192.168.56.102:27018')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570203363, 1),
"signature" : {
"hash" : BinData(0,"T4dglvnrv04rI+Psebo8wKmlzpA="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570203363, 1)
}

4)执行rs.conf()验证仲裁成员已增加

{
"_id" : 4,
"host" : "192.168.56.102:27018",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}

5)关闭辅助成员

rep1:SECONDARY> db.shutdownServer()
2019-10-04T23:39:20.597+0800 I NETWORK [js] DBClientConnection failed to receive message from localhost:27017 - HostUnreachable: Connection closed by peer
server should be down...
2019-10-04T23:39:20.614+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-04T23:39:20.615+0800 I NETWORK [js] reconnect localhost:27017 failed failed
2019-10-04T23:39:20.618+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-04T23:39:20.618+0800 I NETWORK [js] reconnect localhost:27017 failed failed
>

6)移除辅助成员

rep1:PRIMARY> rs.remove('192.168.56.102:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570203893, 1),
"signature" : {
"hash" : BinData(0,"9FEGChxZgAiVFc/Xzw0tlGYlpZ8="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570203893, 1)
}

7)移除辅助成员数据目录

[mongod@pri ~]$ mv /u01/data/db /u01/data/db_old

以上,就是MongoDB副本集成员相关的配置。

 

参考:https://docs.mongodb.com/manual/core/views/

posted @ 2019-10-05 08:57  追梦男生  阅读(581)  评论(0编辑  收藏  举报