简体   繁体   English

使用MongoDB进行分片

[英]Sharding with MongoDB

I am newbie to MongoDB Sharding. 我是MongoDB分片的新手。 I have created mongos and config server and one shard on xyz.com , and second shard on xyzshard.com server: 我在xyz.com上创建了mongosconfig server以及一个分片,在xyzshard.com服务器上xyzshard.com第二个分xyzshard.com

  • mongos on xyz.com:27019 mongosxyz.com:27019

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

  • 1st shard on xyz.com:27017 xyz.com:27017第一个碎片: xyz.com:27017

  • 2nd shard on xyzshard.com:27017 xyzshard.com:27017第二个碎片: xyzshard.com:27017

My database is test and collection is test_collection . 我的数据库是test ,集合是test_collection I have enabled sharding on test.test_collection . 我已经在test.test_collection上启用了分片。

I have indexed _id field using "hash" 我已经使用"hash"索引了_id字段

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

I run the following command: 我运行以下命令:

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

I get the output below: 我得到以下输出:

WriteResult({ "nInserted" : 1 })

But when I run find query on both shards like this: 但是,当我在两个分片上运行find查询时,如下所示:

db.test_collection.find()

I get all documents on each shard. 我得到每个分片上的所有文档。

Is it the right way or I am going wrong anywhere? 这是正确的方法还是我在任何地方都出错了?

Output for sh.status() on xyz.com is 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" } 

Output for sh.status() on xyzshard.com 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" }

You should connect to your mongos server and insert data on it.You could insert 50000 records for testing purpose. 您应该连接到mongos服务器并在其中插入数据。您可以插入50000条记录以进行测试。 And after that run sh.status(). 然后运行sh.status()。

You would finally see the distribution. 您最终将看到分布。 Remember your config server should be running and all the shard servers are connected to the mongos server. 请记住,您的配置服务器应该正在运行,并且所有分片服务器都已连接到mongos服务器。

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

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