MongoDB主备仲裁节点关闭顺序

作者: wencst 分类: linux,架构设计 发布时间: 2019-07-17 13:03 阅读: 364 次

MongoDB分为三个角色节点:Primary(P)、Secondary(S)、Arbiter(A),但是关闭顺序不正确,关闭时就会有问题,有些时候,关闭后启动也会有问题。

因此需要有一个正确的顺序来关闭MongoDB集群。下方为实验顺序:

1:关闭顺序PSA :会报错
2:关闭顺序PAS :可以
3:关闭顺序SAP :主节点会变为从节点
4:关闭顺序SPA :会报错
5:关闭顺序APS
6:关闭顺序ASP :主节点会变为从节点

服务查看:

gechongrepl:PRIMARY> rs.isMaster()
{
    "setName" : "gechongrepl",
    "setVersion" : 13,
    "ismaster" : true,
    "secondary" : false,
    "hosts" : [
        "192.168.91.133:27017",
        "192.168.91.132:27017",
        "192.168.91.132:27018"
    ],
    "arbiters" : [
        "192.168.91.135:27017"
    ],
    "primary" : "192.168.91.133:27017",
    "me" : "192.168.91.133:27017",
    "electionId" : ObjectId("5568098a6de3d53a2ea91c64"),
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2015-05-29T06:39:36.548Z"),
    "maxWireVersion" : 3,
    "minWireVersion" : 0,
    "ok" : 1
}

1:关闭顺序PSA

P:

gechongrepl:PRIMARY> use admin
switched to db admin
gechongrepl:PRIMARY> db.shutdownServer()

S:

从变为了:gechongrepl:PRIMARY>
<br><br>gechongrepl:PRIMARY> use admin
switched to db admin
gechongrepl:PRIMARY> db.shutdownServer()
assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:48:06.022-0700
Error: assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:48:06.022-0700
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at assert (src/mongo/shell/assert.js:20:5)
at DB.shutdownServer (src/mongo/shell/db.js:212:9)
at (shell):1:4
2015-05-28T23:48:06.023-0700 E QUERY Error: assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:48:06.022-0700
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at assert (src/mongo/shell/assert.js:20:5)
at DB.shutdownServer (src/mongo/shell/db.js:212:9)
at (shell):1:4 at src/mongo/shell/assert.js:13

A:

gechongrepl:ARBITER> use admin
switched to db admin
gechongrepl:ARBITER> db.shutdownServer()

再关闭S:

S变为了:ggechongrepl:SECONDARY>
 
gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

2:关闭顺序PAS : 已经验证,不报错。

3:关闭顺序SAP

S:

gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

A:

gechongrepl:ARBITER> use admin
switched to db admin
gechongrepl:ARBITER> db.shutdownServer()

P:

主变为了:gechongrepl:SECONDARY>
 
gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

4:关闭顺序SPA

S:

gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

P:

gechongrepl:PRIMARY> use admin
switched to db admin
gechongrepl:PRIMARY> db.shutdownServer()
assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:59:19.685-0700
Error: assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:59:19.685-0700
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at assert (src/mongo/shell/assert.js:20:5)
    at DB.shutdownServer (src/mongo/shell/db.js:212:9)
    at (shell):1:4
2015-05-28T23:59:19.702-0700 E QUERY    Error: assert failed : unexpected error: Error: shutdownServer failed: No electable secondaries caught up as of 2015-05-28T23:59:19.685-0700
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at assert (src/mongo/shell/assert.js:20:5)
    at DB.shutdownServer (src/mongo/shell/db.js:212:9)
    at (shell):1:4 at src/mongo/shell/assert.js:13

A:

gechongrepl:ARBITER> use admin
switched to db admin
gechongrepl:ARBITER> db.shutdownServer()

再次关闭P:

gechongrepl:PRIMARY> use admin
switched to db admin
2015-05-29T00:00:44.938-0700 I NETWORK  DBClientCursor::init call() failed
2015-05-29T00:00:44.940-0700 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-05-29T00:00:44.941-0700 I NETWORK  reconnect 127.0.0.1:27017 (127.0.0.1) ok
gechongrepl:SECONDARY> db.shutdownServer()

5:关闭顺序APS

A:

gechongrepl:ARBITER> use admin
switched to db admin
gechongrepl:ARBITER> db.shutdownServer()

P:

gechongrepl:PRIMARY> use admin
switched to db admin
gechongrepl:PRIMARY> db.shutdownServer()

S:因为仲裁节点关闭了。所以Primary节点不会再自动切换

gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

6:关闭顺序ASP

A:

gechongrepl:ARBITER> use admin
switched to db admin
gechongrepl:ARBITER> db.shutdownServer()

S:

gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

P:

gechongrepl:PRIMARY>
2015-05-29T00:12:23.962-0700 I NETWORK  DBClientCursor::init call() failed
2015-05-29T00:12:23.965-0700 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-05-29T00:12:23.966-0700 I NETWORK  reconnect 127.0.0.1:27017 (127.0.0.1) ok

Primary变为Secondary:
gechongrepl:SECONDARY>
gechongrepl:SECONDARY> use admin
switched to db admin
gechongrepl:SECONDARY> db.shutdownServer()

总结:

1:先关闭从节点、仲裁节点。或者先关闭仲裁节点,最后关闭从节点。则:主节点自动变为从节点

2:先关闭主节点,则从节点自动变为主节点,在仲裁节点关闭之前,新主节点不能关闭

 

初始化的时候主从节点受priority的影响

priority: 是优先级,默认为1,优先级0为被动节点,不能成为活跃节点。优先级不位0则按照有大到小选出活跃节点。

 

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)



Leave a Reply