1.降级前状态检查
myrelset:PRIMARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T08:47:33.459Z"),
"myState" : 1,
"term" : NumberLong(5),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"appliedOpTime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"durableOpTime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718354788, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 297,
"optime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:47:28Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"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" : 296,
"optime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:47:28Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:47:28Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:47:32.428Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:47:33.441Z"),
"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" : 250,
"optime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:47:28Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:47:28Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:47:32.428Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:47:33.440Z"),
"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" : 207,
"optime" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1718354848, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2024-06-14T08:47:28Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:47:28Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:47:32.461Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:47:31.501Z"),
"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(1718354848, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718354848, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
当前一主3从,主节点是27018,从节点是:27017,27019,27020;
2.执行降级操作 。
myrelset:PRIMARY> rs.stepDown()
2024-06-14T16:49:52.928+0800 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '192.168.75.56:27018' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:185:1
rs.stepDown@src/mongo/shell/utils.js:1433:12
@(shell):1:1
2024-06-14T16:49:52.931+0800 I NETWORK [js] trying reconnect to 192.168.75.56:27018 failed
2024-06-14T16:49:52.932+0800 I NETWORK [js] reconnect 192.168.75.56:27018 ok
--后台日志显示主节点现在是:27017
2024-06-14T16:49:52.927+0800 I COMMAND [conn12] Attempting to step down in response to replSetStepDown command
2024-06-14T16:49:52.927+0800 I REPL [conn12] transition to SECONDARY from PRIMARY
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 25
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 24
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 23
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 22
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 20
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 9
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 7
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 6
2024-06-14T16:49:52.928+0800 I NETWORK [conn12] Skip closing connection for connection # 3
2024-06-14T16:49:52.928+0800 I NETWORK [conn32] end connection 192.168.75.56:37061 (12 connections now open)
2024-06-14T16:49:52.928+0800 I NETWORK [conn38] end connection 192.168.75.56:37077 (11 connections now open)
2024-06-14T16:49:52.928+0800 I NETWORK [conn26] end connection 192.168.75.56:37047 (10 connections now open)
2024-06-14T16:49:52.930+0800 I REPL [conn12] Handing off election to 192.168.75.56:27017
2024-06-14T16:49:52.930+0800 I NETWORK [conn12] Error sending response to client: SocketException: Broken pipe. Ending connection from 192.168.75.56:36991 (connection id: 12)
2024-06-14T16:49:52.930+0800 I NETWORK [conn12] end connection 192.168.75.56:36991 (9 connections now open)
2024-06-14T16:49:52.932+0800 I NETWORK [listener] connection accepted from 192.168.75.56:37095 #44 (10 connections now open)
2024-06-14T16:49:52.932+0800 I NETWORK [conn44] received client metadata from 192.168.75.56:37095 conn44: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.2" }, os: { type: "Linux", name: "Red Hat Enterprise Linux Server release 7.9 (Maipo)", architecture: "x86_64", version: "Kernel 3.10.0-1160.el7.x86_64" } }
2024-06-14T16:49:54.557+0800 I REPL [replexec-9] Member 192.168.75.56:27017 is now in state PRIMARY
2024-06-14T16:49:55.945+0800 I REPL [rsBackgroundSync] sync source candidate: 192.168.75.56:27017
2024-06-14T16:49:55.945+0800 I ASIO [RS] Connecting to 192.168.75.56:27017
2024-06-14T16:49:55.947+0800 I REPL [rsBackgroundSync] Changed sync source from empty to 192.168.75.56:27017
3.集群状态检查
myrelset:SECONDARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T08:52:45.541Z"),
"myState" : 2,
"term" : NumberLong(6),
"syncingTo" : "192.168.75.56:27017",
"syncSourceHost" : "192.168.75.56:27017",
"syncSourceId" : 3,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718355164, 1),
"t" : NumberLong(6)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718355164, 1),
"t" : NumberLong(6)
},
"appliedOpTime" : {
"ts" : Timestamp(1718355164, 1),
"t" : NumberLong(6)
},
"durableOpTime" : {
"ts" : Timestamp(1718355164, 1),
"t" : NumberLong(6)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718355154, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 609,
"optime" : {
"ts" : Timestamp(1718355164, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2024-06-14T08:52:44Z"),
"syncingTo" : "192.168.75.56:27017",
"syncSourceHost" : "192.168.75.56:27017",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 4,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.75.56:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 609,
"optime" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDurable" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2024-06-14T08:52:34Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:52:34Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:52:44.162Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:52:43.723Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27017",
"syncSourceHost" : "192.168.75.56:27017",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 4
},
{
"_id" : 3,
"name" : "192.168.75.56:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 562,
"optime" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDurable" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2024-06-14T08:52:34Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:52:34Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:52:44.162Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:52:45.074Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1718354992, 1),
"electionDate" : ISODate("2024-06-14T08:49:52Z"),
"configVersion" : 4
},
{
"_id" : 4,
"name" : "192.168.75.56:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 519,
"optime" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDurable" : {
"ts" : Timestamp(1718355154, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2024-06-14T08:52:34Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:52:34Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:52:44.163Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:52:43.723Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27017",
"syncSourceHost" : "192.168.75.56:27017",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 4
}
],
"ok" : 1,
"operationTime" : Timestamp(1718355164, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718355164, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
myrelset:SECONDARY>
当前,27017是主节点,27018,27019,27020是从节点。
4.数据检查
myrelset:PRIMARY> use test
switched to db test
myrelset:PRIMARY> show tables;
users
myrelset:PRIMARY> db.users.find()
{ "_id" : ObjectId("666c0189436fccf43e2a5f03"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0b"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f11"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f13"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f06"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f10"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f07"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f12"), "Name" : "Test User16", "Age" : 26, "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("666c0189436fccf43e2a5f0e"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0c"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f14"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0f"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f04"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f0d"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f05"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f08"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f16"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666c0189436fccf43e2a5f15"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
myrelset:PRIMARY>
强制进行一次新的选举。
可以使用rs.stepDown()命令强制当前主服务器关闭。这一强制行为会启动新主节点的选举。
这个命令有如下作用。
1.当在模拟主节点故障时,可以强制集群进行故障转移。这样你就可以测试在这样的场景中
应用程序会如何响应。
2.当主服务器需要离线时,这样做要么是为了维护,要么是为了升级或者检查服务器。
3.当需要针对数据结构运行诊断程序时。