Mongodb

無法分片收集

  • October 7, 2021

我已使用以下命令啟用分片。

mongos> use news
switched to db news
mongos> sh.enableSharding("news");
{
   "ok" : 1,
   "$clusterTime" : {
       "clusterTime" : Timestamp(1633500027, 1),
       "signature" : {
           "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
           "keyId" : NumberLong(0)
       }
   },
   "operationTime" : Timestamp(1633500026, 1)
}
mongos> db.createCollection("articles");
{
   "ok" : 1,
   "$clusterTime" : {
       "clusterTime" : Timestamp(1633500039, 2),
       "signature" : {
           "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
           "keyId" : NumberLong(0)
       }
   },
   "operationTime" : Timestamp(1633500039, 2)
}
mongos> sh.shardCollection("news.articles", {"dc": 1});
{
   "collectionsharded" : "news.articles",
   "ok" : 1,
   "$clusterTime" : {
       "clusterTime" : Timestamp(1633500077, 25),
       "signature" : {
           "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
           "keyId" : NumberLong(0)
       }
   },
   "operationTime" : Timestamp(1633500077, 21)
}
mongos> sh.status();
--- Sharding Status ---
 sharding version: {
   "_id" : 1,
   "minCompatibleVersion" : 5,
   "currentVersion" : 6,
   "clusterId" : ObjectId("615d26c277fa05dc0bf74e23")
 }
 shards:
       {  "_id" : "shard1rs",  "host" : "shard1rs/192.168.226.180:50001,192.168.226.180:50002,192.168.226.180:50003",  "state" : 1,  "topologyTime" : Timestamp(1633495862, 1),  "tags" : [ "bos" ] }
       {  "_id" : "shard2rs",  "host" : "shard2rs/192.168.226.180:50004,192.168.226.180:50005,192.168.226.180:50006",  "state" : 1,  "topologyTime" : Timestamp(1633496033, 2),  "tags" : [ "dfw" ] }
 active mongoses:
       "5.0.3" : 1
 autosplit:
       Currently enabled: yes
 balancer:
       Currently enabled:  yes
       Currently running:  no
       Failed balancer rounds in last 5 attempts:  0
       Migration Results for the last 24 hours:
               512 : Success
 databases:
       {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
               config.system.sessions
                       shard key: { "_id" : 1 }
                       unique: false
                       balancing: true
                       chunks:
       {  "_id" : "news",  "primary" : "shard2rs",  "partitioned" : true,  "version" : {  "uuid" : UUID("dde4cff3-a2e4-4c81-87c1-4fd306370986"),  "timestamp" : Timestamp(1633500025, 2),  "lastMod" : 1 } }
               news.articles
                       shard key: { "dc" : 1 }
                       unique: false
                       balancing: true
                       chunks:

即使,我已經使用上述命令對集合進行了分片,但 getShardDistribution() 返回的集合沒有被分片。

mongos> db.articles.getShardDistribution();
Collection news.articles is not sharded.

如果我在這裡遺漏了什麼,請告訴我。

您需要將數據添加到集合中。只有這樣系統才能知道“如何”共享數據。在此之前,集合沒有分片。

作為解決方法,您可以執行此查詢:

db.getSiblingDB("config").chunks.aggregate([
  { $match: { ns: "news.articles" } },
  { $group: { _id: { shard: "$shard", ns: "$ns" }, chunks: { $sum: 1 } } },
  { $group: { _id: "$_id.ns", shards: { $push: { shard: "$_id.shard", chunks: "$chunks" } } } },
  { $unwind: "$shards" },
  { $group: { _id: "$_id", shards: { $push: { k: "$shards.shard", v: "$shards.chunks" } } } },
  { $replaceRoot: { newRoot: { $mergeObjects: [{ns:"$_id"}, { $arrayToObject: "$shards" }] } } },
  { $sort: { ns: 1 } }
])

引用自:https://dba.stackexchange.com/questions/300706