1. 程式人生 > >大數據 MongoDB 3.2.1 分片

大數據 MongoDB 3.2.1 分片

rec ast parent eat ase jpg pre ada lec

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

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

分片的目的

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

解決方法 :

有兩個基本的方法: 垂直擴展和水平擴展。

  • 垂直擴展:增加更多的CPU和存儲資源來擴展容量。

  •  水平擴展:將數據集分布在多個服務器上。水平擴展即分片

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

技術分享圖片

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

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

Shard:
用於存儲實際的數據塊,實際生產環境中一個shard server角色可由幾臺機器組個一個replica set承擔,防止主機單點故障

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

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

技術分享圖片

分片群集的簡單部署 :

實驗環境 :

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"})   #刪除分片節點

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

大數據 MongoDB 3.2.1 分片