標籤:伺服器 叢集架構 初始化 命令 .net code 節點 3.4 資料庫
環境:
windows作業系統
mongodb 3.4社區版
目標:
配置包含兩個分區一個設定管理員的分區叢集。其中每一個分區和一個設定管理員都被配置為一個單獨的複本集。如所示:
註:每一個分區都應該被配置在一個單獨的伺服器裝置上。方便起見,本文在同一台機器通過不同連接埠類比不同伺服器上的組件,實現分區叢集的配置。(生產環境的配置與此相同,只需使用自己的主機名稱、連接埠、路徑等即可)。
為本文配置的分區叢集架構,其中的任意節點(複本集節點和分區節點)都是可擴充的。
1、分別為config server、shard-1、shard-2建立資料目錄
每一個分區成員配置為一個複本集,因此每個組件下都包含自己的複製集資料目錄。
config server、shard-1、shard-2的複製集分別為rs1 、rs2、 rs3 。
每個複製集各包含三個節點 rsx-0(主節點)、rsx-1、rsx-2 。
其中config server 複製集資料目錄如下:shard-1、shard-2分區的複製集目錄與此類似、以此類推。
每個成員的連接埠分配及啟動配置如下:
2、建立config server 複製集
mongod --configsvr --replSet <setname> --dbpath <path>
--configsvr 聲明該執行個體是分區叢集的佈建服務資料庫
--replSet 指定複製集名稱
--dbpath 指定資料存放目錄
分別使用以下命令,啟動並初始化config server複本集的三個成員:
mongod --configsvr --port 27020 --dbpath E:\devInstall\MongoDB\shard\consrv\rs1-0 --replSet rs1 --smallfiles --oplogSize 128
mongod --configsvr --port 27021 --dbpath E:\devInstall\MongoDB\shard\consrv\rs1-1 --replSet rs1 --smallfiles --oplogSize 128
mongod --configsvr --port 27022 --dbpath E:\devInstall\MongoDB\shard\consrv\rs1-2 --replSet rs1 --smallfiles --oplogSize 128
初始化成功之後,使用mongo shell 串連到3個設定管理員之一:
mongo --host <hostname> --port <port>
串連成功之後,設定複製集及其節點:
rs.initiate( { _id: "<replSetName>", configsvr: true, members: [ { _id : 0, host : "127.0.0.1:27020" }, { _id : 1, host : "127.0.0.1:27021" }, { _id : 2, host : "127.0.0.1:27022" } ] })
複製集設定成功之後,使用rs.conf()即可查看相關狀態:
2、建立shard 複製集(以shard-1為例)
mongod --shardsvr --replSet <replSetname>
--shardsvr 聲明此執行個體為叢集的分區資料庫執行個體
--replSet 指定複製集名稱
分別使用以下命令,啟動並初始化shard-1複製集的三個成員:
shard-1:
mongod --shardsvr --port 27023 --dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-0 --replSet rs2 --smallfiles --oplogSize 128
mongod --shardsvr --port 27024 --dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-1 --replSet rs2 --smallfiles --oplogSize 128
mongod --shardsvr --port 27025 --dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-2 --replSet rs2 --smallfiles --oplogSize 128
初始化成功之後,,使用mongo shell 串連到3個分區伺服器之一:
mongo --host <hostname> --port <port>
串連成功之後,設定複製集及其節點:
rs.initiate( { _id : <replicaSetName>, members: [ { _id : 0, host : "127.0.0.1:27023" }, { _id : 1, host : "127.0.0.1:27024"}, { _id : 2, host : "127.0.0.1:27025" } ] })
設定成功後,使用rs.status()查看複製集及各個節點狀態:
shard-2複製集配置同上。
3、mongos配置
啟動串連到當前分區叢集的mongos(路由)。需要使用 --configdb 選項來指定config server複製集。
格式:<configReplSetName>/cfg1.example.net:27017,cfg2.example.net:27017,...
在此,我們使用config server 複製集的第一個節點在27030連接埠上啟動mongos路由服務:
mongos --configdb "rs1/127.0.0.1:27020" --port 27030
使用mongo shell串連到mongos執行個體:
使用db.isMster()判斷是否到mongos執行個體,如果存在 "msg":"isdbgrid",表明成功串連。
4、將分區添加到叢集中
依然在串連到mongos的mongo shell視窗。
使用sh.addShard("<replSetName>/s1-mongo1.example.net:27017")方法添加shard-1和shard-2兩個分區到叢集中。
5、使用雜湊分區對collection進行分區
開啟資料庫分區功能:
sh.enableSharding("<database>")
指定資料庫表中的欄位key為分區鍵,並對其使用雜湊分區策略。
sh.shardCollection("<database>.<collection>", { <key> : "hashed" } )
到此即結束了分區叢集的設定。
例:開啟shard資料的分區功能。指定student表的stuNum為雜湊索引分區鍵。向student表插入7條資料記錄。如下:
通過mongo 進入1分區shard-1,發現student表中包含5條資料。
通過mongo 進入2分區shard-2,發現student表中包含2條資料。
此分區叢集中的student表中有7條資料。有5條在shard-1伺服器上,2條在shard-2伺服器上。(mongodb 通過計算stuNum欄位的雜湊值決定將其儲存在哪一個分區上)
使用mongos的使用者不關心資料存放區在哪裡,使用者只知道這個叢集的資料庫中有7條student資料記錄。
MongoDB分區叢集配置執行個體