1.关闭节点
myrelset:SECONDARY> use admin
switched to db admin
myrelset:SECONDARY> db.shutdownServer()
server should be down...
2024-06-14T16:33:20.178+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2024-06-14T16:33:20.178+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2024-06-14T16:33:20.182+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2024-06-14T16:33:20.182+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2.在主节点上移除节点
myrelset:PRIMARY> rs.remove("192.168.75.56:27017")
{
"ok" : 1,
"operationTime" : Timestamp(1718354107, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718354107, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
myrelset:PRIMARY> rs.config()
{
"_id" : "myrelset",
"version" : 2,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "192.168.75.56:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.75.56:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"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("666be69069003c1b2c0b51f7")
}
}
移除后,集群变成了两个节点,一主一从。
3.新增节点:新增的节点为原来的节点
myrelset:PRIMARY> use admin
switched to db admin
myrelset:PRIMARY> rs.add("192.168.75.56:27017")
{
"ok" : 1,
"operationTime" : Timestamp(1718354232, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718354232, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
myrelset:PRIMARY> rs.config()
{
"_id" : "myrelset",
"version" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "192.168.75.56:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.75.56:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "192.168.75.56: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("666be69069003c1b2c0b51f7")
}
}
4.我们增加一个以前没有的节点。
myrelset:PRIMARY> use test
switched to db test
myrelset:PRIMARY> for(var i=1;i<=20;i++) db.users.insert({"Name":"Test User"+i,"Age":10+i,"Gender":"F","Country":"India"})
WriteResult({ "nInserted" : 1 })
myrelset:PRIMARY> db.users.find()
{ "_id" : ObjectId("666c0189436fccf43e2a5f03"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f04"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f05"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f06"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f07"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f08"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f09"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0a"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0b"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0c"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0d"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0e"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0f"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f10"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f11"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f12"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f13"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f14"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f15"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f16"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "India" }
插入一些数据,新增一个空节点。
5.初始新节点
mongod --dbpath /mongodb/db4 --port 27020 --bind_ip 192.168.75.56,127.0.0.1 -replSet myrelset/192.168.75.56:27020 &
6.添加新节点到集群中
--添加新节点前集群状态检查。
myrelset:PRIMARY> use admin
switched to db admin
myrelset:PRIMARY> rs.config()
{
"_id" : "myrelset",
"version" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "192.168.75.56:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.75.56:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "192.168.75.56: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("666be69069003c1b2c0b51f7")
}
}
--添加新节点到集群中。
myrelset:PRIMARY> rs.add("192.168.75.56:27020")
{
"ok" : 1,
"operationTime" : Timestamp(1718354646, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718354646, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--检查添加新节点之后集群的状态。
myrelset:PRIMARY> rs.config()
{
"_id" : "myrelset",
"version" : 4,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 1,
"host" : "192.168.75.56:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.75.56:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "192.168.75.56:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 4,
"host" : "192.168.75.56:27020",
"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("666be69069003c1b2c0b51f7")
}
}
--集群的状态查看。
myrelset:PRIMARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T08:44:21.531Z"),
"myState" : 1,
"term" : NumberLong(5),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"appliedOpTime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"durableOpTime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718354608, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 105,
"optime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:44:18Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1718354566, 1),
"electionDate" : ISODate("2024-06-14T08:42:46Z"),
"configVersion" : 4,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.75.56:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 105,
"optime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:44:18Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:44:18Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:44:20.260Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:44:21.285Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 4
},
{
"_id" : 3,
"name" : "192.168.75.56:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 58,
"optime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:44:18Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:44:18Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:44:20.261Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:44:21.288Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 4
},
{
"_id" : 4,
"name" : "192.168.75.56:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15,
"optime" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354658, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:44:18Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:44:18Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:44:20.268Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:44:21.386Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 4
}
],
"ok" : 1,
"operationTime" : Timestamp(1718354658, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718354658, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}