MongoDB的集群模式--Replica Set


一、Replica Set 集群分為兩種架構:

  • 奇數個節點構成Replica Set,所有節點擁有數據集。最小架構: 1個Primary節點,2個Secondary節點

  • 偶數個節點 + 一個仲裁節點 構成的Replica Set,節點擁有數據集,仲裁節點僅參與仲裁選舉出Primary節點。 最小架構:1個Primary節點,1個Secondary節點,1個Arbiter節點
  •  

接下來就以3台服務器為例,部署具有仲裁的框架。

IP地址 操作系統版本 MongoDB版本 端口 功能
10.10.18.10 Centos7.5  4.0  27017  Primary 
10.10.18.11 Centos7.5  4.0  27017  Secondary
10.10.18.12 Centos7.5  4.0  27017  Arbiter

二、安裝部署(請訪問 "MongoDB安裝")

Primary的配置文件:

systemLog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
   fork: true
   pidFilePath: "/data/mongodb/pid/m.pid"
net:
   bindIp: 10.10.18.10
   port: 27017
replication:
   replSetName: "rs0"

Secondary的配置文件

systemLog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
   fork: true
   pidFilePath: "/data/mongodb/pid/m.pid"
net:
   bindIp: 10.10.18.11
   port: 27017
replication: replSetName:
"rs0"

Arbiter的配置文件

systemLog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
   fork: true
   pidFilePath: "/data/mongodb/pid/m.pid"
net:
   bindIp: 10.10.18.12
   port: 27017
replication: replSetName:
"rs0"

啟動三台服務器上的mongdb

mongod -f /data/mongodb/mongod.conf

關閉進程

 mongod -f /data/mongodb/mongod.conf --shutdown

 

三、配置Replica Set

登錄三台服務器中任意一台,登錄mongo(如果報錯就將配置文件中:security 內容暫時先去掉)

mongo --host 10.10.18.10
>cfg={ _id:"rs0",members:[{_id:0,host:'10.10.18.10:27017',priority:1},{_id:1,host:'10.10.18.11:27017',priority:1},{_id:2,host:'10.10.18.12:27017',arbiterOnly:true}] };

>rs.initiate(cfg)

查看Replica Set配置

> rs.conf()

{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.18.10:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.10.18.11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.10.18.12:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"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("5cff76e5e57e23a5bc7054e2")
}
}

四、驗證Replica Set

在Primary中插入數據

rs0:PRIMARY>  show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
rs0:PRIMARY> db.users.insertOne(
... {
... name:"sue",
... age: 26,
... status:"pending"
... })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5cff79e8993e70290a081d04")
}

rs0:PRIMARY> db.users.find()
{ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

在Secondary中,默認是不允許讀

rs0:SECONDARY> db.users.find()
Error: error: {
        "operationTime" : Timestamp(1560247181, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1560247181, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

五、創建用戶

六、故障模擬

停止Primary上的mongo進程,在Secondary上登錄mongo查看

mongo --host 10.10.18.11

rs0:PRIMARY> db.users.find() { "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

發現原Secondary變成Primary,並且可以進行查詢。

開啟原Primary服務器上的mongo進程,該服務器變成了Secondary。

七、數據備份和恢復

mongodump從MongoDB中讀取數據,保存為BSON文件,mongorestore讀取BSON文件恢復到MongoDB中。適用於小型MongoDB的備份和恢復。

mongodump備份的時候只備份MongoDB中的文檔,使用mongorestore恢復之后需要重建索引

mongodump --host 10.10.18.11 --port 27017 --username myUserAdmin --password abc123 --gzip --out /data/mongobak/$(date +%F)

注:--oplog 表示備份的時候oplog.bson文件,存放在dump開始 到 dump結束之間所有的oplog。

根據前面的備份恢復到一個新的Replica Set集群中其中一台服務器上。

恢復

恢復的過程中,不要啟用認證。否則在將其他機器加入到Replica Set集群中來的時候,會出現無法認證的問題。

1、首先在一台服務器上開啟一個干凈mongo服務(無任何數據),做為 Primary

mongd.conf配置文件內容

systemLog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
   fork: true
   pidFilePath: "/data/mongodb/pid/mongod.pid"
net:
   bindIp: 10.10.18.10
   port: 27017
replication:
   replSetName: "rs0"

2、開啟mongod服務

/data/mongodb/bin/mongod -f /data/mongodb/mongod.conf

3、創建單個Replica Set

登錄mongo,然后創建單個Replica Set

mongo --host 10.10.18.10
> cfg={_id:"rs0",members:[{_id:0,host:'10.10.18.10:27017',priority:1}]}
> rs.initiate(cfg)

創建結果:

 1 {
 2         "ok" : 1,
 3         "operationTime" : Timestamp(1560394449, 1),
 4         "$clusterTime" : {
 5                 "clusterTime" : Timestamp(1560394449, 1),
 6                 "signature" : {
 7                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
 8                         "keyId" : NumberLong(0)
 9                 }
10         }
11 }
View Code

創建好了之后,就可以查看當前的數據庫

rs0:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

可以發現,目前就三個基本的數據庫。

4、恢復數據

mongorestore --host 10.10.18.10 --port 27017 --gzip /data/mongobak/2019-06-12 

結果

 1 2019-06-13T10:55:33.817+0800    preparing collections to restore from
 2 2019-06-13T10:55:33.829+0800    reading metadata for admin.test from /data/mongobak/2019-06-12/admin/test.metadata.json.gz
 3 2019-06-13T10:55:33.832+0800    reading metadata for test.users from /data/mongobak/2019-06-12/test/users.metadata.json.gz
 4 2019-06-13T10:55:33.834+0800    reading metadata for reporting.reporting from /data/mongobak/2019-06-12/reporting/reporting.metadata.json.gz
 5 2019-06-13T10:55:33.841+0800    reading metadata for admin.reporting from /data/mongobak/2019-06-12/admin/reporting.metadata.json.gz
 6 2019-06-13T10:55:33.861+0800    restoring test.users from /data/mongobak/2019-06-12/test/users.bson.gz
 7 2019-06-13T10:55:34.085+0800    restoring admin.test from /data/mongobak/2019-06-12/admin/test.bson.gz
 8 2019-06-13T10:55:34.123+0800    no indexes to restore
 9 2019-06-13T10:55:34.123+0800    finished restoring test.users (1 document)
10 2019-06-13T10:55:34.123+0800    restoring admin.reporting from /data/mongobak/2019-06-12/admin/reporting.bson.gz
11 2019-06-13T10:55:34.175+0800    restoring reporting.reporting from /data/mongobak/2019-06-12/reporting/reporting.bson.gz
12 2019-06-13T10:55:34.230+0800    no indexes to restore
13 2019-06-13T10:55:34.230+0800    finished restoring admin.test (2 documents)
14 2019-06-13T10:55:34.233+0800    no indexes to restore
15 2019-06-13T10:55:34.233+0800    finished restoring reporting.reporting (1 document)
16 2019-06-13T10:55:34.234+0800    no indexes to restore
17 2019-06-13T10:55:34.234+0800    finished restoring admin.reporting (1 document)
18 2019-06-13T10:55:34.234+0800    restoring users from /data/mongobak/2019-06-12/admin/system.users.bson.gz
19 2019-06-13T10:55:34.312+0800    done
View Code

恢復完成,在登錄mongo查看,恢復數據庫的情況。

 1 rs0:PRIMARY> show dbs
 2 admin      0.000GB
 3 config     0.000GB
 4 local      0.000GB
 5 reporting  0.000GB
 6 test       0.000GB
 7 rs0:PRIMARY> use test
 8 switched to db test
 9 rs0:PRIMARY> show collections
10 users
11 rs0:PRIMARY> db.users.find()
12 { "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

可以看出之前的記錄已經恢復了。

注意:如果備份時,MongoDB是擁有用戶名和密碼才能進行登錄的,在恢復之后,用戶名和密碼也是可以使用的。如果需要在使用認證登錄,需要將下面信息添加的配置文件mongod.conf:

security:
   keyFile: "/data/mongodb/keyfile"
   authorization: "enabled"

然后重啟mongo服務。

5、關閉剛恢復的MongoDB,將數據目錄同步到另一台服務器上,作為Secondary

通過之前,確認該台服務器上的mongo是無任何數據。將Primary服務上的 /data/mongodb/data 同步到 Secondary服務器上  /data/mongodb/data

6、依次開啟Primary、Secondary、Arbiter的mongo服務

7、在Primary服務器上,將Secondary、Arbiter加入到Replica Set集群中來

查看當前Replica Set集群的配置

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.10.18.10:27017",
                        "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("5d01bad15dfd3f294d147b36")
        }
}

可以看到當前Replica Set集群中就一台服務器

將Secondary、Arbiter加入到Replica Set集群中

rs0:PRIMARY> rs.add(
... {
... _id: 1,
... host:'10.10.18.11:27017',
... priority:1
... }
... )
{
        "ok" : 1,
        "operationTime" : Timestamp(1560397822, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1560397822, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
rs0:PRIMARY> rs.add(
... {
... _id: 2,
... host:'10.10.18.12:27017',
... arbiterOnly:true
... }
... )
{
        "ok" : 1,
        "operationTime" : Timestamp(1560397830, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1560397830, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
 

再次查看狀態:

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.10.18.10:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "10.10.18.11:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "10.10.18.12:27017",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "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("5d01bad15dfd3f294d147b36")
        }
}

加入成功。

開啟認證訪問

在三台服務器的配置文件中添加:

security:
   keyFile: "/data/mongodb/keyfile"
   authorization: "enabled"

然后依次關閉:Arbiter Secondary Primary

依次開啟:Primary Secondary Arbiter 

 

 

security:
   keyFile: "/data/mongodb/keyfile"
   authorization: "enabled"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM