分片架构
分片集群中主要由三个部分组成,即分片服务器(Shard)、路由服务器(Mongos)以及配置服务器(Config Server)组成。其中分片服务器有三个:Shard1、Shard2、Shard3;路由服务器有两个:Mongos1和Mongos2;配置服务器有三个:主、副、副。
主要有三个主要组件:
Shard:用于存储实际数据块,实际生产环境中一个shard server角色可由几台机器组一个replica set承担,防止主机单点故障。
Config Server:mongod实例,存储整个ClusterMetadata,其中包括chunk信息。
Query Routers:前端路由,客户端由此接入,让整个集群看上去像单一数据库,前端应用可以透明使用。
部署分片集群
部署shard
步骤一:环境准备
分片 | 节点 | 端口 | 路径 |
1 | shard11(主) | 4006 | dbpath:D:\shard1\shard11\data logpath:D:\shard1\shard11\log |
shard12(从) | 4007 | dbpath:D:\shard1\shard12\data logpath:D:\shard1\shard12\log | |
2 | shard21(主) | 4008 | dbpath:D:\shard2\shard21\data logpath:D:\shard2\shard21\log |
shard22(从) | 4009 | dbpath:D:\shard2\shard22\data logpath:D:\shard2\shard22\log |
每个分片都应该安装MongoDB实例,需要将bin文件复制每个分片中,并创建data文件以及log文件存放数据库数据和日志数据
步骤二:启动分片服务
启动分片集群1(shard11和shard12)
shard11进入数据库斌目录中,启动cmd
\bin>mongod --shardsvr --replSet shard1 -port 4006 -dbpath D:\shard1\shard11\data -logpath D:\shard1\shard11\log\shard11.log
shardsvr为分片声明
当命令一直保持运行状态则说明服务运行成功,此服务为一次性服务,不能关闭窗口,最小化即可。
再次进入数据库bin目录中启动cmd
shard12:
bin>mongod --shardsvr --replSet shard1 -port 4007 -dbpath D:\shard1\shard12\data -logpath D:\shard1\shard12\log\shard12.log
启动分片集群2(shard21和shard22)
shard21
\bin>mongod --shardsvr --replSet shard2 -port 4008 -dbpath D:\shard2\shard21\data -logpath D:\shard2\shard21\log\shard21.log
shard22
\bin>mongod --shardsvr --replSet shard2 -port 4009 -dbpath D:\shard2\shard22\data -logpath D:\shard2\shard22\log\shard22.log
tips:
电脑版本比较高的, cmd 需要使用管理员身份运行
启动服务均为一次性服务,关闭 cmd 即为关闭服务,所以在未完成前,请勿关闭
实例均未添加至系统环境变量,请在 bin 目录下启动
虽然窗口很多,操作不太友好,但是在 win 系统下,还是多有耐心一点, linux会简单一些
步骤三:配置分片(shard)集群
进入shard1集群任意一个节点中:
use admin
config={_id:"shard1",members:[
... {_id:0,host:"localhost:4006",priority:2},
... {_id:1,host:"localhost:4007",priority:1}
... ]}
rs.initate(config)
进入shard2集群任意一个节点中:
use admin
config={_id:"shard2",members:[
... {_id:0,host:"localhost:4008",priority:2},
... {_id:1,host:"localhost:4009",priority:1}
... ]}
rs.initate(config)
到此shard集群配置好了。
部署config server
步骤一:环境准备
config实例 | 端口 | 数据路径 | 日志路径 |
config1(主) | 4002 | D:\config\config1\data | D:\config\config1\log |
config2(从) | 4003 | D:\config\config2\data | D:\config\config2\log |
每个文件夹添加data和log
步骤二:启动config server
启动config1
进入bin目录中,启动cmd
\bin>mongod --configsvr --replSet confset -port 4002 -dbpath D:\config\config1\data -logpath D:\config\config1\log\conf1.log
启动config2
\bin>mongod --configsvr --replSet confset -port 4003 -dbpath D:\config\config2\data -logpath D:\config\config2\log\conf2.log
不要关闭cmd窗口,最小化即可。
步骤三:配置config server集群
进入任意一个配置服务器的节点初始化配置服务器的集群
use admin
config={_id:"confset",configsvr:true,members:[
... {_id:0,host:"localhost:4002"},
... {_id:1,host:"localhost:4003"}
... ]}
rs.initiate(config)
部署路由服务器 Route Process
可以创建专门的文件夹存放日志
进入数据库bin目录中启动cmd
D:\MongoDB\bin>mongos --configdb confset/localhost:4002,localhost:4003 -logpath D:\mongos\log\mongos.log -port 4000
mongos : mongos 就是一个路由服务器,它会根据管理员设置的 “ 片键 ” 将数据分摊到自己管理的mongod 集群,数据和片的对应关系以及相应的配置信息保存在 "config 服务器 " 上。
配置分片信息
bin目录下使用MongoDB Shell 登录到 mongos,添加Shard节点
mongos> sh.addShard("shard1/localhost:4006,localhost:4007")
mongos> sh.addShard("shard2/localhost:4008,localhost:4009")
测试分片
登入路由(4000) 端口
指定要分片的数据库,以下指令指定分片数据库为test
mongos> sh.enableSharding("test")
指定数据库里需要分片的集合和片键,片键根据实际情况选择
mongos> sh.shardCollection("test.c1",{"id":"hashed"})
上述指令指定分片集合为c1,分片字段为“id”,分片形式是哈希分片,若改成“1”则为范围分片
如果集合已经包含数据,则必须在分片集合之前创建一个支持分片键的索引,如果集合为空,则
mongodb 将创建索引。
插入10000条数据验证,数据必须包含分片键:id
mongos> for(var i=1;i<=10000;i++){
... db.c1.save({id:i,name:"a"+i,age:i});}
查看分片状态
输入sh.status() 查看分片集群状态
两个shard都有数据分布,说明分片成功!
robo 3T查看分片集
连接两个 shard 端口查看分片情况,若两个shard中的数据不同,则说明分片成功!!
shard1--4006端口中的数据存储,查看c1集合中的数据,可见分片键id的分布情况。
shard1--4007端口中的数据存储,查看c1集合中的数据,可见分片键id的分布情况和shard1--4006端口完全一样,说明shard1主从复制成功【副本集群部署成功】。
shard2--4008端口中的数据存储,查看c1集合中的数据,可见分片键id的分布情况和shard1完全不同。说明分片成功!