您好,登錄后才能下訂單哦!
mongodb中怎么切換集群節(jié)點(diǎn)故障,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
處理方法
方法1、一般會(huì)自動(dòng)切換,如果shard是由3個(gè)節(jié)點(diǎn)組成的replica set,主節(jié)無法連接到另外兩個(gè)副節(jié)點(diǎn)時(shí),和rs.conf()里面的electionTimeoutMillis值相關(guān)默認(rèn)是10秒,兩個(gè)副節(jié)點(diǎn)自動(dòng)會(huì)有選擇一個(gè)節(jié)點(diǎn)成為主節(jié)點(diǎn);如果shard是由2個(gè)節(jié)點(diǎn)組成的replica set,把主節(jié)點(diǎn)啟動(dòng)后該主節(jié)點(diǎn)自動(dòng)變成了副節(jié)點(diǎn),原副節(jié)點(diǎn)自動(dòng)變成了新的主節(jié)點(diǎn)
方法2、如果主節(jié)點(diǎn)機(jī)器完全無法使用了,需要對某個(gè)shard的副本節(jié)點(diǎn)執(zhí)行切換,切換到primay狀態(tài),mongo命令進(jìn)入這個(gè)shard的輔助節(jié)點(diǎn),查看rs.conf()配置信息,并查看rs.status()狀態(tài),看到主節(jié)點(diǎn)無法連接,使用rs.reconfig()重新配置,rs.reconfig括號里面的內(nèi)容參考rs.conf()的信息,去掉primay節(jié)點(diǎn)的信息,并添加{ "force": true }。之后退出重新登錄這個(gè)shard,發(fā)現(xiàn)副本切換到了primay狀態(tài),重新登錄mongos發(fā)現(xiàn)正常了
使用mongo登錄mongod實(shí)例執(zhí)行rs.status()或db.isMaster()可以看到誰是主節(jié)點(diǎn),誰是副節(jié)點(diǎn)
mongos實(shí)例執(zhí)行rs.status()會(huì)報(bào)錯(cuò)replSetGetStatus is not supported through mongos
mongos實(shí)例執(zhí)行db.isMaster()正常,默認(rèn)連接到主節(jié)點(diǎn)
實(shí)驗(yàn)案例
1、切換到TDB6執(zhí)行show tables報(bào)錯(cuò),說這個(gè)數(shù)據(jù)庫對應(yīng)的shard的primay節(jié)點(diǎn)無法找到
mongo --host 172.22.138.157 --port 27001
mongos> use TDB6
mongos> show tables
2019-06-20T00:16:17.935-0700 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "Could not find host matching read preference { mode: \"primary\", tags: [ {} ] } for set shard28003",
2、只能進(jìn)入這個(gè)shard的輔助節(jié)點(diǎn),查看rs.conf()配置信息,并查看rs.status()狀態(tài),看到主節(jié)點(diǎn)無法連接,使用rs.reconfig()重新配置,rs.reconfig括號里面的內(nèi)容參考rs.conf()的信息,去掉primay節(jié)點(diǎn)的信息,并添加{ "force": true }
mongo --host 172.22.138.157 --port 28003
shard28003:SECONDARY>rs.conf()
{
"_id" : "shard28003",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "172.22.138.157:28003",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "172.22.138.158:28003",
"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("5d09d2c89fb43c4506d995ac")
}
}
shard28003:SECONDARY> rs.status()
{
"set" : "shard28003",
"date" : ISODate("2019-06-20T07:25:07.438Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1561001587, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1561001587, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1561001587, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1561001587, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.22.138.157:28003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15254,
"optime" : {
"ts" : Timestamp(1561001587, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-06-20T03:33:07Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.22.138.158:28003",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2019-06-20T07:25:07.121Z"),
"lastHeartbeatRecv" : ISODate("2019-06-20T03:33:08.040Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
}
],
"ok" : 1,
"operationTime" : Timestamp(1561001587, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
},
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1561001589, 1),
"t" : NumberLong(2)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1561015481, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shard28003:SECONDARY> rs.reconfig({
"_id" : "shard28003",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "172.22.138.157:28003",
"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("5d09d2c89fb43c4506d995ac")
}
},
{ "force": true }
)
3、退出后,重新登錄,發(fā)現(xiàn)副本切換到了primay狀態(tài)
mongo --host 172.22.138.157 --port 28003
shard28003:PRIMARY>
4、重新登錄mongos,發(fā)現(xiàn)可以正常操作show tables,但是無法db.createCollection,因?yàn)镃onfig Server的replica set的主節(jié)點(diǎn)壞了,重新按上面1、2、3方法切換Config Server的replica set的副節(jié)點(diǎn)為主節(jié)點(diǎn)就好了
mongo --host 172.22.138.157 --port 27001
mongos> show tables
test06
mongos> use testdb2
switched to db testdb2
mongos> db.createCollection("table1")
{
"ok" : 0,
"errmsg" : "Database testdb2 not found due to Could not confirm non-existence of database testdb 2 due to Could not find host matching read preference { mode: \"primary\" } for set config29001",
"code" : 133,
"codeName" : "FailedToSatisfyReadPreference",
"operationTime" : Timestamp(1561034171, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1561034171, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
關(guān)于mongodb中怎么切換集群節(jié)點(diǎn)故障問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。