【MongoDB 高可用篇】MongoDB 副本集添加新成员
目录
副本集(replica set)是一组mongod进程维护的相同的数据集,提供了MongoDB的冗余和高可用,本篇将对向现有副本集环境中如何增加新成员进行演示。
1 软件环境
使用的软件分别为:
- VirtualBox 6.0
- Oracle Linux 6.7
- MongoDB 4.2.0
2 新增副本集成员
2.1 创建新成员mongod实例
参照:安装MongoDB单实例
2.2 启动新成员
[mongod@sec02 ~]$ mongod -f /u01/data/conf/mongod.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3156
child process started successfully, parent exiting
2.3 连接到主库
[mongod@sec conf]$ mongo localhost:27017/admin -uroot -p
MongoDB shell version v4.2.0
Enter password:
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("94500bee-57dc-4b9a-9da7-63760d8093dd") }
MongoDB server version: 4.2.0
Server has startup warnings:
2019-10-02T20:45:51.262+0800 I STORAGE [initandlisten]
2019-10-02T20:45:51.262+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-10-02T20:45:51.262+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
rep1:PRIMARY>
如果无法判断主库,可使用命令rs.isMaster()进行判断。
2.4 增加新成员
rep1:PRIMARY> rs.add({host:'192.168.56.105:27017',priority:0,votes:0})
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570025295, 1),
"signature" : {
"hash" : BinData(0,"H8Y5YDKvxwIEJNrdwJVehF+Eqys="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570025295, 1)
}
2.5 查看副本集状态
通过下面的输出看到,新成员已经变为从库了。
rep1:PRIMARY> rs.status()
{
"set" : "rep1",
"date" : ISODate("2019-10-02T14:10:32.066Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"lastCommittedWallTime" : ISODate("2019-10-02T14:10:30.449Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"readConcernMajorityWallTime" : ISODate("2019-10-02T14:10:30.449Z"),
"appliedOpTime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"lastAppliedWallTime" : ISODate("2019-10-02T14:10:30.449Z"),
"lastDurableWallTime" : ISODate("2019-10-02T14:10:30.449Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.56.102:27017",
"ip" : "192.168.56.102",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3601,
"optime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-10-02T14:10:30Z"),
"optimeDurableDate" : ISODate("2019-10-02T14:10:30Z"),
"lastHeartbeat" : ISODate("2019-10-02T14:10:31.753Z"),
"lastHeartbeatRecv" : ISODate("2019-10-02T14:10:31.965Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.56.103:27017",
"syncSourceHost" : "192.168.56.103:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 1,
"name" : "192.168.56.103:27017",
"ip" : "192.168.56.103",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5081,
"optime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-10-02T14:10:30Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1570021578, 1),
"electionDate" : ISODate("2019-10-02T13:06:18Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.56.104:27017",
"ip" : "192.168.56.104",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 4814,
"lastHeartbeat" : ISODate("2019-10-02T14:10:31.753Z"),
"lastHeartbeatRecv" : ISODate("2019-10-02T14:10:31.896Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 3,
"name" : "192.168.56.105:27017",
"ip" : "192.168.56.105",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1570025430, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-10-02T14:10:30Z"),
"optimeDurableDate" : ISODate("2019-10-02T14:10:30Z"),
"lastHeartbeat" : ISODate("2019-10-02T14:10:31.950Z"),
"lastHeartbeatRecv" : ISODate("2019-10-02T14:10:31.553Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.56.103:27017",
"syncSourceHost" : "192.168.56.103:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570025430, 1),
"signature" : {
"hash" : BinData(0,"SEuYl3Z7htW2Ee4tKJX+gI8LKAY="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570025430, 1)
}
2.6 重新配置新成员
rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members[3].priority=1
1
rep1:PRIMARY> cfg.members[3].votes=1
1
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570025779, 1),
"signature" : {
"hash" : BinData(0,"m2dVCsofNGYTAz1zWeHh3EWjCmQ="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570025779, 1)
}
2.7 查看最新配置
rep1:PRIMARY> rs.conf()
{
"_id" : "rep1",
"version" : 3,
"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")
}
}
至此,副本集添加新成员完成。