天天看點

大資料 MongoDB 3.2.1 分片

MongoDB 分片

  • 在Mongodb裡面存在另一種叢集,就是分片技術,可以滿足MongoDB資料量大量增長的需求。
  • 當MongoDB存儲海量的資料時,一台機器可能不足以存儲資料,也可能不足以提供可接受的讀寫吞吐量。這時,我們就可以通過在多台機器上分割資料,使得資料庫系統能存儲和處理更多的資料。

分片的目的

  高資料量和吞吐量的資料庫應用會對單機的性能造成較大壓力,大的查詢量會将單機的CPU耗盡,大的資料量對單機的存儲壓力較大,最終會耗盡系統的記憶體而将壓力轉移到磁盤IO上。

解決方法 :

有兩個基本的方法: 垂直擴充和水準擴充。

  • 垂直擴充:增加更多的CPU和存儲資源來擴充容量。
  •  水準擴充:将資料集分布在多個伺服器上。水準擴充即分片

分片結構圖(圖檔來源于網絡) :

大資料 MongoDB 3.2.1 分片

MongoDB 分片群集的組成(圖檔來源于網絡) :

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

Shard:

用于存儲實際的資料塊,實際生産環境中一個shard server角色可由幾台機器組個一個replica set承擔,防止主機單點故障

Config Server:

mongod執行個體,存儲了整個 ClusterMetadata,其中包括 chunk資訊。

分片群集的簡單部署 :

實驗環境 :

1台路由執行個體(端口27017)。

1台配置執行個體(端口37017)。

2台shard執行個體(端口47017、47018)。

1.配置配置伺服器 :

vim mongodb1.conf

port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true        #開啟配置服務
mongod -f /usr/local/mongodb/bin/mongodb1.conf  #開啟配置執行個體           

2.配置分片伺服器 :

vim mongodb2.conf

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

vim mongodb3.conf

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

mongod -f /usr/local/mongodb/bin/mongodb2.conf   #開啟分片執行個體
mongod -f /usr/local/mongodb/bin/mongodb3.conf           

3.啟動路由伺服器 :

[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.添加分片伺服器 :

[root@localhost bin]# mongo
MongoDB shell version: 3.2.1
......
mongos> show dbs
config  0.031GB
mongos> sh.status()      #檢視分片狀态
--- 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()       #檢視分片狀态
--- 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.啟用分片伺服器 :

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

mongos> sh.enableSharding("test")   #啟用資料庫分片

mongos> db.users.createIndex({"id":1})   #建立索引

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) 
#資料存放在兩個分片伺服器上即:shard0000、shard0001中。           

5.給分片添加标簽 :

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")  #添加标簽
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.删除分片節點 :

mongos> use admin

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

繼續閱讀