mongodb将所有数据存储到一个分片

问题描述

我正在尝试使用一台配置服务器,一台路由器和两台具有一个节点的sard来设置分片。但是,所有数据仅定向到分片上。
以下是我要执行的步骤。

mongod --port 27019 --configsvr --dbpath "config server db path" --replSet rs01
    rs.initiate(
        {
            _id: "rs01",configsvr: true,members: [
                { _id: 0,host : "localhost:27019" }
            ]
        }
    )

mongod --config "path\mongod.cfg" --shardsvr --replSet rs02
    rs.initiate(
        {
            _id: "rs02",host : "localhost:27040" }
            ]
        }
    )
mongod --config "path\mongod.cfg" --shardsvr --replSet rs03
    rs.initiate(
        {
            _id: "rs03",host : "localhost:27041" }
            ]
        }
    )
mongos --configdb rs01/localhost:27019
sh.addShard( "rs02/localhost:27040")
sh.addShard( "rs03/localhost:27041")
sh.enableSharding("social")

直到这里没有集合或数据
这是“社交”数据库的“收件箱”集合中的预期数据。

{
    "to": ["a","b"],"from": 12,"message": "hello world"
}

此后,我将从连接到路由器服务器的nod​​ejs代码执行以下代码

  1. 在“收件箱”集合的“发件人”字段上创建索引
  2. 在“收件箱”集合的“来自”字段上创建分片键

以下是分片状态的结果

--- Sharding Status ---
  sharding version: {
        "_id" : 1,"minCompatibLeversion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("5f95862f3eef5ee1bb33a6a3")
  }
  shards:
        {  "_id" : "rs02","host" : "rs02/localhost:27040","state" : 1 }
        {  "_id" : "rs03","host" : "rs03/localhost:27041","state" : 1 }
  active mongoses:
        "4.2.2" : 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:
                No recent migrations
  databases:
        {  "_id" : "config","primary" : "config","partitioned" : true }
        {  "_id" : "social","primary" : "rs02","partitioned" : true,"version" : {  "uuid" : UUID("6d3b8ab0-fbd0-42a0-b911-1498a2244ce1"),"lastMod" : 1 } }
                social.inBox
                        shard key: { "from" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rs02    1
                        { "from" : { "$minKey" : 1 } } -->> { "from" : { "$maxKey" : 1 } } on : rs02 Timestamp(1,0)

此处minKey和maxKey为1,所有数据都保存在主(rs02)碎片中,而不拆分到另一个碎片(rs03),我什至不愿意插入1000多个文档。另外,我将块大小减小到1MB
以下是db.getCollection('inBox')。getSharddistribution()

的结果
Shard rs02 at rs02/localhost:27040
 data : 100KiB docs : 51 chunks : 1
 estimated data per chunk : 100KiB
 estimated docs per chunk : 51

Totals
 data : 100KiB docs : 51 chunks : 1
 Shard rs02 contains 100% data,100% docs in cluster,avg obj size on shard : 1KiB

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)