1逸吵、分片組件
mongo中分片由以下三個組件組成:Shard Server凶硅,Config Server,Router Server扫皱。
1.1足绅、Shard Server
Shard Server為存儲實際數(shù)據(jù)的分片,每個Shard 可以是一個mongod 實例韩脑,也可以是一組mongod 實例構(gòu)成的Replica Set编检。為了實現(xiàn)每個Shard 內(nèi)部的auto-failover,MongoDB 官方建議每個Shard為一組Replica Set扰才。
分片中的每個數(shù)據(jù)庫都有一個主要分片允懂,它包含該數(shù)據(jù)庫的所有未分片的集合,每個數(shù)據(jù)庫都有自己的主分片衩匣,主分片與副本集中的主無關(guān)蕾总。在創(chuàng)建新數(shù)據(jù)庫時通過選擇 cluster 中具有最少數(shù)據(jù)量的分片來選擇主分片。
要更改數(shù)據(jù)庫的主分片琅捏,請使用movePrimary命令生百。遷移主分片的 process 可能需要很長時間才能完成,并且在完成之前不應訪問與數(shù)據(jù)庫關(guān)聯(lián)的集合柄延。根據(jù)要遷移的數(shù)據(jù)量蚀浆,遷移可能會影響整個 cluster 操作。在嘗試更改主分片之前搜吧,請考慮對 cluster 操作和網(wǎng)絡(luò)負載的影響市俊。
使用sh.status()方法可查看分片的信息,包含數(shù)據(jù)庫主分片及分片中的塊分布等滤奈。
1.2摆昧、Config Server
配置服務器存儲分片 cluster的元數(shù)據(jù)。元數(shù)據(jù)反映了分片 cluster 中所有數(shù)據(jù)和組件的 state 和組織蜒程。元數(shù)據(jù)包括每個分片上的塊列表以及定義塊的范圍绅你。mongos實例緩存此數(shù)據(jù)并使用它將讀取和寫入操作路由到正確的分片伺帘, mongos在 cluster 的元數(shù)據(jù)更改時更新緩存,每個分片 cluster 必須有自己的配置服務器忌锯。不要將相同的配置服務器用于不同的分片群集伪嫁。
分片群集的配置服務器可以部署為副本集而不是三個鏡像配置服務器。使用配置服務器的副本集可以提高配置服務器之間的一致性偶垮,因為 MongoDB 可以利用配置數(shù)據(jù)的標準副本集讀寫協(xié)議礼殊。此外,使用配置服務器的副本集允許分片 cluster 具有 3 個以上的配置服務器针史,因為副本集最多可包含 50 個成員。要將配置服務器部署為副本集碟狞,配置服務器必須 run WiredTiger 存儲引擎啄枕。
當副本集用于配置服務器時,有以下限制:
- 必須為零仲裁者族沃。
- 必須沒有推遲會員频祝。
- 必須 build 索引(i.e.沒有成員應將buildIndexes設(shè)置設(shè)置為 false)。
admin數(shù)據(jù)庫包含與身份驗證和授權(quán)相關(guān)的集合以及內(nèi)部使用的其他system.*集合脆淹。配置數(shù)據(jù)庫包含包含分片 cluster 元數(shù)據(jù)的集合常空。 MongoDB 在元數(shù)據(jù)更改時將數(shù)據(jù)寫入配置數(shù)據(jù)庫。
MongoDB 從admin數(shù)據(jù)庫讀取身份驗證和授權(quán)數(shù)據(jù)以及其他內(nèi)部用途盖溺。當mongos啟動時或在元數(shù)據(jù)發(fā)生更改后漓糙,MongoDB 從config數(shù)據(jù)庫讀取。
如果所有配置服務器都不可用烘嘱,則 cluster 可能無法運行昆禽。為確保配置服務器保持可用且完整,配置服務器的備份至關(guān)重要蝇庭。配置服務器上的數(shù)據(jù)與存儲在 cluster 中的數(shù)據(jù)相比較小醉鳖,并且配置服務器的活動負載相對較低。
mongod和mongos實例會監(jiān)聽config服務器狀態(tài)哮内,如果聯(lián)系不成功監(jiān)聽配置服務器副本集次數(shù)超過replMonitorMaxFailedChecks的配置值盗棵,則mongos或mongod實例將無法使用,直到重新啟動實例北发。
1.3纹因、Router Server
router在分片 cluster中對分片進行查詢和寫入操作。 mongos從客戶端的角度提供了分片 cluster 的唯一接口琳拨, 用戶永遠不會與分片直接連接或通信辐怕。mongos通過緩存配置服務器中的元數(shù)據(jù)來跟蹤哪些數(shù)據(jù)在哪個分片上。 mongos使用元數(shù)據(jù)來將 applications 和 clients 中的操作路由到mongod實例从绘。 mongos沒有持久 state 并且消耗最少的系統(tǒng)資源寄疏。
mongos查詢處理為是牢,確定必須接收查詢的碎片列表,在所有分片上建立游標陕截,合并來自各目標分片的數(shù)據(jù)并返回結(jié)果文檔驳棱。某些查詢修飾符在主分片上執(zhí)行。對不包含片鍵的查詢執(zhí)行廣播操作农曲,即將查詢路由到分片集群的所有分片中社搅,同時某些包含片鍵的查詢?nèi)钥赡軐е聫V播查詢,具體取決于分片中的數(shù)據(jù)分布和查詢的選擇性乳规。
mongos查詢修飾符處理:
- 排序:如果未對查詢結(jié)果進行排序形葬,則mongos實例將打開一個結(jié)果游標,即從分片上的所有游標“循環(huán)”結(jié)果暮的。
- 范圍:如果查詢使用limit() 方法限制結(jié)果集的大小笙以,則mongos實例會將該限制傳遞給分片,然后在將結(jié)果返回給 client 之前將結(jié)果限制為 re-applies冻辩。
- 跳過:如果查詢使用skip() 方法指定要跳過的記錄數(shù)猖腕,則mongos無法將跳過傳遞給分片,而是從分片中檢索未提取的結(jié)果恨闪,并在匯總完整結(jié)果時跳過適當數(shù)量的文檔倘感。
通常,分片環(huán)境中最快的查詢是mongos route 到單個分片咙咽,使用配置服務器中的碎片 key和 cluster 元數(shù)據(jù)老玛。這些目標操作使用 shard key value 來定位滿足查詢文檔的分片或分片子集。對于不包含 shard key 的查詢钧敞,mongos必須查詢所有分片逻炊,等待其響應,然后_return 將結(jié)果返回到 application犁享。這些“scatter/gather”查詢可以是 long running 操作余素。
updateMany()和deleteMany()方法是 broadcast 操作,除非查詢文檔完整指定了 shard key炊昆。
mongos可以 route 查詢包含 shard key 或復合 shard key 的前綴 key 特定分片或一組分片桨吊。 mongos使用 shard key來定位塊,其范圍包括 shard key凤巨,并將查詢指向包含該塊的碎片视乐。
所有insertOne()操作都針對一個分片。 insertMany() 中的每個文檔都以單個分片為目標敢茁,但不能保證 insert 中的所有文檔都成為一個分片佑淀。所有updateOne(),replaceOne()和deleteOne()操作必須在查詢文檔中包含碎片 key或_id彰檬。如果在沒有 shard key 或_id的情況下使用這些方法伸刃,則 MongoDB 會返回錯誤谎砾。根據(jù) cluster 中的數(shù)據(jù)分布和查詢的選擇性,mongos可能仍會執(zhí)行broadcast 操作來完成這些查詢捧颅。
如果查詢不包含碎片 key景图,則mongos必須將查詢作為“scatter/gather”操作發(fā)送到所有分片。反過來碉哑,每個分片將使用 shard key 索引或另一個更有效的索引來完成查詢挚币。如果查詢包含多個 sub-expressions,它們引用由分片 key 和輔助索引索引的字段扣典,則mongos可以將查詢路由到特定分片妆毕,并且分片將使用允許其最有效地執(zhí)行的索引。
2贮尖、片鍵
片鍵確定分片中集合文件的分布情況笛粘,片鍵是集合中每個文檔存在的索引或符合索引。mongo通過片鍵的范圍來確定分塊远舅,一個范圍的片鍵對應一個數(shù)據(jù)分塊,片鍵在分塊中均勻分布痕钢,片鍵與塊分布的有效性直接相關(guān)图柏。片鍵一旦確定無法更改。
2.1任连、片鍵索引
所有分片集合必須具有支持碎片 key的索引蚤吹。索引可以是 shard key 上的索引,也可以是復合索引随抠,其中 shard key 是索引的字首裁着。如果集合為空,則sh.shardCollection()在分片 key 上創(chuàng)建索引(如果此類索引尚不存在)拱她。如果集合不為空二驰,則必須先使用sh.shardCollection()創(chuàng)建索引。
通過在 shard key 上使用唯一索引秉沼,MongoDB 可以對 shard key 值強制執(zhí)行唯一性桶雀。 MongoDB 強制整個 key 組合的唯一性,而不是 shard key 的各個組件唬复。要對 shard key 值強制實施唯一性矗积,請將unique參數(shù)作為true傳遞給sh.shardCollection()方法:如果集合為空,則sh.shardCollection()在分片 key 上創(chuàng)建唯一索引(如果此類索引尚不存在)敞咧。如果集合不為空棘捣,則必須先使用sh.shardCollection()創(chuàng)建索引。
2.2休建、片鍵的選擇
片鍵的選擇會影響所有分片中塊的創(chuàng)建和分布情況乍恐。這會影響分片集群中的整體效率和操作性能评疗。理想的片鍵能使文檔在分片中均勻分布。
片鍵的基數(shù)確定平衡器可以創(chuàng)建的最大塊數(shù)禁熏,大的基數(shù)會減少或消除分片集合的水平擴容能力壤巷。在給定的時間內(nèi),唯一的片鍵只存在于唯一的塊內(nèi)瞧毙。
具有高基數(shù)的分片 key 不能保證在分片 cluster 中均勻分布數(shù)據(jù)胧华,盡管它更有利于水平擴展。 shard key 的頻率和變化率也有助于數(shù)據(jù)分發(fā)宙彪。選擇分片 key 時請考慮每個因素矩动。若數(shù)據(jù) 模型需要對基數(shù)較低的 key 進行分片,請考慮使用具有較高相對基數(shù)的符合索引释漆。
具有低頻率的 shard key 不能保證在分片 cluster 中均勻分布數(shù)據(jù)悲没。 shard key 的基數(shù)和變化率也有助于數(shù)據(jù)分發(fā)。value 上單調(diào)增加或減少的分片 key 更有可能將插入分布到 cluster 中的單個分片男图。發(fā)生這種情況是因為每個 cluster 都有一個塊示姿,它捕獲一個上限為maxKey的范圍。 maxKey始終比其他所有值都高逊笆。類似地栈戳,有一個塊捕獲具有minKey下限的范圍。 minKey始終比較低于所有其他值难裆。如果 shard key value 始終在增加子檀,則所有新插入都將路由到以maxKey作為上限的塊。如果 shard key value 始終在減少乃戈,則所有新插入都將路由到以minKey作為下限的塊褂痰。包含該塊的分片成為寫操作的瓶頸。當使用單調(diào)變化的字段作為片鍵時症虑,考慮使用哈希索引缩歪。
2.3、散列片鍵
散列分片鍵使用單個字段的哈希指數(shù)作為碎片 key來分割分片 cluster 中的數(shù)據(jù)谍憔。
散列分片在分片 cluster 中提供更均勻的數(shù)據(jù)分布驶冒,但代價是減少目標操作與 Broadcast Operations。使用散列分片韵卤,具有“close”分片 key 值的文檔不可能在同一塊或分片上骗污,并且mongos更有可能執(zhí)行Broadcast Operations來完成給定查詢。
以下操作使用哈希分片策略對目標集合進行分片:
sh.shardCollection("<database>.<collection>", { <shard key> : "hashed" } )
必須指定集合的完整命名空間和 shard key沈条。選擇的 shard key 會影響分片的效率需忿,以及利用某些分片 features(如zones)的能力。
2.4、范圍片鍵
在 ranged-based 分片中屋厘,MongoDB 會自動將數(shù)據(jù)劃分為由碎片 key值確定的連續(xù)范圍涕烧。在此 model 中,具有“close”shard key 值的文檔可能位于相同的塊或碎片中汗洒。這允許有效查詢议纯,其中讀取操作將目標文檔定位在連續(xù)范圍內(nèi)。但是溢谤,讀取和寫入 performance 都可能會因為 shard key 選擇不當而降低瞻凤。見Shard Key Selection。
3世杀、分片中的Chunks
MongoDB 使用與集合關(guān)聯(lián)的片鍵將數(shù)據(jù)分區(qū)為塊阀参,塊由分片數(shù)據(jù)的子集組成,mongos routes 根據(jù)碎片 key value 寫入相應的塊瞻坝。 MongoDB 在超出配置的塊大小時會分割塊蛛壳。插入和更新都可以觸發(fā)塊拆分。
3.1所刀、塊大小
MongoDB 中的默認塊大小為 64 兆字節(jié)衙荐。你可以增加或減少塊大小。
考慮更改默認塊大小的含義:
小塊以更頻繁的遷移為代價導致更均勻的數(shù)據(jù)分布浮创。這會在查詢路由(mongos)層創(chuàng)建費用忧吟。
大塊導致遷移更少瓦糟。從網(wǎng)絡(luò)角度和查詢路由層的內(nèi)部開銷來看,這都更有效徒爹。但是掏愁,這些效率是以可能不均衡的數(shù)據(jù)分布為代價的。
塊大小會影響每個要遷移的塊的最大文檔數(shù)下愈。
分片現(xiàn)有的集合時,塊大小會影響最大集合大小。 Post-sharding乡话,塊大小不會限制集合大小。
3.2耳奕、塊分裂
當塊增長超過指定的塊大小時绑青,或者塊中的文檔數(shù)超過Maximum Number of Documents Per Chunk to Migrate時,MongoDB 根據(jù)塊中的 shard key 值對塊進行拆分屋群。必要時闸婴,塊可以分成多個塊。插入和更新可能會觸發(fā)拆分芍躏。分裂是一種有效的 meta-data 變化邪乍。要創(chuàng)建拆分,MongoDB 不會遷移任何數(shù)據(jù)或影響分片。拆分可能導致碎片的分布不均勻庇楞,從而在分片上產(chǎn)生塊遷移榜配。在這種情況下,平衡器會跨分片重新分配塊
3.3吕晌、塊遷移
mongodb通過在分片直接遷移塊來保持分片的數(shù)據(jù)均衡蛋褥。遷移方式為:
- 手動:可通過命令手動進行塊的遷移,以達到分片之間的數(shù)據(jù)均衡睛驳;
- 自動:當分片中數(shù)據(jù)塊分布不均時烙心,平衡器自動進行塊遷移。
4柏靶、平衡器
MongoDB 平衡器是一個后臺 process弃理,用于監(jiān)視每個碎片上塊的數(shù)量。當給定分片上的塊數(shù)達到特定遷移閾值時屎蜓,平衡器會嘗試在分片之間自動遷移塊痘昌,并在每個分片中達到相同數(shù)量的塊。 分片簇的平衡過程對用戶和 application 層完全透明炬转,盡管在執(zhí)行過程時可能會產(chǎn)生一些性能影響辆苔。
4.1、查看平衡器狀態(tài)
sh.getBalancerState()檢查平衡器是否已啟用(i.e.允許平衡器運行 run)扼劈。 sh.getBalancerState()不檢查平衡器是否正在主動平衡塊驻啤。要查看分片 cluster中是否啟用了平衡器,請發(fā)出以下命令荐吵,該命令將返回 boolean:
sh.getBalancerState()
4.2骑冗、檢查平衡器是否運行中
使用以下操作確定平衡器是否正在運行:
sh.isBalancerRunning()
4.3、配置默認塊大小
分片 cluster 的默認塊大小為 64 MB先煎。在大多數(shù)情況下贼涩,默認大小適合拆分和遷移塊。更改默認塊大小會影響遷移期間進程和 auto-splits 的塊薯蝎,但不會追溯影響所有塊遥倦。
4.4、設(shè)置平衡窗口
在某些情況下占锯,特別是當數(shù)據(jù)集緩慢增長且遷移可能影響性能時袒哥,確保平衡器僅在特定時間處于活動狀態(tài)非常有用。以下過程指定activeWindow消略,這是平衡器能夠遷移塊的時間范圍堡称,確保平衡器未停止。平衡器不會在stopped state 中激活艺演。要確保平衡器不是stopped却紧。
使用update()設(shè)置activeWindow婿失,如下所示:
db.settings.update(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },
{ upsert: true }
)
4.5、刪除平衡窗口
使用$unset清除activeWindow啄寡,如下所示:
use config
db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })
4.6豪硅、禁用平衡器
默認情況下,平衡器可以在任何時間運行挺物,并且只在需要時移動塊懒浮。要在短時間內(nèi)禁用平衡器并阻止所有遷移,請使用以下過程:
- 使用mongo shell 連接到 cluster 中的任何mongos识藤。
- 發(fā)出以下操作以禁用平衡器:sh.stopBalancer()砚著;
4.7、啟用平衡器
如果您已禁用平衡器并準備好 re-enable 它痴昧,請使用此過程:
使用mongo shell 連接到 cluster 中的任何mongos稽穆。
發(fā)出以下操作之一以啟用平衡器:sh.setBalancerState(true)
4.8、在集合上啟用/禁用平衡器
您可以使用sh.enableBalancing()方法為特定集合啟用平衡赶撰。為集合啟用平衡時舌镶,MongoDB 不會立即開始平衡數(shù)據(jù)。但是豪娜,如果分片集合中的數(shù)據(jù)不平衡餐胀,MongoDB 將能夠更均勻地開始分發(fā)數(shù)據(jù)。要在集合上啟用平衡瘤载,請使用mongo shell 連接到mongos并調(diào)用sh.enableBalancing()方法否灾。
sh.enableBalancing("students.grades")
禁用集合上的平衡時,MongoDB 不會中斷正在進行的遷移鸣奔。
sh.disableBalancing("students.grades")
4.9墨技、確定集合啟用/禁用平衡器
要確認是否已啟用或禁用集合的平衡,請在config數(shù)據(jù)庫中查詢集合命名空間的collections集合挎狸,并檢查noBalance字段扣汪。例如:
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
此操作將返回 null 錯誤,true伟叛,false或無輸出:
null 錯誤表示集合命名空間不正確私痹。
如果結(jié)果為true脐嫂,則禁用平衡统刮。
如果結(jié)果為false,則當前啟用平衡账千,但過去已為集合禁用了平衡侥蒙。此集合的平衡將在平衡器運行的下一個 time 開始。
如果操作未返回任何輸出匀奏,則當前啟用平衡鞭衩,并且此集合以前從未禁用過。此集合的平衡將在平衡器運行的下一個 time 開始。
5论衍、部署分片
5.1瑞佩、shard集群配置
5.1.1、分片集群目錄
構(gòu)造三個節(jié)點的副本集分片坯台。
端口號為27117的節(jié)點目錄:
#數(shù)據(jù)目錄
C:\mongo\shard\shard_27117\data
#日志目錄
C:\mongo\shard\shard_27117\log
//配置目錄
C:\mongo\shard\shard_27117\config.conf
端口號為27118的目錄:
#數(shù)據(jù)目錄
C:\mongo\shard\shard_27118\data
#日志目錄
C:\mongo\shard\shard_27118\log
//配置目錄
C:\mongo\shard\shard_27118\config.conf
端口號為27119的目錄:
#數(shù)據(jù)目錄
C:\mongo\shard\shard_27119\data
#日志目錄
C:\mongo\shard\shard_27119\log
//配置目錄
C:\mongo\shard\shard_27119\config.conf
5.1.2炬丸、分片集群配置
端口號為27117節(jié)點的配置:
logpath=C:\mongo\shard\shard_27117\log\db.log
pidfilepath=C:\mongo\shard\shard_27117\mongo.pid
logappend=true
port=27117
dbpath=C:\mongo\shard\shard_27117\data
shardsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=shard
端口號為27118節(jié)點的配置:
logpath=C:\mongo\shard\shard_27118\log\db.log
pidfilepath=C:\mongo\shard\shard_27118\mongo.pid
logappend=true
port=27118
dbpath=C:\mongo\shard\shard_27118\data
shardsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=shard
端口號為27119分片的配置:
logpath=C:\mongo\shard\shard_27119\log\db.log
pidfilepath=C:\mongo\shard\shard_27119\mongo.pid
logappend=true
port=27119
dbpath=C:\mongo\shard\shard_27119\data
shardsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=shard
5.1.3、分片集群的啟動
./mongod.exe --config C:\\mongo\\shard\\shard_27117\\config.conf ./mongod.exe --config C:\\mongo\\shard\\shard_27118\\config.conf ./mongod.exe --config C:\\mongo\\shard\\shard_27119\\config.conf
5.1.4蜒蕾、分片集群的初始化
連接一個分片副本集的一個節(jié)點:
$ ./mongo.exe --port 27117
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27117/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f31e1cf8-82d5-4b7d-b70b-9c7db887ad3d") }
MongoDB server version: 4.2.0
副本集初始化:
use admin
switched to db admin
rsconf = { _id:"shard",members:[ {_id:0,host:"localhost:27117"}, {_id:1,host:"localhost:27118"}, {_id:2,host:"localhost:27119"}] }
{
"_id" : "shard",
"members" : [
{
"_id" : 0,
"host" : "localhost:27117"
},
{
"_id" : 1,
"host" : "localhost:27118"
},
{
"_id" : 2,
"host" : "localhost:27119"
}
]
}
rs.initiate(rsconf)
{
"operationTime" : Timestamp(1579231067, 1),
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(1579231067, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
分片副本集狀態(tài)查看:
db.isMaster()
{
"hosts" : [
"localhost:27117",
"localhost:27118",
"localhost:27119"
],
"setName" : "shard",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost:27117",
"me" : "localhost:27117",
"electionId" : ObjectId("7fffffff0000000000000002"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1579231077, 1),
"t" : NumberLong(2)
},
"lastWriteDate" : ISODate("2020-01-17T03:17:57Z"),
"majorityOpTime" : {
"ts" : Timestamp(1579231077, 1),
"t" : NumberLong(2)
},
"majorityWriteDate" : ISODate("2020-01-17T03:17:57Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-01-17T03:18:03.873Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 17,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1579231077, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1579231077, 1)
}
5.2稠炬、config副本集配置
5.2.1、config副本集目錄
端口號為27217的節(jié)點:
#數(shù)據(jù)目錄
C:\mongo\config\config_27217\data
#日志目錄
C:\mongo\config\config_27217\log
//配置目錄
C:\mongo\config\config_27217\config.conf
端口號為27218的節(jié)點
#數(shù)據(jù)目錄
C:\mongo\config\config_27218\data
#日志目錄
C:\mongo\config\config_27218\log
//配置目錄
C:\mongo\config\config_27218\config.conf
端口號為27219的節(jié)點:
#數(shù)據(jù)目錄
C:\mongo\config\config_27219\data
#日志目錄
C:\mongo\config\config_27219\log
//配置目錄
C:\mongo\config\config_27219\config.conf
5.2.2咪啡、三節(jié)點的配置
端口號為27117節(jié)點的配置:
logpath=C:\mongo\config\config_27217\log\db.log
pidfilepath=C:\mongo\config\config_27217\mongo.pid
logappend=true
port=27217
dbpath=C:\mongo\config\config_27217\data
configsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=config
端口號為27118節(jié)點的配置:
logpath=C:\mongo\config\config_27218\log\db.log
pidfilepath=C:\mongo\config\config_27218\mongo.pid
logappend=true
port=27218
dbpath=C:\mongo\config\config_27218\data
configsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=config
端口號為27119節(jié)點的配置:
logpath=C:\mongo\config\config_27219\log\db.log
pidfilepath=C:\mongo\config\config_27219\mongo.pid
logappend=true
port=27219
dbpath=C:\mongo\config\config_27219\data
configsvr=true # 在配置文件添加此項就行
oplogSize=512
replSet=config
5.1.3首启、config副本集的啟動
./mongod.exe --config C:\\mongo\\config\\config_27217\\config.conf ./mongod.exe --config C:\\mongo\\config\\config_27218\\config.conf ./mongod.exe --config C:\\mongo\\config\\config_27219\\config.conf
5.1.4、副本集的初始化
連接到某個節(jié)點:
./mongo.exe --port 27217
配置初始化參數(shù):
rsconf = {
_id: "config",
members: [{
_id: 0,
host: "localhost:27217"
}, {
_id: 1,
host: "localhost:27218"
}, {
_id: 2,
host: "localhost:27219"
}]
}
初始化副本集:
rs.initiate(rsconf)
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1579168488, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1579168488, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1579168488, 1)
}
查看副本集狀態(tài):
rs.conf()
{
"_id" : "config",
"version" : 1,
"configsvr" : true,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27217",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27218",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27219",
"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("5e2032e8cf32782800b7fa3a")
}
}
rs.isMaster()
{
"hosts" : [
"localhost:27217",
"localhost:27218",
"localhost:27219"
],
"setName" : "config",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost:27217",
"me" : "localhost:27217",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1579169171, 2),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2020-01-16T10:06:11Z"),
"majorityOpTime" : {
"ts" : Timestamp(1579169171, 2),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2020-01-16T10:06:11Z")
},
"configsvr" : 2,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-01-16T10:06:28.483Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 2,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1579168488, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1579169171, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1579169171, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1579169171, 2)
}
5.3撤摸、router配置
5.3.1毅桃、router的目錄
C:\mongo\router\router_27017\data
C:\mongo\router\router_27017\log
C:\mongo\router\router_27017\config.conf
5.3.2、router的配置
logpath=C:\mongo\router\router_27017\db.log
pidfilepath=C:\mongo\router\router_27017mongo.pid
logappend=true
port=27017
configdb=config/127.0.0.1:27217,127.0.0.1:27218,127.0.0.1:27219
configdb中格式為<setName>/[host1:port1,host2:port2]
5.3.3准夷、添加服務器分片
連接router:
./mongo.exe --port 27117
添加分片服務器:
mongos> db.runCommand({addshard:"shard/localhost:27117"})
{
"shardAdded" : "shard",
"ok" : 1,
"operationTime" : Timestamp(1579231179, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1579231179, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
addShard命令參數(shù)格式為:<setName>/[host1:port1,host2:port2]
5.4疾嗅、對集合分片
創(chuàng)建數(shù)據(jù)和集合:
mongos> use dest
switched to db dest
mongos> db.createCollection("city")
{
"ok" : 1,
"operationTime" : Timestamp(1579231933, 8),
"$clusterTime" : {
"clusterTime" : Timestamp(1579231933, 8),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> db.city.createIndex({name:"code"})
{
"raw" : {
"config/localhost:27217,localhost:27218,localhost:27219" : {
"ok" : 0,
"errmsg" : "Unknown index plugin 'code'",
"code" : 67,
"codeName" : "CannotCreateIndex"
}
},
"code" : 67,
"codeName" : "CannotCreateIndex",
"ok" : 0,
"errmsg" : "Unknown index plugin 'code'",
"operationTime" : Timestamp(1579232211, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1579232211, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
設(shè)置數(shù)據(jù)及索引分片
mongos> use admin
switched to db admin
mongos> db.runCommand({ enablesharding:"dest" })
{
"ok" : 1,
"operationTime" : Timestamp(1579232062, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1579232062, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> db.runCommand({ shardcollection: "dest.city", key: { code:1}})
{
"ok" : 0,
"errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection",
"code" : 72,
"codeName" : "InvalidOptions",
"operationTime" : Timestamp(1579232071, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1579232071, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
6、分片管理
6.1冕象、sh.status()查看集群信息
sh.status()可查看分片代承、數(shù)據(jù)庫和分片集合的信息。塊的數(shù)量較多時渐扮,sh.status()會輸出塊的狀態(tài)论悴,而非打印每個塊的相關(guān)信息。如需查看所有的塊墓律,可使用sh.status(true)命令來打印盡可能詳細的信息膀估。
mongos> use admin
switched to db admin
mongos> db.runCommand({ enablesharding:"dest" })
{
"ok" : 1,
"operationTime" : Timestamp(1579232062, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1579232062, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> db.runCommand({ shardcollection: "dest.city", key: { code:1}})
{
"ok" : 0,
"errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection",
"code" : 72,
"codeName" : "InvalidOptions",
"operationTime" : Timestamp(1579232071, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1579232071, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
6.2、查詢配置信息
分片集群的所有配置信息都保存在配置服務器中耻讽,可直接訪問config數(shù)據(jù)庫來獲取配置信息察纯。
連接上配置服務器集群后,通過如下操作找到配置數(shù)據(jù)庫:
show dbs
admin 0.000GB
config 0.001GB
local 0.000GB
use config
switched to db config
show collections
changelog
chunks
collections
databases
lockpings
locks
migrations
mongos
shards
tags
transactions
version
6.2.1针肥、config.shards集合
config.shards集合中跟蹤記錄集群內(nèi)所有的分片信息饼记。其結(jié)構(gòu)如下:
db.shards.findOne()
{
"_id" : "shard",
"host" : "shard/localhost:27117,localhost:27118,localhost:27119",
"state" : 1
}
分片id為副本集名稱。
6.2.2慰枕、config.databases集合
config.databases集合跟蹤記錄集群中所有數(shù)據(jù)庫的信息具则,不論數(shù)據(jù)庫是否分片。
db.databases.findOne()
{
"_id" : "city",
"primary" : "shard",
"partitioned" : true,
"version" : {
"uuid" : UUID("b8a1ef6f-4673-40bd-84d7-10a30918348e"),
"lastMod" : 1
}
}
primary標識數(shù)據(jù)的主分片具帮,所有新集合均默認被創(chuàng)建在主分片上博肋。
6.2.3低斋、config.collection集合
config.collection集合跟蹤記錄分片集合的信息,非分片集合信息除外匪凡。
db.collections.findOne()
{
"_id" : "config.system.sessions",
"lastmodEpoch" : ObjectId("5e21286a75e4dc2b10df9cf3"),
"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
"dropped" : false,
"key" : {
"_id" : 1
},
"unique" : false,
"uuid" : UUID("60aa6a51-d8fd-4a29-bbcc-6923b277c97e")
}
_id:集合命名空間膊畴;
key:片鍵;
unique:片鍵是否唯一病游,片鍵默認是不唯一的巴比;
6.2.4、config.chunks集合
config.chunks集合跟蹤記錄分片集合中所有塊信息礁遵;
{
"_id": "config.system.sessions-_id_MinKey",
"ns": "config.system.sessions",
"min": {
"_id": {
"$minKey": 1
}
},
"max": {
"_id": {
"$maxKey": 1
}
},
"shard": "shard",
"lastmod": Timestamp(1, 0),
"lastmodEpoch": ObjectId("5e21286a75e4dc2b10df9cf3"),
"history": [{
"validAfter": Timestamp(1579231338, 3),
"shard": "shard"
}]
}
_id:塊唯一標識符轻绞。該標識符通常由命名空間、片鍵和塊的下邊界值組成佣耐;
ns:塊所屬是集合名稱政勃;
min:塊范圍的最小值(包含);
max:塊范圍的最大值(不包含)兼砖;
shard:塊所屬分片奸远;
6.2.5、config.changelog集合
config.changelog集合用于跟蹤記錄集群的操作讽挟,此集合會記錄所有的拆分和遷移操作懒叛。
db.changelog.findOne()
{
"_id" : "010A1905050081:27217-2020-01-17T11:19:39.023+0800-5e2127cbba5ea4e9b1f8103c",
"server" : "010A1905050081:27217",
"shard" : "config",
"clientAddr" : "127.0.0.1:56969",
"time" : ISODate("2020-01-17T03:19:39.023Z"),
"what" : "addShard",
"ns" : "",
"details" : {
"name" : "shard",
"host" : "shard/localhost:27117"
}
}
6.2.6、config.tags集合
config.tags集合保存系統(tǒng)配置分片的標簽耽梅,每個標簽都與一個塊分為相關(guān)聯(lián)薛窥。
6.2.7、config.settings集合
config.settings集合含有當前均衡器設(shè)置和塊大小的文檔信息眼姐。通過修改該文檔诅迷,可關(guān)閉或開啟均衡器,也可修改塊大小众旗。