溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

大數(shù)據(jù) MongoDB 3.2.1 分片

發(fā)布時間:2020-07-24 11:49:23 來源:網(wǎng)絡(luò) 閱讀:884 作者:HHHNDYD 欄目:MongoDB數(shù)據(jù)庫

MongoDB 分片

  • 在Mongodb里面存在另一種集群,就是分片技術(shù),可以滿足MongoDB數(shù)據(jù)量大量增長的需求。

  • 當(dāng)MongoDB存儲海量的數(shù)據(jù)時,一臺機(jī)器可能不足以存儲數(shù)據(jù),也可能不足以提供可接受的讀寫吞吐量。這時,我們就可以通過在多臺機(jī)器上分割數(shù)據(jù),使得數(shù)據(jù)庫系統(tǒng)能存儲和處理更多的數(shù)據(jù)。

分片的目的

  高數(shù)據(jù)量和吞吐量的數(shù)據(jù)庫應(yīng)用會對單機(jī)的性能造成較大壓力,大的查詢量會將單機(jī)的CPU耗盡,大的數(shù)據(jù)量對單機(jī)的存儲壓力較大,最終會耗盡系統(tǒng)的內(nèi)存而將壓力轉(zhuǎn)移到磁盤IO上。

解決方法 :

有兩個基本的方法: 垂直擴(kuò)展和水平擴(kuò)展。
  • 垂直擴(kuò)展:增加更多的CPU和存儲資源來擴(kuò)展容量。

  •  水平擴(kuò)展:將數(shù)據(jù)集分布在多個服務(wù)器上。水平擴(kuò)展即分片

分片結(jié)構(gòu)圖(圖片來源于網(wǎng)絡(luò)) :

大數(shù)據(jù) MongoDB 3.2.1 分片

MongoDB 分片群集的組成(圖片來源于網(wǎng)絡(luò)) :

MongoDB分片群集的三個主要組件:

Shard:
用于存儲實際的數(shù)據(jù)塊,實際生產(chǎn)環(huán)境中一個shard server角色可由幾臺機(jī)器組個一個replica set承擔(dān),防止主機(jī)單點故障

Config Server:
mongod實例,存儲了整個 ClusterMetadata,其中包括 chunk信息。

Query Routers:
前端路由,客戶端由此接入,且讓整個集群看上去像單一數(shù)據(jù)庫,前端應(yīng)用可以透明使用。

大數(shù)據(jù) MongoDB 3.2.1 分片

分片群集的簡單部署 :

實驗環(huán)境 :

1臺路由實例(端口27017)。
1臺配置實例(端口37017)。
2臺shard實例(端口47017、47018)。

1.配置配置服務(wù)器 :

vim mongodb1.conf

port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true        #開啟配置服務(wù)
mongod -f /usr/local/mongodb/bin/mongodb1.conf  #開啟配置實例

2.配置分片服務(wù)器 :

vim mongodb2.conf

port=47017
dbpath=/data/mongodb/mongodb2
logpath=/data/logs/mongodb2.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true    #開啟分片服務(wù)

vim mongodb3.conf

port=47018
dbpath=/data/mongodb/mongodb3
logpath=/data/logs/mongodb3.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true    #開啟分片服務(wù)

mongod -f /usr/local/mongodb/bin/mongodb2.conf   #開啟分片實例
mongod -f /usr/local/mongodb/bin/mongodb3.conf

3.啟動路由服務(wù)器 :

[root@localhost bin]# ./mongos --port 27017 --fork  --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.217.134:37017 --chunkSize 1
2018-07-23T14:15:28.185+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 15337
child process started successfully, parent exiting

4.添加分片服務(wù)器 :

[root@localhost bin]# mongo
MongoDB shell version: 3.2.1
......
mongos> show dbs
config  0.031GB
mongos> sh.status()      #查看分片狀態(tài)
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5b557280f9effb757fd31cdb")
}
  shards:            #分片為空
  active mongoses:
    "3.2.1" : 1
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
mongos> sh.addShard("192.168.217.134:47017")   #添加分片
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.217.134:47018")
{ "shardAdded" : "shard0001", "ok" : 1 }

mongos> sh.status()       #查看分片狀態(tài)
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5b557280f9effb757fd31cdb")
}
  shards:      #分片信息
    {  "_id" : "shard0000",  "host" : "192.168.217.134:47017" }
    {  "_id" : "shard0001",  "host" : "192.168.217.134:47018" }
  active mongoses:
    "3.2.1" : 1
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:

4.啟用分片服務(wù)器 :

mongos> use test
switched to db test
mongos> for(var i=1;i<=10000;i++)db.users.insert({"id":i,"name":"tom"+i})  #添加數(shù)據(jù)
WriteResult({ "nInserted" : 1 })
mongos> sh.status()  
.......
  databases:
    {  "_id" : "test",  "primary" : "shard0000",  "partitioned" : false }
    #partitioned 值為false 表示數(shù)據(jù)庫尚未分片。

mongos> sh.enableSharding("test")   #啟用數(shù)據(jù)庫分片

mongos> db.users.createIndex({"id":1})   #創(chuàng)建索引

mongos> sh.shardCollection("test.users",{"id":1})  #表分片
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos> sh.status()
......
            { "id" : { "$minKey" : 1 } } -->> { "id" : 2341 } on : shard0001 Timestamp(5, 1) 
            { "id" : 2341 } -->> { "id" : 4682 } on : shard0001 Timestamp(3, 0) 
            { "id" : 4682 } -->> { "id" : 7023 } on : shard0000 Timestamp(6, 1) 
            { "id" : 7023 } -->> { "id" : 9364 } on : shard0000 Timestamp(1, 3) 
            { "id" : 9364 } -->> { "id" : 13407 } on : shard0000 Timestamp(3, 2) 
            { "id" : 13407 } -->> { "id" : 21295 } on : shard0000 Timestamp(3, 3) 
            { "id" : 21295 } -->> { "id" : 25976 } on : shard0001 Timestamp(4, 2) 
            { "id" : 25976 } -->> { "id" : 33545 } on : shard0001 Timestamp(4, 3) 
            { "id" : 33545 } -->> { "id" : 38226 } on : shard0000 Timestamp(5, 2) 
            { "id" : 38226 } -->> { "id" : 45910 } on : shard0000 Timestamp(5, 3) 
            { "id" : 45910 } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(6, 0) 
#數(shù)據(jù)存放在兩個分片服務(wù)器上即:shard0000、shard0001中。

5.給分片添加標(biāo)簽 :

mongos> sh.status()
......
  shards:
    {  "_id" : "shard0000",  "host" : "192.168.217.134:47017" }
    {  "_id" : "shard0001",  "host" : "192.168.217.134:47018" }
mongos> sh.addShardTag("shard0000","sales00")  #添加標(biāo)簽
mongos> sh.addShardTag("shard0001","sales01")
mongos> sh.status()
......
  shards:
    {  "_id" : "shard0000",  "host" : "192.168.217.134:47017",  "tags" : [ "sales00" ] }
    {  "_id" : "shard0001",  "host" : "192.168.217.134:47018",  "tags" : [ "sales01" ] }

6.刪除分片節(jié)點 :

mongos> use admin

mongos> db.runCommand({"removeshard":"192.168.217.134:47018"})   #刪除分片節(jié)點

ps:MongoDB 4以上的版本做分片,需要先把實例做成復(fù)制集。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI