加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Nosql Mongodb之旅(28)—MongoDB管理维护Sharding

发布时间:2020-12-13 13:45:54 所属栏目:百科 来源:网络整理
导读:1、列出所有的Shard Server [plain] view plain copy db.runCommand({listshards:1})--列出所有的ShardServer { "shards":[ "_id":"shard0000", "host":"localhost:20000" },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21p

1、列出所有的Shard Server

[plain] view plain copy
  1. >db.runCommand({listshards:1})--列出所有的ShardServer
  2. {
  3. "shards":[
  4. "_id":"shard0000",
  5. "host":"localhost:20000"
  6. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "_id":"shard0001",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "host":"localhost:20001"
  7. }
  8. ],
  9. "ok":1
  10. }
2、查看Sharding信息
    >printShardingStatus()--查看Sharding信息
  1. ---ShardingStatus---
  2. shardingversion:{"_id":1,"version":3}
  3. shards:
  4. {"_id":"shard0000","host":"localhost:20000"}
  5. {"_id":"shard0001","host":"localhost:20001"}
  6. databases:
  7. {"_id":"admin","partitioned":false,"primary":"config"}
  8. {"_id":"test","partitioned":true,"primary":"shard0000"}
  9. test.userschunks:
  10. shard00001
  11. {"_id":{$minKey:1}}-->>{"_id":{$maxKey:1}}on:
  12. shard0000{"t":1000,"i":0}
  13. >
3、判断是否是Sharding
    >db.runCommand({isdbgrid:1})
  1. {"isdbgrid":1,"hostname":"localhost","ok":1}
  2. >
4、对现有的集合进行分片(实例)

刚才我们是对表test.users 进行分片了,下面我们将对库中现有的未分片的表test.users_2 进行分片处理。

表最初状态如下,可以看出他没有被分片过:

    >db.users_2.stats()
  1. "ns":"test.users_2",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "sharded":false,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "primary":"shard0000",248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ns":"test.users_2",108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "count":500000,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "size":48000016,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "avgObjSize":96.000032,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "storageSize":61875968,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "numExtents":11,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "nindexes":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "lastExtentSize":15001856,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "paddingFactor":1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "flags":1,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "totalIndexSize":20807680,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "indexSizes":{
  2. "_id_":20807680
  3. "ok":1
  4. }
对其进行分片处理:
    >useadmin
  1. switchedtodbadmin
  2. >db.runCommand({shardcollection:"test.users_2",key:{_id:1}})
  3. {"collectionsharded":"test.users_2","ok":1}
再次查看分片后的表的状态,可以看到它已经被我们分片了
    >usetest
  1. switchedtodbtest
  2. >db.users_2.stats()
  3. "sharded":true,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "count":505462,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> ……
  4. "shards":{
  5. "shard0000":{
  6. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "shard0001":{
  7. ……
  8. }
  9. >

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读