MongoDB添加仲裁节点报错:replica set IDs do not match的解决方法
MongoDB是一个常用的分布式数据库管理系统,具有高性能、高可用性、可扩展性等优点,在大型应用中广泛使用。MongoDB的复制集架构可以保证数据的高可用性,其中节点包括主节点、从节点和仲裁节点。在增加仲裁节点时,有时可能会遇到“replica set IDs do not match”错误。此时,需要进行相关的配置和操作才能成功添加仲裁节点。本文将详细介绍MongoDB添加仲裁节点出现“replica set IDs do not match”错误的原因和解决方法。
1. 错误原因
在MongoDB复制集架构中,每个节点都有一个唯一的ID,称为复制集ID。新节点加入时,需要使用相同的复制集ID。如果添加的节点复制集ID与现有节点的复制集ID不相同,则会出现“replica set IDs do not match”错误。
1.1 查看复制集ID
在MongoDB中,可以通过rs状态命令来查看当前复制集的状态和各节点的信息。在控制台输入以下命令:
rs.status()
会输出如下结果:
{
"set" : "rs0",
"date" : ISODate("2022-02-25T06:27:42.811Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : 2000,
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00.000Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"readConcernMajorityWallTime" : ISODate("1970-01-01T00:00:00.000Z"),
"appliedOpTime" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2022-02-25T06:27:42.173Z"),
"lastDurableWallTime" : ISODate("2022-02-25T06:27:42.173Z")
},
"lastStableRecoveryTimestamp" : Timestamp(0, 0),
"electionCandidateMetrics" : {
"lastElectionReason" : "stepUpRequestSkipDryRun",
"lastElectionDate" : ISODate("2022-02-25T06:27:39.623Z"),
"termAtElection" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1645741627, 1),
"t" : NumberLong(1)
},
"votesAhead" : 0,
"voters" : [
{
"_id" : 0,
"name" : "localhost:27017",
"lastVoteDate" : ISODate("2022-02-25T06:27:39.573Z")
},
{
"_id" : 2,
"name" : "localhost:27019",
"lastVoteDate" : ISODate("2022-02-25T06:27:39.333Z")
}
],
"stepUpTimeMillis" : 1000,
"stepUpTrueMostOfTheTime" : true,
"successfulElectionsSinceLastStart" : 1,
"stepUpCandidateID" : 0,
"lastFailedElectionDate" : ISODate("1970-01-01T00:00:00.000Z")
},
"members" : [
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 265,
"optime" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-02-25T06:27:42Z"),
"optimeDurableDate" : ISODate("2022-02-25T06:27:42Z"),
"lastHeartbeat" : ISODate("2022-02-25T06:27:41.384Z"),
"lastHeartbeatRecv" : ISODate("2022-02-25T06:27:41.385Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1645741659, 1),
"electionDate" : ISODate("2022-02-25T06:27:39Z"),
"configVersion" : 1,
"self" : true,
"lastReceived" : ISODate("2022-02-25T06:27:41.900Z"),
"lastTS" : Timestamp(1645741662, 1),
"lastVoteDate" : ISODate("2022-02-25T06:27:39.573Z"),
"priority" : 1,
"numYield" : 0,
"readOnly" : false,
"tags" : {
},
"linearizable" : false,
"networkInterfaces" : {
},
"lastApplied" : Timestamp(1645741662, 1),
"lastMdbStatusUpdate" : ISODate("2022-02-25T06:27:42.811Z")
},
{
"_id" : 1,
"name" : "localhost:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 130,
"optime" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-02-25T06:27:42Z"),
"optimeDurableDate" : ISODate("2022-02-25T06:27:42Z"),
"lastHeartbeat" : ISODate("2022-02-25T06:27:41.384Z"),
"lastHeartbeatRecv" : ISODate("2022-02-25T06:27:41.384Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27017",
"syncSourceHost" : "localhost:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"self" : false,
"lastReceived" : ISODate("2022-02-25T06:27:42.572Z"),
"lastTS" : Timestamp(1645741662, 1),
"priority" : 0,
"numYield" : 0,
"readOnly" : false,
"tags" : {
},
"linearizable" : false,
"networkInterfaces" : {
},
"lastMdbStatusUpdate" : ISODate("2022-02-25T06:27:42.811Z")
},
{
"_id" : 2,
"name" : "localhost:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 118,
"optime" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1645741662, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-02-25T06:27:42Z"),
"optimeDurableDate" : ISODate("2022-02-25T06:27:42Z"),
"lastHeartbeat" : ISODate("2022-02-25T06:27:41.384Z"),
"lastHeartbeatRecv" : ISODate("2022-02-25T06:27:41.385Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27017",
"syncSourceHost" : "localhost:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"self" : false,
"lastReceived" : ISODate("2022-02-25T06:27:42.572Z"),
"lastTS" : Timestamp(1645741662, 1),
"priority" : 0,
"numYield" : 0,
"readOnly" : false,
"tags" : {
},
"linearizable" : false,
"networkInterfaces" : {
},
"lastMdbStatusUpdate" : ISODate("2022-02-25T06:27:42.811Z")
}
],
"ok" : 1,
"operationTime" : Timestamp(1645741662, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1645741662, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : 0
}
}
}
在上述rs.status()命令的输出结果中,可以看到“set”字段对应的值就是复制集的ID。
2. 解决方法
当出现“replica set IDs do not match”错误时,需要进行以下操作:
2.1 更新仲裁节点的复制集ID
更新仲裁节点的复制集ID,使其与现有节点的复制集ID相同。可以在控制台输入以下命令:
rs.reconfig({ _id: "rs0", members: [ { _id: 0, host: "localhost:27017" }, { _id: 1, host: "localhost:27018" }, { _id: 2, host: "localhost:27019", arbiterOnly: true, priority: 0 } ] }, { force: true })
其中,{ "_id" : "rs0" }是现有复制集的ID,{ "_id" : 2, "host" : "localhost:27019", "arbiterOnly" : true, "priority" : 0 }是要添加的仲裁节点的信息。注意,添加的节点需要设置“arbiterOnly”为true,以指定其为仲裁节点。
执行完该命令后,在控制台中输入rs.status()命令,查看状态是否为“SECONDARY”。
2.2 重新启动mongod进程
如果仲裁节点的复制集ID已更新,但仍无法连接,可以尝试重新启动mongod进程。在控制台中,输入以下命令:
sudo systemctl mongod restart
2.3 检查网络配置
如果上述操作无法解决问题,可能是因为网络配置错误。可以检查各节点之间的网络是否正常,并且确保各节点可以相互访问和通信。如果网络配置与mongodb的配置不匹配,则会出现连接错误。
总结
本文介绍了MongoDB添加仲裁节点出现“replica set IDs do not match”错误的原因和解决方法。出现这种错误时,可以先通过rs.status()命令查看复制集ID,然后在重新配置仲裁节点的复制集ID和重启进程的基础上,进行网络配置的检查。这样,就可以成功添加仲裁节点,并实现复制集的高可用性。