MongoDB分區配置系列一:

來源:互聯網
上載者:User

標籤:

接這篇部落格:

http://www.cnblogs.com/xiaoit/p/4479066.html

這裡不再說明安裝過程。

1:分區簡介

分區是一種將海量的資料水平擴充的資料庫叢集系統,資料分表格儲存體在sharding的各個節點上,使用者通過簡單的配置就可以很方便地構建一個分布式MongoDB叢集。MongoDB的資料分塊稱為chunk。每個chunk都是Collection中一段連續的資料記錄,通常最大尺寸是200MB,超出則產生新的資料區塊。

2:

配置MongoDB的Sharding Cluster需要3中角色:
a:Shard Server
Shard Server 即儲存資料的分區,每個Shard可以是一個執行個體,也可以是一個複本集。為了實現每個Shard內部的auto-failover,官方建議,每一個Shard為一組Replica Set .
b:Config Server
為了將一個特定的collection存放在多個shard中。需要為collection指定一個shard key,例如{age:1}.shard key 決定了資料存放在哪個分區中。Config Server 就是用來儲存所有Shard節點的配置資訊、分區的分布情況、該叢集中所有DB和collection的sharding配置資訊。
c:Route Process
這是一個前端的路由進程,用戶端由此進入。然後會詢問Config Server需要到哪個Shard上查詢或者儲存記錄?再串連相應的Shard進行操作,最後將結果返回給用戶端。用戶端只需原封不動的將原來發給Mongod的查詢或者更新要求發給Route Process即可。不用關心文檔到底儲存在哪個Shard上面。

在一台機器上類比環境配置分區
Shard Server 1:27017
Shard Server 2:27018
Config Server :27019
Route Process:40000

3:建立資料目錄並保證有讀寫權限

mkdir -p ./data/shard/{s01,s02}

4:啟動Shard Server1:

[[email protected] mongodb]# ./bin/mongod --port 27017 --dbpath ./data/shard/s01 --logpath ./log/s01 --forkabout to fork child process, waiting until server is ready for connections.forked process: 54045child process started successfully, parent exiting

  

啟動Shard Server2:

[[email protected] mongodb]# ./bin/mongod --port 27018 --dbpath ./data/shard/s02 --logpath ./log/s02 --forkabout to fork child process, waiting until server is ready for connections.forked process: 54065child process started successfully, parent exiting

  

啟動Config Server

[[email protected] mongodb]# mkdir -p ./data/shard/config[[email protected] mongodb]# ./bin/mongod --port 27019 --dbpath ./data/shard/config --logpath ./log/configlog --forkabout to fork child process, waiting until server is ready for connections.forked process: 54111child process started successfully, parent exiting

注意:我們完全可以像啟動普通Mongod服務一樣啟動,不需要添加-shardsvr和configsvr參數。因為兩個參數的作用就是改變啟動連接埠的,這裡我們自行指定連接埠就可以了。

 

啟動Route Process 

[[email protected] mongodb]# ./bin/mongos --port 40000 --fork --logpath ./data/shard/route.log --chunkSize 1 --configdb localhost:270192015-05-05T01:28:04.996-0700 warning: running with 1 config server should be done only for testing purposes and is not recommended for productionabout to fork child process, waiting until server is ready for connections.forked process: 54169child process started successfully, parent exiting

chunkSize :指定chunk的大小,預設為200M。為了方便測試指定為1M。意思是當這個分區中插入的資料大於1M時候開始資料轉移。

 

5:配置Sharding

此時直接連mongos路由進程即可

[[email protected] mongodb]# ./bin/mongo admin --port 40000MongoDB shell version: 2.6.9connecting to: 127.0.0.1:40000/adminmongos> 
[[email protected] mongodb]# ./bin/mongo admin --port 40000MongoDB shell version: 2.6.9connecting to: 127.0.0.1:40000/adminmongos> db.runCommand({addshard:"localhost:27017"}){ "shardAdded" : "shard0000", "ok" : 1 }mongos> db.runCommand({addshard:"localhost:27018"}){ "shardAdded" : "shard0001", "ok" : 1 }mongos> db.runCommand({enablesharding:"test"}){ "ok" : 1 }mongos> db.runCommand({shardcollection:"test.users",key:{id:1}}){ "collectionsharded" : "test.users", "ok" : 1 }mongos> 

  

注意這裡我們要注意片鍵的選擇,選擇片鍵時需要根據具體業務的資料形態來選擇,切不可隨意選擇,實際中尤其不要輕易選擇自增_id作為片鍵,除非你很清楚你這麼做的目的,根據經驗推薦一種較合理的片鍵方式,“自增欄位+查詢欄位”。另外這裡說明一點,分區的基本機制:分區總是試圖將現有資料均分到所有的分區上。舉例說,現在有兩個分區,我已經選擇了id作為片鍵,假定插入的id是自增的,如1——10000,則分區後的結果是均分,即1——5000在片A,5000——10000在片B,當然,不一定有這麼精確,但卻是保證盡量的平均的,以此類推,如果有三塊分區,同樣均分三等分。還需要說明的是,一開始插入資料時,資料是只插入到其中一塊分區上的,插入完畢後,mongodb內部開始在各片之間進行資料的移動,這個過程可能不是立即的,mongodb足夠智能會根據當前負載決定是立即進行移動還是稍後移動。
在插入資料後,立馬執行db.users.stats();兩次可以驗證如上所說。

 

MongoDB分區配置系列一:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.