1、副本集部署教程
1.1、要求
三个成员副本集提供足够的冗余以承受大多数网络分区和其他系统故障。这些副本集也具有足够的容量用于许多分布式读取操作。副本集应始终具有奇数个成员。这可确保选举顺利进行。对于生产部署,应该通过在不同的计算机上托管mongod实例来尽可能多地保持成员之间的分离。当使用虚拟机进行生产部署时,应将每个mongod实例放在由冗余电源电路和冗余网络路径服务的单独host服务器上。
1.2、注意事项
- 架构体系:在生产环境,将副本集的每个成员部署到自己的机器上,并尽可能绑定到mongod的标准端口27017上。
- 主机名称:如果可能,在部署副本集或分配集群时,使用逻辑域名代替ip地址。使用域名代替ip可以防止ip改变时配置的更改。
- ip绑定:使用bind_ip选项来保证mongod监听来自指定ip的应用的连接。默认情况下,绑定的是本地localhost,此时mongod只接受本地的mongo的shell、副本集或分片集群的其他成员的连接。若需要绑定其他ip地址,可以使用net.bindIp配置或使用bind_ip命令行来指定主机名或ip地址列表。
1.3、副本集部署
1.3.1、数据目录创建
创建如下三个目录:用于保存mongod数据及日志等。
"C:\mongo\data27017"
"C:\mongo\data27018"
"C:\mongo\data27019"
1.3.1、启动mongod实例
./mongod.exe --replSet mongoset --dbpath "C:\mongo\data27017" --port 27017 --logpath "C:\mongo\data27017\log.log"
./mongod.exe --replSet mongoset --dbpath "C:\mongo\data27018" --port 27018 --logpath "C:\mongo\data27018\log.log"
./mongod.exe --replSet mongoset --dbpath "C:\mongo\data27019" --port 27019 --logpath "C:\mongo\data27019\log.log"
1.3.2、初始副本集
mongoset:PRIMARY> rsconf = {
... _id: "mongoset",
... members: [{
... _id: 0,
... host: "localhost:27017"
... }, {
... _id: 1,
... host: "localhost:27018"
... }, {
... _id: 2,
... host: "localhost:27019"
... }]
... }
{
"_id" : "mongoset",
"members" : [
{
"_id" : 0,
"host" : "localhost:27017"
},
{
"_id" : 1,
"host" : "localhost:27018"
},
{
"_id" : 2,
"host" : "localhost:27019"
}
]
}
mongoset:PRIMARY> rs.initiate(rsconf)
{
"operationTime" : Timestamp(1577706032, 1),
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(1577706032, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongoset:PRIMARY>
1.3.3、查看副本集状态
mongoset:PRIMARY> rs.conf()
{
"_id" : "mongoset",
"version" : 29603,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 3,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27019",
"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("5e01feb0815cdc9cd73d4fca")
}
}
mongoset:PRIMARY>
mongoset:PRIMARY> rs.status()
{
"set" : "mongoset",
"date" : ISODate("2019-12-30T11:43:31.227Z"),
"myState" : 1,
"term" : NumberLong(14),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"lastCommittedWallTime" : ISODate("2019-12-30T11:43:22.516Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"readConcernMajorityWallTime" : ISODate("2019-12-30T11:43:22.516Z"),
"appliedOpTime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"durableOpTime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"lastAppliedWallTime" : ISODate("2019-12-30T11:43:22.516Z"),
"lastDurableWallTime" : ISODate("2019-12-30T11:43:22.516Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1577706172, 1),
"lastStableCheckpointTimestamp" : Timestamp(1577706172, 1),
"members" : [
{
"_id" : 0,
"name" : "localhost:27017",
"ip" : "127.0.0.1",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 519,
"optime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"optimeDate" : ISODate("2019-12-30T11:43:22Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1577705751, 1),
"electionDate" : ISODate("2019-12-30T11:35:51Z"),
"configVersion" : 29603,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "localhost:27018",
"ip" : "127.0.0.1",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 463,
"optime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"optimeDurable" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"optimeDate" : ISODate("2019-12-30T11:43:22Z"),
"optimeDurableDate" : ISODate("2019-12-30T11:43:22Z"),
"lastHeartbeat" : ISODate("2019-12-30T11:43:31.127Z"),
"lastHeartbeatRecv" : ISODate("2019-12-30T11:43:29.872Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27017",
"syncSourceHost" : "localhost:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 29603
},
{
"_id" : 2,
"name" : "localhost:27019",
"ip" : "127.0.0.1",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 446,
"optime" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"optimeDurable" : {
"ts" : Timestamp(1577706202, 1),
"t" : NumberLong(14)
},
"optimeDate" : ISODate("2019-12-30T11:43:22Z"),
"optimeDurableDate" : ISODate("2019-12-30T11:43:22Z"),
"lastHeartbeat" : ISODate("2019-12-30T11:43:31.127Z"),
"lastHeartbeatRecv" : ISODate("2019-12-30T11:43:30.220Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27018",
"syncSourceHost" : "localhost:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 29603
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1577706202, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1577706202, 1)
}
mongoset:PRIMARY>
2、副本集配置
2.1、调整节点优先级
副本集成员的priority设置会影响主要的选举的时间和结果。高优先级成员更有可能召集选举,更有可能获胜。使用此设置可确保某些成员更有可能成为主要成员,而其他成员永远不会成为主要成员。成员的优先级priority的 value 决定了选举中成员的优先。数字越大,优先级越高。
priority的值可以是0-1000之间的任何浮点数,其默认值为1。若有阻止成员选举为主节点,可将其优先级设置为0。隐藏及延迟成员将优先级设置0。非选举节点优先级必须为0,仲裁者优先级为0。
在计划的维护窗口更改优先级配置,将导致主节点强制断开所有连接,重新开始选举。
2.2、设置隐藏节点
如果settings.chainingAllowed设置允许辅助成员从其他辅助节点同步,则默认情况下 MongoDB 在选择同步目标时更喜欢隐藏成员上的 non-hidden 成员。 MongoDB 只会选择隐藏的成员作为最后的手段。如果希望辅助节点从隐藏成员同步,请使用replSetSyncFrom database 命令覆盖默认同步目标。
隐藏成员配置示例:
{
"_id" : <num>
"host" : <hostname:port>,
"priority" : 0,
"hidden" : true
}
cfg = rs.conf()
cfg.members[0].priority = 0
cfg.members[0].hidden = true
rs.reconfig(cfg)