1.初始化一个仲裁节点
--初始化仲裁节点。
mongod --bind_ip 192.168.1.51,127.0.0.1 --port 27027 --dbpath /mysql/mongodb/db1/shard5/data -shardsvr --replSet=shard1 &
2.将仲裁节点加入复制集
--登陆副本集的主节点。
mongo --port 27025 (主节点)
rs.addArb("192.168.1.51:27027")
{
"ok" : 1,
"operationTime" : Timestamp(1718444080, 1),
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1718444080, 1),
"t" : NumberLong(2)
},
"electionId" : ObjectId("7fffffff0000000000000002")
},
"lastCommittedOpTime" : Timestamp(1718444073, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1718444079, 2),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1718444080, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2024-06-15T09:35:52.724Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718444101, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.1.51:27023",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 42012,
"optime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-15T09:35:43Z"),
"optimeDurableDate" : ISODate("2024-06-15T09:35:43Z"),
"lastHeartbeat" : ISODate("2024-06-15T09:35:52.535Z"),
"lastHeartbeatRecv" : ISODate("2024-06-15T09:35:51.540Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.1.51:27025",
"syncSourceHost" : "192.168.1.51:27025",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 1,
"name" : "192.168.1.51:27024",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 42012,
"lastHeartbeat" : ISODate("2024-06-15T09:35:52.535Z"),
"lastHeartbeatRecv" : ISODate("2024-06-15T09:35:52.537Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "192.168.1.51:27025",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 42190,
"optime" : {
"ts" : Timestamp(1718444143, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-15T09:35:43Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1718402158, 1),
"electionDate" : ISODate("2024-06-14T21:55:58Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 3,
"name" : "192.168.1.51:27027",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 72,
"lastHeartbeat" : ISODate("2024-06-15T09:35:52.537Z"),
"lastHeartbeatRecv" : ISODate("2024-06-15T09:35:52.552Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1718444143, 1),
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1718444080, 1),
"t" : NumberLong(2)
},
"electionId" : ObjectId("7fffffff0000000000000002")
},
"lastCommittedOpTime" : Timestamp(1718444143, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1718444146, 1),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1718444146, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--现在变长了,一主一从2仲裁。
--shard里面没有变化,因为没有添加数据集,添加的是仲裁节点。仲裁节点没在shard中配置。
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("666cc96736506823d13a932c")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.1.51:27023,192.168.1.51:27025", "state" : 1 }
active mongoses:
"4.0.2" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
3.删除仲裁节点
--删除仲裁节点
shard1:PRIMARY> rs.remove("192.168.1.51:27027")
{
"ok" : 1,
"operationTime" : Timestamp(1718444306, 1),
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1718444306, 1),
"t" : NumberLong(2)
},
"electionId" : ObjectId("7fffffff0000000000000002")
},
"lastCommittedOpTime" : Timestamp(1718444303, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1718444296, 1),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1718444306, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--现在又变回1主一从一仲裁的状态了。
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2024-06-15T09:39:32.627Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718444323, 2),
"members" : [
{
"_id" : 0,
"name" : "192.168.1.51:27023",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 42232,
"optime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-15T09:39:23Z"),
"optimeDurableDate" : ISODate("2024-06-15T09:39:23Z"),
"lastHeartbeat" : ISODate("2024-06-15T09:39:32.261Z"),
"lastHeartbeatRecv" : ISODate("2024-06-15T09:39:31.272Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.1.51:27025",
"syncSourceHost" : "192.168.1.51:27025",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 7
},
{
"_id" : 1,
"name" : "192.168.1.51:27024",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 42232,
"lastHeartbeat" : ISODate("2024-06-15T09:39:32.261Z"),
"lastHeartbeatRecv" : ISODate("2024-06-15T09:39:32.260Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 7
},
{
"_id" : 2,
"name" : "192.168.1.51:27025",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 42410,
"optime" : {
"ts" : Timestamp(1718444363, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-15T09:39:23Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1718402158, 1),
"electionDate" : ISODate("2024-06-14T21:55:58Z"),
"configVersion" : 7,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"operationTime" : Timestamp(1718444363, 1),
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1718444306, 1),
"t" : NumberLong(2)
},
"electionId" : ObjectId("7fffffff0000000000000002")
},
"lastCommittedOpTime" : Timestamp(1718444363, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1718444367, 1),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1718444367, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4.总结
添加仲裁节点通过:rs.addArb("IP:PORT"),删除仲裁节点的命令和删除普通数据节点的命令相同。
rs.remove("IP:PORT"); 仲裁节点,可以随时添加和删除。