W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在Mongodb里面存在另一種集群,就是分片技術(shù),可以滿足MongoDB數(shù)據(jù)量大量增長的需求。
當MongoDB存儲海量的數(shù)據(jù)時,一臺機器可能不足以存儲數(shù)據(jù)也足以提供可接受的讀寫吞吐量。這時,我們就可以通過在多臺機器上分割數(shù)據(jù),使得數(shù)據(jù)庫系統(tǒng)能存儲和處理更多的數(shù)據(jù)。
下圖展示了在MongoDB中使用分片集群結(jié)構(gòu)分布:
上圖中主要有如下所述三個主要組件:
用于存儲實際的數(shù)據(jù)塊,實際生產(chǎn)環(huán)境中一個shard server角色可由幾臺機器組個一個relica set承擔,防止主機單點故障
mongod實例,存儲了整個 ClusterMetadata,其中包括 chunk信息。
前端路由,客戶端由此接入,且讓整個集群看上去像單一數(shù)據(jù)庫,前端應(yīng)用可以透明使用。
分片結(jié)構(gòu)端口分布如下:
Shard Server 1:27020 Shard Server 2:27021 Shard Server 3:27022 Shard Server 4:27023 Config Server :27100 Route Process:40000
步驟一:啟動Shard Server
[root@100 /]# mkdir -p /www/mongoDB/shard/s0 [root@100 /]# mkdir -p /www/mongoDB/shard/s1 [root@100 /]# mkdir -p /www/mongoDB/shard/s2 [root@100 /]# mkdir -p /www/mongoDB/shard/s3 [root@100 /]# mkdir -p /www/mongoDB/shard/log [root@100 /]# /usr/local/mongoDB/bin/mongod --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork .... [root@100 /]# /usr/local/mongoDB/bin/mongod --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork
步驟二: 啟動Config Server
[root@100 /]# mkdir -p /www/mongoDB/shard/config [root@100 /]# /usr/local/mongoDB/bin/mongod --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork
注意:這里我們完全可以像啟動普通mongodb服務(wù)一樣啟動,不需要添加—shardsvr和configsvr參數(shù)。因為這兩個參數(shù)的作用就是改變啟動端口的,所以我們自行指定了端口就可以。
步驟三: 啟動Route Process
/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500
mongos啟動參數(shù)中,chunkSize這一項是用來指定chunk的大小的,單位是MB,默認大小為200MB.
步驟四: 配置Sharding
接下來,我們使用MongoDB Shell登錄到mongos,添加Shard節(jié)點
[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000 MongoDB shell version: 2.0.7 connecting to: 127.0.0.1:40000/admin mongos> db.runCommand({ addshard:"localhost:27020" }) { "shardAdded" : "shard0000", "ok" : 1 } ...... mongos> db.runCommand({ addshard:"localhost:27029" }) { "shardAdded" : "shard0009", "ok" : 1 } mongos> db.runCommand({ enablesharding:"test" }) #設(shè)置分片存儲的數(shù)據(jù)庫 { "ok" : 1 } mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}}) { "collectionsharded" : "test.log", "ok" : 1 }
步驟五: 程序代碼內(nèi)無需太大更改,直接按照連接普通的mongo數(shù)據(jù)庫那樣,將數(shù)據(jù)庫連接接入接口40000
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: