2015-06-03 39 views
0

我有mongoDb副本集,一個主要的一個次要和一個仲裁者進行投票。我打算實施分片,因爲數據預計會呈指數級增長。我發現下面的mongoDb文檔難以分片。有人可以解釋清楚,設置它。提前致謝。在複製組中分片MongoDB

回答

0

如果您可以完成replicaset,則sharding非常簡單。在這裏快速轉發mongo文檔非常多:

下面是一個示例設置:3 configDB和3個分片 對於下面的示例,您可以運行它所有的一臺機器看到它的所有工作。

  1. 如果您需要三個分片設置三個副本集。 (假設3個Primary的值爲127:0.0.1:27000,127.0.0.1:37000,127.0.0.1:47000)
  2. 運行3個實例mongod作爲3個配置服務器。 (假設:127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:270122)
  3. 啓動mongos(注意mongos中的s)讓它知道你的配置服務器在哪裏。 (例如:127.0.0.1:27023)
  4. 從mongo shell連接到mongos,並將3個副本集中的三個主mongod添加爲碎片。
  5. 爲您的數據庫啓用分片。
  6. 如果需要,爲集合啓用分片。
  7. 如果需要,請選擇分片鍵。 (非常重要,你第一次就做對了!!!)
  8. 檢查分片狀態
  9. 泵的數據;連接到單個mongod主體並查看分佈在三個分片上的數據。

#start mongos with three configs: 
mongos --port 27023 --configdb localhost:27017,localhost:27018,localhost:27019 

mongos> sh.addShard("127.0.0.1:27000"); 
{ "shardAdded" : "shard0000", "ok" : 1 } 
mongos> sh.addShard("127.0.0.1:37000"); 
{ "shardAdded" : "shard0001", "ok" : 1 } 
mongos> sh.addShard("127.0.0.1:47000"); 
{ "shardAdded" : "shard0002", "ok" : 1 } 
mongos> sh.enableSharding("db_to_shard"); 
{ "ok" : 1 } 
mongos> use db_to_shard; 
switched to db db_to_shard 
mongos> 
mongos> sh.shardCollection("db_to_shard.coll_to_shard", {collId: 1, createdDate: 1}); 
{ "collectionsharded" : "db_to_shard.coll_to_shard", "ok" : 1 } 
mongos> show databases; 
admin  (empty) 
config  0.063GB 
db_to_shard 0.078GB 
mongos> sh.status(); 
--- Sharding Status --- 
    sharding version: { 
     "_id" : 1, 
     "minCompatibleVersion" : 5, 
     "currentVersion" : 6, 
     "clusterId" : ObjectId("557003eb4a4e61bb2ea0555b") 
} 
    shards: 
     { "_id" : "shard0000", "host" : "127.0.0.1:27000" } 
     { "_id" : "shard0001", "host" : "127.0.0.1:37000" } 
     { "_id" : "shard0002", "host" : "127.0.0.1:47000" } 
    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: 
     { "_id" : "admin", "partitioned" : false, "primary" : "config" } 
     { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } 
     { "_id" : "db_to_shard", "partitioned" : true, "primary" : "shard0000" } 
       db_to_shard.coll_to_shard 
         shard key: { "collId" : 1, "createdDate" : 1 } 
         chunks: 
           shard0000  1 
         { "collId" : { "$minKey" : 1 }, "createdDate" : { "$minKey" : 1 } } -->> { "collId" : { "$maxKey" : 1 }, "createdDate" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)