繁体   English   中英

使用MongoDB进行分片

[英]Sharding with MongoDB

我是MongoDB分片的新手。 我在xyz.com上创建了mongosconfig server以及一个分片,在xyzshard.com服务器上xyzshard.com第二个分xyzshard.com

  • mongosxyz.com:27019

  • xyz.com:27018 config server xyz.com:27018

  • xyz.com:27017第一个碎片: xyz.com:27017

  • xyzshard.com:27017第二个碎片: xyzshard.com:27017

我的数据库是test ,集合是test_collection 我已经在test.test_collection上启用了分片。

我已经使用"hash"索引了_id字段

db.test_collection.ensureIndex( { _id : "hashed" } )

我运行以下命令:

for (var i = 1; i <= 500; i++) 
    db.test_collection.insert( { x : i } )

我得到以下输出:

WriteResult({ "nInserted" : 1 })

但是,当我在两个分片上运行find查询时,如下所示:

db.test_collection.find()

我得到每个分片上的所有文档。

这是正确的方法还是我在任何地方都出错了?

xyz.com上sh.status()的输出是

sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("55b8d3e9a8742d49164820b6")
}
  shards:
    {  "_id" : "shard0000",  "host" : "localhost:27017" }
  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" : true,  "primary" : "shard0000" }
        test.test_collection
            shard key: { "_id" : "hashed" }
            chunks:
                shard0000   2
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard0000 Timestamp(1, 1) 
            { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2) 
    {  "_id" : "test_db",  "partitioned" : false,  "primary" : "shard0000" }
    {  "_id" : "db",  "partitioned" : false,  "primary" : "shard0000" } 

xyzshard.com上sh.status()的输出

sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("55b8d3e9a8742d49164820b6")
}
  shards:
    {  "_id" : "shard0000",  "host" : "localhost:27017" }
  databases:
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    {  "_id" : "test",  "partitioned" : true,  "primary" : "shard0000" }
        test.test_collection
            shard key: { "_id" : "hashed" }
            chunks:
                shard0000   2
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard0000 Timestamp(1, 1) 
            { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2) 
    {  "_id" : "test_db",  "partitioned" : false,  "primary" : "shard0000" }
    {  "_id" : "db",  "partitioned" : false,  "primary" : "shard0000" }

您应该连接到mongos服务器并在其中插入数据。您可以插入50000条记录以进行测试。 然后运行sh.status()。

您最终将看到分布。 请记住,您的配置服务器应该正在运行,并且所有分片服务器都已连接到mongos服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM