MongoDB分片部署(windows)

目录

os (电脑Windows版本):10

MongoDB:4.4.24

硬件要求:至少三台服务器(用于分片、配置服务器和路由器)

分片架构​编辑

安装MongoDB

 分片集群组件

 部署分片集群

 部署shard

 步骤一:环境准备

步骤二 : 启动分片服务(实例)

启动分片集群1(shard11和shard12)

步骤三:配置分片(shard)集群

部署config server

步骤一:环境准备

步骤二:启动config server

启动config1

启动config2

!!!注意 请不要关闭任何cmd窗口 否则需要重新启动 最小化即可

步骤三:配置config server集群

部署路由服务器 Route Process

配置分片信息

测试分片

查看分片状态


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

  1. 下载MongoDB社区版安装包。
  2. 行运安装程序,按照提示完成安装。
  3. 配置环境变量,添加MongoDB的bin目录到PATH中。

 分片集群组件

  • 分片服务器(Shard):存储数据的实际节点。
  • 配置服务器(Config Server):存储集群元数据。
  • 路由器(mongos):客户端与集群之间的接口。

 部署分片集群

 部署shard
 步骤一:环境准备
分片节点(实例)端口路径
1shard11(主)4006

dbpath:D:\shard1\shard11\data

logpath:D:\shard1\shard11\log

1shard12(从)4007

dbpath:D:\shard1\shard12\data

logpath:D:\shard1\shard12\log

2shard21(主)4008

dbpath:D:\shard2\shard21\data

logpath:D:\shard2\shard21\log

2shard22(从)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

小提示

  1. 某些电脑版本比较高,所以的 cmd 需要使用管理员身份进行运行
  2. 启动服务均为一次性服务,关闭 cmd 即为关闭服务,所以在未完成前,请勿关闭
  3. 实例均未添加至系统环境变量,请在 bin 目录下启动
  4. 虽然窗口很多,但是操作不太友好,所以在 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(主)4002D:\config\config1\dataD:\config\config1\log
config2(从)4003D:\config\config2\dataD:\config\config2\log

 照葫芦画瓢,在每个文件夹添加datalong

步骤二:启动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 端口查看分片情况,若两个片段不同,则说明分片成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值