【问题标题】:Sharding with MongoDB使用 MongoDB 进行分片
【发布时间】:2015-10-20 16:38:42
【问题描述】:

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

  • mongosxyz.com:27019

  • config serverxyz.com:27018

  • xyz.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 })

但是当我像这样在两个分片上运行查找查询时:

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" }

【问题讨论】:

  • 你的意思是你得到了 500 个文档作为对 both 分片的find 查询的结果?或者总结一下,例如xyz.com 200,xyzshard.com 300?
  • 是的...我在两个分片上各获得 500 个文档
  • 你得到什么输出?可以贴吗?
  • 嗨,我已经在问题中添加了输出

标签: mongodb sharding


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2022-10-03
    • 1970-01-01
    • 2020-06-21
    • 2014-06-13
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多