阅读 123

MongoDB集群分片管理之范围分片

查看数据库分片情况

登录后复制

mongos> use config
switched to db config
mongos> db.databases.find()
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : true, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }
{ "_id" : "db1", "primary" : "hdshard3", "partitioned" : true, "version" : { "uuid" : UUID("71bb472c-7896-4a31-a77c-e3aaf723be3c"), "lastMod" : 1 } }

可以看到recommend库已激活数据库分片。但是recommend下的集合都没有开启分片,查阅相关资料显示,没有分片的集合,全部存储在主分片上,每个数据库都有自己的主分片
 
可以看到recommend库主分片为hdshard1,我们登录hdshard1,hdshard2,hdshard3验证一下

查看分片集详细信息

登录后复制

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {    "_id" : 1,    "minCompatibleVersion" : 5,    "currentVersion" : 6,    "clusterId" : ObjectId("60545017224c766911a9c440")
  }
  shards:
        {  "_id" : "hdshard1",  "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001",  "state" : 1 }
        {  "_id" : "hdshard2",  "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002",  "state" : 1 }
        {  "_id" : "hdshard3",  "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003",  "state" : 1 }
  active mongoses:        "4.2.12" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  Could not find host matching read preference { mode: "primary" } for set hdshard2
        Time of Reported error:  Thu Apr 15 2021 21:50:43 GMT+0800 (CST)
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                hdshard1    342
                                hdshard2    341
                                hdshard3    341
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "db1",  "primary" : "hdshard3",  "partitioned" : true,  "version" : {  "uuid" : UUID("71bb472c-7896-4a31-a77c-e3aaf723be3c"),  "lastMod" : 1 } }
        {  "_id" : "recommend",  "primary" : "hdshard1",  "partitioned" : true,  "version" : {  "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"),  "lastMod" : 1 } }

hdshard1对应172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001三个节点。

登录三个节点进行验证

hdshard1(172.16.254.136:40001)

登录后复制

hdshard1:PRIMARY> show dbsadmin       0.000GBconfig      0.001GBlocal       5.556GBrecommend  26.885GBhdshard1:PRIMARY>

hdshard2(172.16.254.136:40002)

登录后复制

hdshard2:SECONDARY> rs.secondaryOk()hdshard2:SECONDARY> show dbsadmin   0.000GBconfig  0.000GBlocal   0.001GB

hdshard3(172.16.254.136:40003)

登录后复制

hdshard3:SECONDARY> rs.secondaryOk()hdshard3:SECONDARY> show dbsadmin   0.000GBconfig  0.000GBdb1     0.000GBlocal   0.005GB

可以看到集合没有分片的情况下,只在当前数据库主分片上存储数据。
 
接下来我们对recommend下的集合rcmd_1_tag_li_liao进行分片

对集合进行分片

登录后复制

mongos> sh.shardCollection("recommend.rcmd_1_tag_li_liao",{"id": 1})
{    "ok" : 0,    "errmsg" : "can't shard collection 'recommend.rcmd_1_tag_li_liao' with unique index on { userid: 1.0 } and proposed shard key { id: 1.0 }. Uniqueness can't be maintained unless shard key is a prefix",    "code" : 72,    "codeName" : "InvalidOptions",    "operationTime" : Timestamp(1618559308, 5),    "$clusterTime" : {        "clusterTime" : Timestamp(1618559308, 5),        "signature" : {            "hash" : BinData(0,"CgMa7JLUEDu3S3C8soeL90Nlh6M="),            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

可以看到,报错提示说userid字段存在唯一索引。

查看集合索引

登录后复制

mongos> db.rcmd_1_tag_li_liao.getIndexes()
[
    {        "v" : 2,        "key" : {            "_id" : 1
        },        "name" : "_id_",        "ns" : "recommend.rcmd_1_tag_li_liao"
    },
    {        "v" : 2,        "unique" : true,        "key" : {            "userid" : 1
        },        "name" : "userid_1.0",        "ns" : "recommend.rcmd_1_tag_li_liao",        "background" : true,        "sparse" : true
    }
]
mongos>

根据mongodb要求:
只有_id字段索引、分片键上的索引、或分片键是前缀的复合索引可以是唯一索引,如果其他字段有唯一索引,则不能对集合进行分片,也不能在分片集合的其他字段上创建唯一索引。
 
为了测试,我们把userid上的唯一索引删除。

登录后复制

mongos> db.getCollection('rcmd_1_tag_li_liao').dropIndex('userid_1.0')
{    "raw" : {        "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001" : {            "nIndexesWas" : 2,            "ok" : 1
        }
    },    "ok" : 1,    "operationTime" : Timestamp(1618559955, 1),    "$clusterTime" : {        "clusterTime" : Timestamp(1618559955, 1),        "signature" : {            "hash" : BinData(0,"Ecbu/vwdtc8Z/alveXdkF5vsgyM="),            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

再次检查索引

登录后复制

mongos> db.rcmd_1_tag_li_liao.getIndexes()
[
    {        "v" : 2,        "key" : {            "_id" : 1
        },        "name" : "_id_",        "ns" : "recommend.rcmd_1_tag_li_liao"
    }
]

对集合进行范围分片

登录后复制

mongos> sh.shardCollection("recommend.rcmd_1_tag_li_liao",{"_id": 1})
{    "collectionsharded" : "recommend.rcmd_1_tag_li_liao",    "collectionUUID" : UUID("b4698624-8497-441c-9bc9-3e2ad8a6041c"),    "ok" : 1,    "operationTime" : Timestamp(1618560118, 113),    "$clusterTime" : {        "clusterTime" : Timestamp(1618560118, 113),        "signature" : {            "hash" : BinData(0,"TInnJYpb+JhcreThZ1+gkl6TUqg="),            "keyId" : NumberLong("6941260985399246879")
        }
    }
}

查看集合分片情况

登录后复制

mongos> db.rcmd_1_tag_li_liao.getShardDistribution()

Shard hdshard1 at hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001
 data : 3.32GiB docs : 379143 chunks : 82
 estimated data per chunk : 41.47MiB
 estimated docs per chunk : 4623Shard hdshard2 at hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002
 data : 398.34MiB docs : 46384 chunks : 12
 estimated data per chunk : 33.19MiB
 estimated docs per chunk : 3865Shard hdshard3 at hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003
 data : 400.34MiB docs : 46383 chunks : 13
 estimated data per chunk : 30.79MiB
 estimated docs per chunk : 3567Totals
 data : 4.1GiB docs : 471910 chunks : 107
 Shard hdshard1 contains 80.98% data, 80.34% docs in cluster, avg obj size on shard : 9KiB
 Shard hdshard2 contains 9.48% data, 9.82% docs in cluster, avg obj size on shard : 8KiB
 Shard hdshard3 contains 9.53% data, 9.82% docs in cluster, avg obj size on shard : 8KiB

到此集合范围分片已经完成。


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐