MongoDB副本集新增和刪除仲裁節點一例

chenfeng發表於2018-02-27
MongoDB仲裁者(Arbiter)是複製集中的一個mongodb例項,它並不儲存資料。
仲裁節點使用最小的資源並且不要求硬體裝置,最好不要將Arbiter部署在同一個資料集節點中,可以部署在其他伺服器中,也可部署在單獨的虛擬機器中。

當節點數目為奇數時,可以不需要仲裁節點。
當節點數目為偶數個時,需要部署一個仲裁節點,否則偶數個節點,當主節點掛了後,其他節點會變為只讀。

副本集新增仲裁節點步驟(本例中MongoDB副本集是一主兩從):
新增仲裁節點:
在仲裁節點編輯mongodb.conf
# vi /etc/mongodb.conf
logpath=/home/mongodb/logs/arbiter.log
logappend=false
port=27019
fork=true
dbpath=/home/mongodb/arbiter
#auth=true
replSet=test  (應和副本集其他節點的叢集名字保持一致)

啟動arbiter上的mongodb例項:
#/data/mongodb-3.4.13/bin/mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 23331
child process started successfully, parent exiting

登陸到副本集上的主節點,進行如下操作:
test:PRIMARY> rs.addArb("192.168.0.3:27019");
{ "ok" : 1 }

檢視狀態:
test:PRIMARY> rs.status()
{
        "set" : "test",
        "date" : ISODate("2018-02-27T08:24:28.866Z"),
        "myState" : 1,
        "term" : NumberLong(6),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1519719858, 1),
                        "t" : NumberLong(6)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1519719858, 1),
                        "t" : NumberLong(6)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1519719858, 1),
                        "t" : NumberLong(6)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.0.51:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 4429,
                        "optime" : {
                                "ts" : Timestamp(1519719858, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2018-02-27T08:24:18Z"),
                        "electionTime" : Timestamp(1519716787, 1),
                        "electionDate" : ISODate("2018-02-27T07:33:07Z"),
                        "configVersion" : 4,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.0.52:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4369,
                        "optime" : {
                                "ts" : Timestamp(1519719858, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1519719858, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2018-02-27T08:24:18Z"),
                        "optimeDurableDate" : ISODate("2018-02-27T08:24:18Z"),
                        "lastHeartbeat" : ISODate("2018-02-27T08:24:27.265Z"),
                        "lastHeartbeatRecv" : ISODate("2018-02-27T08:24:28.224Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "192.168.0.51:27017",
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "192.168.0.61:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 2501,
                        "optime" : {
                                "ts" : Timestamp(1519719858, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1519719858, 1),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2018-02-27T08:24:18Z"),
                        "optimeDurableDate" : ISODate("2018-02-27T08:24:18Z"),
                        "lastHeartbeat" : ISODate("2018-02-27T08:24:27.099Z"),
                        "lastHeartbeatRecv" : ISODate("2018-02-27T08:24:28.024Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "192.168.0.51:27017",
                        "configVersion" : 4
                },
                {
                        "_id" : 3,
                        "name" : "192.168.0.3:27019",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 96,
                        "lastHeartbeat" : ISODate("2018-02-27T08:24:28.443Z"),
                        "lastHeartbeatRecv" : ISODate("2018-02-27T08:24:27.449Z"),
                        "pingMs" : NumberLong(1),
                        "configVersion" : 4
                }
        ],
        "ok" : 1
}
test:PRIMARY>


test:PRIMARY> db.isMaster()
{
        "hosts" : [
                "192.168.0.51:27017",
                "192.168.0.52:27017",
                "192.168.0.61:27017"
        ],
        "arbiters" : [
                "192.168.0.3:27019"
        ],
        "setName" : "test",
        "setVersion" : 4,
        "ismaster" : true,
        "secondary" : false,
        "primary" : "192.168.0.51:27017",
        "me" : "192.168.0.51:27017",
        "electionId" : ObjectId("7fffffff0000000000000006"),
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1519719928, 1),
                        "t" : NumberLong(6)
                },
                "lastWriteDate" : ISODate("2018-02-27T08:25:28Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2018-02-27T08:25:33.660Z"),
        "maxWireVersion" : 5,
        "minWireVersion" : 0,
        "readOnly" : false,
        "ok" : 1
}

刪除仲裁節點方法:
test:PRIMARY> rs.remove("192.168.0.3:27019");

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2151331/,如需轉載,請註明出處,否則將追究法律責任。

相關文章