目录
分片架构
从图中可以看出,分片集群中主要由三个部分组成,即分片服务器(Shard)、路由服务器(Mongos) 以及配置服务器(Config Server)组成。其中,分片服务器有三个,即主、副、副。
Shard: 用于存储实际的数据块,实际生产环境中一个shard server 角色可由几台机器组个一个 replica set(副本集群) 承担,防止主机单点故障【3.6版本后必须配置成副本集群】
Config Server: mongod实例,存储了整个 ClusterMetadata ,其中包括 chunk 信息【3.4版本后必须部署成副本集群】。
Query Routers: 前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
部署分片集群
步骤一:环境准备
分 片 | 节点 | ip:端口 | 数据库路径 | 日志路径 |
1 | shard1(主) | localhost: 4006 | D:\mongodb\shard1\db | D:\mongodb\shard1\log |
1 | shard2(副) | localhost: 4007 | D:\mongodb\shard1\shard2\db | D:\mongodb\shard1\shard2\log |
2 | shard3(主) | localhost: 4008 | D:\mongodb\shard2\shard3\db | D:\mongodb\shard2\shard3\log |
2 | shard4(副) | localhost: 4009 | D:\mongodb\shard2\shard4\db | D:\mongodb\shard2\shard4\log |
每一个分片都应该安装MongoDB实例,和前面的主从复制类似,也需要将bin文件复制到每个分片中,并且创建db文件以及log文件存放数据库数据和日志数据
每个shard包含两个实例,一主一从。
启动分片服务
首先关闭之前打开的数据库服务
步骤二 启动分片服务(实例)
启动分片集群shard1(shard11和shard12)
shard11
然后进入数据库bin目录中,启动cmd
momgod --shardsvr --replSet shard1 --dbpath "D:\mongodb\shard1\db" --port 4006 --logpath "D:\mongodb\shard1\log\shard1.log"
--shardsvr 为分片声明
当命令一直保持运行状态则说明服务运行成功,此服务为一次性服务,不要关闭此窗口,最小化即可。
shard12:
再次进入数据库bin目录中,启动cmd
momgod --shardsvr --replSet shard1 --dbpath "D:\mongodb\shard1\shard2\db" --port 4007 --logpath "D:\mongodb\shard1\shard2\log\shard1.log"
启动分片集群2(shard21和shard22)
shard21
momgod --shardsvr --replSet shard2 --dbpath "D:\mongodb\shard2\shard3\db" --port 4008 --logpath "D:\mongodb\shard2\shard3\log\shard2.log"
shard22
momgod --shardsvr --replSet shard2 --dbpath "D:\mongodb\shard2\shard4\db" --port 4009 --logpath "D:\mongodb\shard2\shard4\log\shard2.log"
步骤三:配置分片(shard1和shard2)集群
进入到shard1集群任何一个节点中
mongo 127.0.0.1:4006
初始化 rs.initiate(config1)
config1={_id:"shard1",members:[
{id:0,host:"localhost:4006",priority:1},
{id:1,host:"localhost:4007",priority:2}
]}
rs.intiate(config1)
进入到shard2集群任何一个节点中
mongo 127.0.0.1:4009
config2={_id:"shard2",members:[
{id:0,host:"localhost:4008",priority:2},
{id:1,host:"localhost:4009",priority:1}
]}
rs.intiate(config2)
至此,shard的两个集群配置好了。
部署config server
步骤一:环境准备
config实例 | 端口 | 数据路径 | 日志路径 |
---|---|---|---|
config1(主) | 4002 | D:\config\config1\data | D:\config\config1\log |
config2(从 | 4003 | D:\config\config2\data | D:\config\config2\log |
值得注意的是:在 MongoDB 3。4 版本后 config 服务必须配置为 副本集,这里设置为一主一从。
老样子,每个文件夹添加data和log
步骤二:启动config server
启动config1
进入到bin目录中,启动cmd
mongod --configsvr --replSet confset -port 4002 -dbpath D:\config\config1\data -logpath D:\config\config1\log\conf1.log
--configsvr 这里我们完全可以像启动普通 mongodb 服务一样启动,不需要添加 —shardsvr 和 configsvr 参数。因为这两个参数的作用就是改变启动端口的,所以我们自行指定了端口就可以。
启动config2
mongod --configsvr --replSet confset -port 4003 -dbpath D:\config\config2\data -logpath D:\config\config2\log\conf2.log
不要关闭 cmd 窗口,最小化即可 ]
步骤三:配置config server集群
进入任何一个配置服务器的节点初始化配置服务器的群集
mongo 127.0.0.1:4002
use admin
config={_id:"confset",configsvr:true,members:[
... {_id:0,host:"localhost:4002"},
... {_id:1,host:"localhost:4003"}
... ]}
rs.initiate(config3)
配置路由器服务器 Route Process
可以创建专门的文件夹存日志
在进入要分片的数据库bin目录中,启动cmd
mongos : mongos 就是一个路由服务器,它会根据管理员设置的 “ 片键 ” 将数据分摊到自己管理的mongod 集群,数据和片的对应关系以及相应的配置信息保存在 "config 服务器 " 上。
配置分片信息
bin 目录下使用 MongoDB Shell 登录到 mongos ,添加 Shard 节点
mongo 127.0.0.1:4000
sh.addShard("shard1/loclhost:4006,localhost:4007")
sh.addShard("shard1/loclhost:4008,localhost:4009")
查看分片集
db.getSiblingDB("config").shards.find()
mongodb分片测试
登入路由(4000) 端口
指定要分片的数据库,以下指令指定分片数据库为test
mongo 127.0.0.1:4000
sh. enableSharding("test")
上述指令指定分片集合为c2,分片字段为“id”,分片形式是哈希分片,若改成“1”则为范围分片
如果集合已经包含数据,则必须在分片集合之前创建一个支持分片键的索引,如果集合为空,则
mongodb 将创建索引。
指定数据库里需要分片的集合和片键,片键根据实际情况选择
sh.shardCollection("test.c2",{"id":1})
插入10000条数据验证,数据必须包含分片键:id
robo 3T 查看分片集
robo 3T链接4000端口进行查看
也可以链接两个shard端口查看分片情况
最后
重新打开mongodb服务,即可像原来一样使用,但是数据存储方式和原来已经不一样了,变成了分布式的分片存储。