mongodb shard 分片技术
admin
2023-04-12 16:42:04
0

启动route process

配置route process并且启动,代码如下:

[root@localhost ~]# /usr/local/mongo/bin/mongos --port 40000 --configdb localhost:30000 --fork --logpath /data/shard/log/route.log --chunkSize 1
forked process: 2911
[root@localhost ~]# all output going to: /data/shard/log/route.log

配置sharding

[root@localhost ~]# /usr/local/mongo/bin/mongo --port 40000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:40000/test
mongos>

这个地方一定要切换到admin数据库

mongos> use admin
switched to db admin
mongos> db
admin
mongos> db.runCommand({addshard:"localhost:20000"})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"localhost:20001"})
{ "shardAdded" : "shard0001", "ok" : 1 }

mongos> db.runCommand({enablesharding:"test"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"test.users",key:{_id:1}})
{ "collectionsharded" : "test.users", "ok" : 1 }

 

 

 


momongos> for(var i =1;i <= 500000;i++) db.users.insert({age:i,name:"fangwei",addr:"hangzhou",country:"China"})
mongos> db.users.find()
{ "_id" : ObjectId("53d4eefd67d21d1d69f5bcd0"), "age" : 1 }
{ "_id" : ObjectId("53d4f024b1e3fc47712a0576"), "age" : 1, "name" : "fangwei", "addr" : "hangzhou", "country" : "China" }
{ "_id" : ObjectId("53d4f024b1e3fc47712a0577"), "age" : 2, "name" : "fangwei", "addr" : "hangzhou", "country" : "China" }ngos>

 

---验证我们的分片

mongos> db.users.stats()
{
 "sharded" : true,
 "flags" : 1,
 "ns" : "test.users",
 "count" : 500001,
 "numExtents" : 17,
 "size" : 46000036,
 "storageSize" : 81166336,
 "totalIndexSize" : 18845680,
 "indexSizes" : {
  "_id_" : 18845680
 },
 "avgObjSize" : 91.999888000224,
 "nindexes" : 1,
 "nchunks" : 37,
 "shards" : {
  "shard0000" : {
   "ns" : "test.users",
   "count" : 197093,
   "size" : 18132500,

   "avgObjSize" : 91.99971587017296,
   "storageSize" : 33333248,
   "numExtents" : 8,
   "nindexes" : 1,
   "lastExtentSize" : 12083200,
   "paddingFactor" : 1,
   "flags" : 1,
   "totalIndexSize" : 8969072,
   "indexSizes" : {
    "_id_" : 8969072
   },
   "ok" : 1
  },
  "shard0001" : {
   "ns" : "test.users",
   "count" : 302908,
   "size" : 27867536,

   "avgObjSize" : 92,
   "storageSize" : 47833088,
   "numExtents" : 9,
   "nindexes" : 1,
   "lastExtentSize" : 14499840,
   "paddingFactor" : 1,
   "flags" : 1,
   "totalIndexSize" : 9876608,
   "indexSizes" : {
    "_id_" : 9876608
   },
   "ok" : 1
  }
 },
 "ok" : 1
}
mongos>

 

查看磁盘上物理文件分布情况

mongos> exit
bye
[root@localhost ~]# ll /data/shard/s0/test
total 213004
-rw-------. 1 root root  67108864 Jul 27 20:28 test.0
-rw-------. 1 root root 134217728 Jul 27 20:09 test.1
-rw-------. 1 root root  16777216 Jul 27 20:28 test.ns
drwxr-xr-x. 2 root root      4096 Jul 27 20:09 _tmp
[root@localhost ~]#

[root@localhost ~]# ll /data/shard/s1/test
total 475148
-rw-------. 1 root root  67108864 Jul 27 20:38 test.0
-rw-------. 1 root root 134217728 Jul 27 20:27 test.1
-rw-------. 1 root root 268435456 Jul 27 20:27 test.2
-rw-------. 1 root root  16777216 Jul 27 20:38 test.ns
drwxr-xr-x. 2 root root      4096 Jul 27 20:27 _tmp
[root@localhost ~]#

---列出所有的shard server

mongos> use admin
switched to db admin
mongos> db.runCommand({listshards:1})
{
 "shards" : [
  {
   "_id" : "shard0000",
   "host" : "localhost:20000"
  },
  {
   "_id" : "shard0001",
   "host" : "localhost:20001"
  }
 ],
 "ok" : 1
}

-----查看sharding信息

mongos> printShardingSizes()
--- Sharding Status ---
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "shard0000", "host" : "localhost:20000" }
      { "_id" : "shard0001", "host" : "localhost:20001" }
  databases:
 { "_id" : "admin", "partitioned" : false, "primary" : "config" }
 { "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
  test.users chunks:
   { "_id" : { $minKey : 1 } } -->> { "_id" : ObjectId("53d4eefd67d21d1d69f5bcd0") } on : shard0000 { "estimate" : false, "size" : 0, "numObjects" : 0 }
   { "_id" : ObjectId("53d4eefd67d21d1d69f5bcd0") } -->> { "_id" : ObjectId("53d4f024b1e3fc47712a1dd6") } on : shard0000 { "estimate" : false, "size" : 574116, "numObjects" : 6241 }
 

-------判断是佛sharding

> db.runCommand({isdbgrid:1})
{ "isdbgrid" : 1, "hostname" : "localhost.mongo102", "ok" : 1 }
>

----------对现有表执行sharding

> db
test
> db.users_2.stats()
{
 "sharded" : false,                               ---------------------可看出没有分表
 "primary" : "shard0000",
 "errmsg" : "ns not found",
 "ok" : 0
}

对其进行分表

> db
admin
> db.runCommand({shardcollection:"test.users_2",key:{_ud:1}})
{ "collectionsharded" : "test.users_2", "ok" : 1 }

> use test
switched to db test
> db.users_2.stats()
{
 "sharded" : true,
 "flags" : 1,
 "ns" : "test.users_2",
 "count" : 0,
 "numExtents" : 1,
 "size" : 0,
 "storageSize" : 8192,
 "totalIndexSize" : 16352,
 "indexSizes" : {
  "_id_" : 8176,
  "_ud_1" : 8176
 },
 "avgObjSize" : 0,
 "nindexes" : 2,
 "nchunks" : 1,
 "shards" : {
  "shard0000" : {
   "ns" : "test.users_2",
   "count" : 0,
   "size" : 0,
   "storageSize" : 8192,
   "numExtents" : 1,
   "nindexes" : 2,
   "lastExtentSize" : 8192,
   "paddingFactor" : 1,
   "flags" : 1,
   "totalIndexSize" : 16352,
   "indexSizes" : {
    "_id_" : 8176,
    "_ud_1" : 8176
   },
   "ok" : 1
  }
 },
 "ok" : 1
}
>

-------新增shard server 分片

[root@localhost ~]# mkdir /data/shard/s2

[root@localhost ~]# /usr/local/mongo/bin/mongod --shardsvr --port 20002 --dbpath /data/shard/s2 --fork --logpath /data/shard/log/s2.log --directoryperdb
[root@localhost ~]# forked process: 3480
all output going to: /data/shard/log/s2.log

[root@localhost ~]# /usr/local/mongo/bin/mongo --port 40000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:40000/test

mongos> db
test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"localhost:20002"})
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos>

相关内容

热门资讯

徐巧芯剖析赖清德:最怕自己丢掉... 海峡导报综合报道 大罢免投票一周年纪念活动26日举行,台北市长蒋万安出席并怒轰,民进党一年过后仍没有...
中国红十字会总会紧急向广东调拨... 记者从中国红十字会总会了解到,针对台风“红霞”给广东造成的灾情,中国红十字会总会启动四级应急响应,紧...
这些少年,正被“毒蛋”围猎 李梦抽了几口电子烟,吐出了白色的烟雾,有一股浓烈的水果香味。随后,她看着自己握电子烟的手,跟身边人说...
蒋万安号召“倒阁”,卢秀燕认为... 海峡导报综合报道 台北市长蒋万安27日抛出震撼议题,表示多名民代向他提议推动“倒阁”,要求台行政机构...
特朗普将在白宫会见两位重要客人 俄乌战争仍未结束,美伊冲突又有升级风险。在此背景下,美国总统特朗普将在白宫接见乌克兰和以色列领导人。...
新租客打扫卧室柜子上掉落4万元... 近日,莲前西路某小区内,张倩签下一套房子的租赁合同。当天上午,她带着12岁的大女儿周馨怡和11岁的小...
六部门通告:禁止涉军队退役报废... 关于禁止涉军队退役报废装备销售活动的通告近年来,部分经营者在线上线下公开销售涉及我军退役报废装备(指...
九寨沟景区发生泥石流,部分道路... 7月26日,九寨沟风景名胜区管理局发布泥石流灾害通报:2026年7月26日15时50分,九寨沟景区局...
空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...