1. 程式人生 > >MongoDB--架構搭建 分片+副本集

MongoDB--架構搭建 分片+副本集

分片架構
  • 1、分片是對資料量比較大的集合進行拆分到不同伺服器MongoDB例項中去,如果架構是分片架構,則以後登入不能使用mongo,要改為mongos路由連結形式
  • 2、分片架構的核心是分片的片鍵設定,片鍵選取的目標是能讓後續插入的集合均勻的插入到各個片區中去。如果根據自增、建立時間等自增型別設定片鍵,則會造成,最後的資料庫越來越大,不符合初衷,如果根據性別、學歷等設定片鍵,則會造成資料塊較少,起不到分片的效果。
  • 3、結合副本集,則架構如下圖:


  • 4、例項準備,3臺伺服器:伺服器1對應埠1.2.3、伺服器2對應埠4.5.6、伺服器3對應埠7.8.9 (如果開啟許可權認證之後,請保持所有的例項關聯的keyfile檔案一樣

9個分片節點:127.0.0.1:27771、127.0.0.1:27772、127.0.0.1:27773、127.0.0.1:27774.....127.0.0.1:27779

1.4.7為shard1副本集、2.5.8為shard2副本集、3.6.9為shard3副本集

單個節點設定完成之後,登陸其中要設定為副本集的主節點,進行設定副本集

命令:

rs.initiate() --節點初始化

cfg={_id:"Set1", members:[ {_id:0,host:"172.17.7.106:25552"}] } --節點預設值

rs.reconfig(cfg, {force:true}) --載入預設值節點

rs.add("XXXXXXX") --新增其他節點

3個配置節點:127.0.0.1:27761、127.0.0.1:27762、127.0.0.1:27763

注意配置節點需要配置為副本集形式,此舉是3.0版本之後得強制設定,這樣可以提高容災機制

配置節點每臺伺服器一個

命令:與分片節點的副本集命令一樣,注意修改副本集的ID

3個路由節點:127.0.0.1:27751、127.0.0.1:27752、127.0.0.1:27753(注意:因為是單機模擬,所以路由起一個就可以了,多了則會報錯)

請注意,開啟路由節點需要使用mongos 命令

1、分片節點例項配置檔案

例項1(注意下面開頭的---也是配置檔案內容為第一行)

---
systemLog:
   destination: file
   path: "D:\\MGDBP\\Set1\\LOG\\Set1_1.log"
   logAppend: true
storage:
   dbPath: "D:\\MGDBP\\Set1\\DB"
   journal:
      enabled: true
   directoryPerDB: true
   syncPeriodSecs: 30
   engine: wiredTiger # MongoDB 3.0版本開始務必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
#         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: "slowOp"
#processManagement:
#   fork: true
#   pidFilePath: "D:\\MSset\\Set1\\Forklog" 
net:
   port: 27771
replication:
  replSetName: "Set1"
  oplogSizeMB: 10000
sharding:
  clusterRole: shardsvr
security:
  authorization: "enabled"
  clusterAuthMode: "keyFile"
  keyFile: "D:\\MGDBP\\Set1\\KEY\\skey.key"

例項2

---
systemLog:
   destination: file
   path: "D:\\MGDBP\\Set2\\LOG\\Set2_1.log"
   logAppend: true
storage:
   dbPath: "D:\\MGDBP\\Set2\\DB"
   journal:
      enabled: true
   directoryPerDB: true
   syncPeriodSecs: 30
   engine: wiredTiger # MongoDB 3.0版本開始務必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
#         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: "slowOp"
#processManagement:
#   fork: true
#   pidFilePath: "D:\\MSset\\Set2\\Forklog" 
net:
   port: 27772
replication:
  replSetName: "Set2"
  oplogSizeMB: 10000
sharding:
  clusterRole: shardsvr
security:
  authorization: "enabled"
  clusterAuthMode: "keyFile"
  keyFile: "D:\\MGDBP\\Set2\\KEY\\skey.key"

仿照設定到例項9...

2、配置節點例項配置檔案

節點1

---
systemLog:
   destination: file
   path: "D:\\MGDBP\\Conf\\LOG\\s1.log"
   logAppend: true
storage:
   dbPath: "D:\\MGDBP\\Conf\\DB"
   journal:
      enabled: true
   directoryPerDB: true
   syncPeriodSecs: 30
   engine: wiredTiger # MongoDB 3.0版本開始務必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
#         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: "slowOp"
#processManagement:
#   fork: true
#   pidFilePath: "D:\\MSset\\Set1\\Forklog" 
net:
   port: 27761
replication:
  replSetName: "confrs"
  oplogSizeMB: 10000
sharding:
  clusterRole: "configsvr"
security:
  authorization: "enabled"
  clusterAuthMode: "keyFile"
  keyFile: "D:\\MGDBP\\Conf\\confkey.key"

節點2

---
systemLog:
   destination: file
   path: "D:\\MGDBP\\Conf\\LOG\\s1.log"
   logAppend: true
storage:
   dbPath: "D:\\MGDBP\\Conf\\DB"
   journal:
      enabled: true
   directoryPerDB: true
   syncPeriodSecs: 30
   engine: wiredTiger # MongoDB 3.0版本開始務必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
#         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: "slowOp"
#processManagement:
#   fork: true
#   pidFilePath: "D:\\MSset\\Set1\\Forklog" 
net:
   port: 27762--(注意此處,如果是同一臺機器,請修改埠)
replication:
  replSetName: "confrs"
  oplogSizeMB: 10000
sharding:
  clusterRole: "configsvr"
security:
  authorization: "enabled"
  clusterAuthMode: "keyFile"
  keyFile: "D:\\MGDBP\\Conf\\confkey.key"

節點3

---
systemLog:
   destination: file
   path: "D:\\MGDBP\\Conf\\LOG\\s1.log"
   logAppend: true
storage:
   dbPath: "D:\\MGDBP\\Conf\\DB"
   journal:
      enabled: true
   directoryPerDB: true
   syncPeriodSecs: 30
   engine: wiredTiger # MongoDB 3.0版本開始務必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
#         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: "slowOp"
#processManagement:
#   fork: true
#   pidFilePath: "D:\\MSset\\Set1\\Forklog" 
net:
   port: 27763--(注意此處,如果是同一臺機器,請修改埠)
replication:
  replSetName: "confrs"
  oplogSizeMB: 10000
sharding:
  clusterRole: "configsvr"
security:
  authorization: "enabled"
  clusterAuthMode: "keyFile"
  keyFile: "D:\\MGDBP\\Conf\\confkey.key"

3、路由節點例項配置檔案

節點1

logpath=D:\\MGDBP\\Mongos\\LOG\\s1.log 
logappend=true 
port=27751
configdb=confrs/XXXXXX:25541,XXXXXXXX:25541,XXXXXXXX:25541 --(注意,此處為配置檔案所設定的IP和埠)
keyFile=D:\\MGDBP\\Mongos\\mongoskey.key

節點2

logpath=D:\\MGDBP\\Mongos\\LOG\\s1.log 
logappend=true 
port=27752
configdb=confrs/XXXXXX:25541,XXXXXXXX:25541,XXXXXXXX:25541 --(注意,此處為配置檔案所設定的IP和埠)
keyFile=D:\\MGDBP\\Mongos\\mongoskey.key

節點3

logpath=D:\\MGDBP\\Mongos\\LOG\\s1.log 
logappend=true 
port=27753
configdb=confrs/XXXXXX:25541,XXXXXXXX:25541,XXXXXXXX:25541 --(注意,此處為配置檔案所設定的IP和埠)
keyFile=D:\\MGDBP\\Mongos\\mongoskey.key
  • 4、最後使用mongo 登入到路由節點,mongo 127.0.0.1:27751,進行分片的配置初始化

路由節點進行配置

db.runCommand({

"addshard":"shardrs1/127.0.0.1:27771,127.0.0.1:27774,127.0.0.1:27777"

})

db.runCommand({

"addshard":"shardrs2/127.0.0.1:27772,127.0.0.1:27775,127.0.0.1:27778"

})

db.runCommand({

"addshard":"shardrs3/127.0.0.1:27773,127.0.0.1:27776,127.0.0.1:27779"

})

  • 5、物理節點配置好之後,需要配置sharding分片規則。

設定需要分片的資料庫

db.runCommand({

enablesharding:"testsd"

})

設定片鍵

db.runCommand({

shardcollection:"testsd.stu",

key:{"name":1}

})

執行到此步驟 則證明 配置成功 

專案目錄

http://pan.baidu.com/s/1nvqhPXJ     (此專案檔案為單機部署,沒有許可權)



伺服器環境搭建實踐
遇到問題:
1、以windows服務啟動時報錯,msvcp140.dll 丟失
1、使用mongoDB安裝檔案 安裝一下
2、以windows服務啟動時報錯,vc++錯誤
1、下載vc_redist.x64.exe檔案,執行。
3、登入節點,設定叢集初始化命令報錯
1、檢視防火牆,埠是否都開啟,可以設定20000-65535埠開啟
2、使用命令單獨增加節點, rs.initiate() , 等待成為主節點時,rs.add("") 新增剩下節點。
3、如果節點添加出現泵機或其他問題,可以暫停問題節點,刪除資料,重新開啟,重新初始化節點新增。
cfg={_id:"Set2", members:[ {_id:0,host:"172.17.7.106:25552"}] }
rs.reconfig(cfg, {force:true});
rs.add(""")
4、分片時 設定路由出錯,sharding.configdb配置檔案報錯
1、路由啟動時應該是用mongos命令 而不是mongod
5、mongos 路由連結不上配置節點Error initializing sharding state, sleeping for 2 seconds and trying again 
1、檢視keyfile是否和配置節點的一致,必須一致才能鏈上配置節點
6、當設定初始化副本集的時候報錯
1、請注意配置檔案的Ip地址,與rs.status() 裡面節點名稱是否和配置檔案顯示的是否一致