shared Server 分片

                   这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在 sharding 的各个节点
上,使用者通过简单的配置就可以很方便地构建一个分布式 MongoDB 集群。

MongoDB 的数据分块称为 chunk。每个 chunk 都是 Collection 中一段连续的数据记录,通
常最大尺寸是 200MB,超出则生成新的数据块。

要构建一个 MongoDB Sharding Cluster,需要三种角色:
Shard Server
即存储实际数据的分片,每个 Shard 可以是一个 mongod 实例,也可以是一组 mongod 实例
构成的 Replica Set。为了实现每个 Shard 内部的 auto-failover, MongoDB 官方建议每个 Shard
为一组 Replica Set。

Config Server
为了将一个特定的 collection 存储在多个 shard 中,需要为该 collection 指定一个 shard key,
例如{age: 1} , shard key 可以决定该条记录属于哪个 chunk。 Config Servers 就是用来存储:
所有 shard 节点的配置信息、每个 chunk 的 shard key 范围、 chunk 在各 shard 的分布情况、
该集群中所有 DB 和 collection 的 sharding 配置信息。

Route Process
这是一个前端路由,客户端由此接入,然后询问 Config Servers 需要到哪个 Shard 上查询或
保存记录,再连接相应的 Shard 进行操作,最后将结果返回给客户端。客户端只需要将原本
发给 mongod 的查询或更新请求原封不动地发给 Routing Process,而不必关心所操作的记录
存储在哪个 Shard 上。
下面我们在同一台物理机器上构建一个简单的 Sharding Cluster:
Shard Server 1: 20000
Shard Server 2: 20001
Config Server : 30000
Route Process: 40000



1 创建shared Server

  1. mkdir - p /data/shard/s0 - - 创建数据目录

  2. mkdir - p /data/shard/s1

  3. mkdir - p /data/shard/log - - 创建日志目录

  4. /Apps/mongo/bin/mongod - - shardsvr - - port 20000 - - dbpath /data/shard/s0 - - fork - - logpath

  5. /data/shard/log/s0 . log - - directoryperdb - - 启动 Shard Server 实例 1 ---一个分片

  6. /Apps/mongo/bin/mongod - - shardsvr - - port 20001 - - dbpath /data/shard/s1 - - fork - - logpath

  7. /data/shard/log/s1 . log - - directoryperdb - - 启动 Shard Server 实例 2   --一个分片


2 启动 Config Server

  1. mkdir - p /data/shard/config - - 创建数据目录

  2. /Apps/mongo/bin/mongod - - configsvr - - port 30000 - - dbpath /data/shard/config - - fork - - logpath

  3. /data/shard/log/config . log - - directoryperdb - - 启动 Config Server 实例


3 启动 Route Process

点击( 此处 )折叠或打开

  1. /Apps/mongo/bin/mongos - - port 40000 - - configdb localhost : 30000 - - fork - - logpath

  2. /data/shard/log/route . log - - chunkSize 1 - - 启动 Route Server 实例,和下面一样 最好写i p




4配置 Sharding

  1. 接下来,我们使用 MongoDB Shell 登录到 mongos,添加 Shard 节点

  2. [ root@localhost ~ ] # /Apps/mongo/bin/mongo admin - - port 40000 - - 此操作需要连接 admin 库

  3. MongoDB shell version : 1 . 8 . 1

  4. connecting to : 127 . . . 1 : 40000/admin

  5. > db . runCommand ( { addshard : "localhost:20000" } ) - - 添加 Shard Server,最好写ip

  6. { "shardAdded" : "shard0000" , "ok" : 1 }


  7. > db . runCommand ( { addshard : "localhost:20001" } )

  8. { "shardAdded" : "shard0001" , "ok" : 1 }


  9. > db . runCommand ( { enablesharding : "test" } )

  10. [这里只是标识这个数据库可以启用分片,但实际上新建的文档并没有进行分片]

  11. { "ok" : 1 }


  12. > db . runCommand ( { shardcollection : "test.users" , key : { _id : 1 }} ) - - 设置分片的集合名称,且必

  13. 须指定 Shard Key,系统会自动创建索引

  14. { "collectionsharded" : "test.users" , "ok" : 1 }

  15. 或者命令:

  16. sh.shardCollection("welike_mongo.userRelationMongo",{_id:1})



5验证 Sharding 正常工作

  1. 我们已经对 test . users 表进行了分片的设置,下面我们们插入一些数据看一下结果

  2. > use test

  3. switched to db test

  4. > for ( var i = 1 ; i < = 500000 ; i + + ) db . users . insert ( { age : i , name : "wangwenlong" , addr : "Beijing" ,

  5. country : "China" } )

  6. > db . users . stats ( )

  7. {

  8. "sharded" : true , - - 说明此表已被 shard

  9. "ns" : "test.users" ,

  10. "count" : 500000 ,

  11. "size" : 48000000 ,

  12. "avgObjSize" : 96 ,

  13. "storageSize" : 66655232 ,

  14. "nindexes" : 1 ,

  15. "nchunks" : 43 ,

  16. "shards" : {

  17. "shard0000" : { - - 在此分片实例上约有 24 . 5M 数据

  18. "ns" : "test.users" ,

  19. "count" : 254889 ,

  20. "size" : 24469344 ,

  21. "avgObjSize" : 96 ,

  22. 79 / 91

  23. "storageSize" : 33327616 ,

  24. "numExtents" : 8 ,

  25. "nindexes" : 1 ,

  26. "lastExtentSize" : 12079360 ,

  27. "paddingFactor" : 1 ,

  28. "flags" : 1 ,

  29. "totalIndexSize" : 11468800 ,

  30. "indexSizes" : {

  31. "_id_" : 11468800

  32. } ,

  33. "ok" : 1

  34. } ,

  35. "shard0001" : { - - 在此分片实例上约有 23 . 5M 数据

  36. "ns" : "test.users" ,

  37. "count" : 245111 ,

  38. "size" : 23530656 ,

  39. "avgObjSize" : 96 ,

  40. "storageSize" : 33327616 ,

  41. "numExtents" : 8 ,

  42. "nindexes" : 1 ,

  43. "lastExtentSize" : 12079360 ,

  44. "paddingFactor" : 1 ,

  45. "flags" : 1 ,

  46. "totalIndexSize" : 10649600 ,

  47. "indexSizes" : {

  48. "_id_" : 10649600

  49. } ,

  50. "ok" : 1

  51. }

  52. } ,

  53. "ok" : 1

  54. }

  55. >



7列出所有的 Shard Server

  1. > db . runCommand ( { listshards : 1 } ) - - 列出所有的 Shard Server

  2. {

  3. "shards" : [

  4. {

  5. "_id" : "shard0000" ,

  6. "host" : "localhost:20000"

  7. } ,

  8. {

  9. "_id" : "shard0001" ,

  10. "host" : "localhost:20001"

  11. }

  12. ] ,

  13. "ok" : 1

  14. }




8 查看 Sharding 信息

  1. > printShardingStatus ( )  等价  sh.status()      - - 查看 所有表的Sharding 信息

  2. - - - Sharding Status - - -

  3. sharding version : { "_id" : 1 , "version" : 3 }

  4. shards :

  5. { "_id" : "shard0000" , "host" : "localhost:20000" }

  6. { "_id" : "shard0001" , "host" : "localhost:20001" }

  7. databases :

  8. { "_id" : "admin" , "partitioned" : false , "primary" : "config" }

  9. { "_id" : "test" , "partitioned" : true , "primary" : "shard0000" }

  10. test . users chunks :

  11. shard0000 1     ----test.users在shard0000 上有一个chunks块

  12. { "_id" : { $minKey : 1 } } - - > > { "_id" : { $maxKey : 1 } } on :

  13. shard0000 { "t" : 1000 , "i" : 0 }

  14. >


判断是否是 Sharding

点击( 此处 )折叠或打开

  1. > db . runCommand ( { isdbgrid : 1 } )

  2. { "isdbgrid" : 1 , "hostname" : "localhost" , "ok" : 1 }





 对现有的表进行 Sharding
刚才我们是对表 test.users 进行分片了,下面我们将对库中现有的未分片的表 test.users_2 进
行分片处理
表最初状态如下,可以看出他没有被分片过:
> db.users_2.stats()  或者 db.hotFeedMongo.stats().sharded
{
"ns" : "test.users_2",
"sharded" : false,
"primary" : "shard0000",
"ns" : "test.users_2",
"count" : 500000,
"size" : 48000016,
"avgObjSize" : 96.000032,
"storageSize" : 61875968,
"numExtents" : 11,
"nindexes" : 1,
"lastExtentSize" : 15001856,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 20807680,
"indexSizes" : {
"_id_" : 20807680
},
"ok" : 1
}
对其进行分片处理:
> use admin
switched to db admin
> db.runCommand({ shardcollection: "test.users_2", key: { _id:1 }})
{ "collectionsharded" : "test.users_2", "ok" : 1 }
再次查看分片后的表的状态,可以看到它已经被我们分片了
> use test
switched to db test
> db.users_2.stats()
{
"sharded" : true,
"ns" : "test.users_2",
"count" : 505462,
……
"shards" : {
"shard0000" : {
"ns" : "test.users_2",
……
"ok" : 1
},
"shard0001" : {
"ns" : "test.users_2",
……
"ok" : 1
}
},
"ok" : 1
}
>




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-1800199/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-1800199/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值