MongoDB整理筆記のSharding分區

來源:互聯網
上載者:User

標籤:

    這是一種將海量的資料水平擴充的資料庫叢集系統,資料分表格儲存體在sharding 的各個節點上,使用者通過簡單的配置就可以很方便地構建一個分布式MongoDB 叢集。
MongoDB 的資料分塊稱為 chunk。每個 chunk 都是 Collection 中一段連續的資料記錄,通常最大尺寸是 200MB,超出則產生新的資料區塊。要構建一個 MongoDB Sharding Cluster,需要三種角色:
    Shard Server
    即儲存實際資料的分區,每個Shard 可以是一個mongod 執行個體,也可以是一組mongod 執行個體構成的Replica Set。為了實現每個Shard 內部的auto-failover,MongoDB 官方建議每個Shard為一組Replica Set。
    Config Server
    為了將一個特定的collection 儲存在多個shard 中,需要為該collection 指定一個shard key,例如{age: 1} ,shard key 可以決定該條記錄屬於哪個chunk。Config Servers 就是用來儲存:所有shard 節點的配置資訊、每個chunk 的shard key 範圍、chunk 在各shard 的分布情況、該叢集中所有DB 和collection 的sharding 配置資訊。
    Route Process
    這是一個前端路由,用戶端由此接入,然後詢問Config Servers 需要到哪個Shard 上查詢或儲存記錄,再串連相應的Shard 進行操作,最後將結果返回給用戶端。用戶端只需要將原本發給mongod 的查詢或更新要求原封不動地發給Routing Process,而不必關心所操作的記錄儲存在哪個Shard 上。
    下面我們在同一台物理機器上構建一個簡單的 Sharding Cluster:

    架構圖如下:

    

    Shard Server 1:20000
     Shard Server 2:20001
    Config Server :30000
     Route Process:40000

  (1)啟動三服務

     啟動Shard Server 

mkdir -p /data/shard/s0 --建立資料目錄mkdir -p /data/shard/s1mkdir -p /data/shard/log --建立日誌目錄/Apps/mongo/bin/mongod --shardsvr --port 20000 --dbpath /data/shard/s0 --fork --logpath/data/shard/log/s0.log --directoryperdb --啟動Shard Server 執行個體1/Apps/mongo/bin/mongod --shardsvr --port 20001 --dbpath /data/shard/s1 --fork --logpath/data/shard/log/s1.log --directoryperdb --啟動Shard Server 執行個體2
View Code

    啟動Config Server

mkdir -p /data/shard/config --建立資料目錄/Apps/mongo/bin/mongod --configsvr --port 30000 --dbpath /data/shard/config --fork --logpath/data/shard/log/config.log --directoryperdb --啟動Config Server 執行個體

    啟動Route Process

/Apps/mongo/bin/mongos --port 40000 --configdb localhost:30000 --fork --logpath/data/shard/log/route.log --chunkSize 1 --啟動Route Server 執行個體

   mongos 啟動參數中,chunkSize 這一項是用來指定chunk 的大小的,單位是MB,預設大小為200MB,為了方便測試Sharding 效果,我們把chunkSize 指定為 1MB。
  (2)配置Sharding

   接下來,我們使用MongoDB Shell 登入到mongos,添加Shard 節點

[[email protected] ~]# /Apps/mongo/bin/mongo admin --port 40000 --此操作需要串連admin 庫MongoDB shell version: 1.8.1connecting to: 127.0.0.1:40000/admin> db.runCommand({ addshard:"localhost:20000" }) --添加 Shard Server{ "shardAdded" : "shard0000", "ok" : 1 }> db.runCommand({ addshard:"localhost:20001" }){ "shardAdded" : "shard0001", "ok" : 1 }> db.runCommand({ enablesharding:"test" }) --設定分區儲存的資料庫{ "ok" : 1 }> db.runCommand({ shardcollection: "test.users", key: { _id:1 }}) --設定分區的集合名稱,且必須指定Shard Key,系統會自動建立索引{ "collectionsharded" : "test.users", "ok" : 1 }>
View Code

  (3)驗證Sharding正常工作
   我們已經對test.users 表進行了分區的設定,下面我們們插入一些資料看一下結果

> use testswitched to db test> for (var i = 1; i <= 500000; i++) db.users.insert({age:i, name:"wangwenlong", addr:"Beijing",country:"China"})> db.users.stats(){"sharded" : true, --說明此表已被shard"ns" : "test.users","count" : 500000,"size" : 48000000,"avgObjSize" : 96,"storageSize" : 66655232,"nindexes" : 1,"nchunks" : 43,"shards" : {"shard0000" : { --在此分區執行個體上約有24.5M 資料"ns" : "test.users","count" : 254889,"size" : 24469344,"avgObjSize" : 96,"storageSize" : 33327616,"numExtents" : 8,"nindexes" : 1,"lastExtentSize" : 12079360,"paddingFactor" : 1,"flags" : 1,"totalIndexSize" : 11468800,"indexSizes" : {"_id_" : 11468800},"ok" : 1},"shard0001" : { --在此分區執行個體上約有23.5M 資料"ns" : "test.users","count" : 245111,"size" : 23530656,"avgObjSize" : 96,"storageSize" : 33327616,"numExtents" : 8,"nindexes" : 1,"lastExtentSize" : 12079360,"paddingFactor" : 1,"flags" : 1,"totalIndexSize" : 10649600,"indexSizes" : {"_id_" : 10649600},"ok" : 1}},"ok" : 1}>
View Code

   我們看一下磁碟上的物理檔案情況

[[email protected] bin]# ll /data/shard/s0/test --此分區執行個體上有資料產生總計 262420-rw------- 1 root root 16777216 06-03 15:21 test.0-rw------- 1 root root 33554432 06-03 15:21 test.1-rw------- 1 root root 67108864 06-03 15:22 test.2-rw------- 1 root root 134217728 06-03 15:24 test.3-rw------- 1 root root 16777216 06-03 15:21 test.ns[[email protected] bin]# ll /data/shard/s1/test --此分區執行個體上有資料產生總計 262420-rw------- 1 root root 16777216 06-03 15:21 test.0-rw------- 1 root root 33554432 06-03 15:21 test.1-rw------- 1 root root 67108864 06-03 15:22 test.2-rw------- 1 root root 134217728 06-03 15:23 test.3-rw------- 1 root root 16777216 06-03 15:21 test.ns[[email protected] bin]#
View Code

   看上述結果,表明test.users 集合已經被分區處理了,但是通過mongos 路由,我們並感覺不到是資料存放在哪個shard 的chunk 上的,這就是MongoDB 使用者體驗上的一個優勢,即對使用者是透明的。

MongoDB整理筆記のSharding分區

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.