在复制集MongoDB中进行分片

时间:2015-06-03 04:55:03

标签: mongodb sharding

我有mongoDb副本集,一个主要的一个次要和一个仲裁者投票。我计划实现分片,因为预计数据会呈指数级增长。我发现很难跟踪mongoDb文档进行分片。有人可以清楚解释它来设置它。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果你能完成复制,那么分片就很简单了。在快进中重复mongo文档:

下面是一个示例设置:3个configDB和3个分片 对于下面的示例,您可以在一台计算机上运行所有计算机以查看它是否正常工作。

  1. 如果您需要三个分片设置三个副本集。 (假设3个主要是127:0.0.1:27000,127.0.0.1:37000,127.0.0.1:47000)
  2. 将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 primarys并查看分布在三个分片中的数据。
  10. #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)
    

    #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)