目录
!!!注意 请不要关闭任何cmd窗口 否则需要重新启动 最小化即可
os (电脑Windows版本):10
MongoDB:4.4.24
硬件要求:至少三台服务器(用于分片、配置服务器和路由器)
分片架构
从图中可以看出,分片集群中主要由三个部分组成,即分片服务器( Shard )、路由服务器
( Mongos )以及配置服务器( Config Server )组成。其中,分片服务器有三个,即 Shard1 、
Shard2 、 Shard3 ;路由服务器有两个,即 Mongos1 和 Mongos2 ;配置服务器有三个,即主、副、副。
主要有如下所述三个主要组件:
Shard: 用于存储实际的数据块,实际生产环境中一个shard server 角色可由几台机器组个一个 replica set 承担,防止主机单点故障
Config Server: mongod实例,存储了整个 ClusterMetadata ,其中包括 chunk 信息。
Query Routers: 前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
安装MongoDB
- 下载MongoDB社区版安装包。
- 行运安装程序,按照提示完成安装。
- 配置环境变量,添加MongoDB的bin目录到PATH中。
分片集群组件
- 分片服务器(Shard):存储数据的实际节点。
- 配置服务器(Config Server):存储集群元数据。
- 路由器(mongos):客户端与集群之间的接口。
部署分片集群
部署shard
步骤一:环境准备
分片 | 节点(实例) | 端口 | 路径 |
1 | shard11(主) | 4006 | dbpath:D:\shard1\shard11\data logpath:D:\shard1\shard11\log |
1 | 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 |
2 | shard22(从) | 4009 | dbpath:D:\shard2\shard21\data logpath:D:\shard2\shard21\log |
每一个分片都应该安装 MongoDB 实例,需要将 bin 文件复制到每个分片中, 并且创建data 文件以及 log 文件存放数据库数据和日志数据
步骤二 : 启动分片服务(实例)
启动分片集群1(shard11和shard12)
shard11
然后进入数据库bin目录中,启动cmd
\bin>mongod --shardsvr --replSet shard1 -port 4006 -dbpath D:\shard1\shard11\data -logpath D:\shard1\shard11\log\shard11.log
--shardsvr 为分片声明
当命令一直保持运行状态则说明服务运行成功,此服务为一次性服务,不要关闭此窗口,最小化即可。
shard12:
再次进入数据库bin目录中,启动cmd
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
小提示
- 某些电脑版本比较高,所以的 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
步骤一:环境准备
值得注意的是:在 MongoDB 3 版本后 config 服务必须配置为从 副本集 ,所以直接用前面设置好了的副 本启动即可
config实例 | 端口 | config2(从) | 日志路径 |
config1(主) | 4002 | D:\config\config1\data | D:\config\config1\log |
config2(从) | 4003 | D:\config\config2\data | D:\config\config2\log |
照葫芦画瓢,在每个文件夹添加data和long
步骤二:启动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
配置分片信息
bin 目录下使用 MongoDB Shell 登录到 mongos ,添加 Shard 节点
mongos> sh.addShard("shard1/localhost:4006,localhost:4007")
mongos> sh.addShard("shard2/localhost:4008,localhost:4009")
测试分片
登入路由(4000) 端口
指定要分片的数据库
mongos> sh.enableSharding("test")
指定数据库里需要分片的集合和片键,片键根据实际情况选择
mongos> sh.shardCollection("test.c2",{"id":"hashed"})
上述指令指定分片集合为c2,分片字段为“id”,分片形式是哈希分片,若改成“1”则为范围分片
如果集合已经包含数据,则必须在分片集合之前创建一个支持分片键的索引,如果集合为空,则
mongodb 将创建索引。
插入10000条数据验证,数据必须包含分片键:id
mongos> for(var i=1;i<=10000;i++){
... db.c2.save({id:i,name:"a"+i});}
查看分片状态
输入sh.status() 查看分片集群状态
两个 shard都有数据分布,说明分片成功!
连接两个 shard 端口查看分片情况,若两个片段不同,则说明分片成功。