MongoDB分片部署(windows)

发布于:2024-04-20 ⋅ 阅读:(22) ⋅ 点赞:(0)

OS:win10

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: 前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。

部署分片集群

部署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

然后进入数据库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

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

步骤一:环境准备

值得注意的是:在 MongoDB 3 版本后 config 服务必须配置为从 副本集 ,所以直接用前面设置好了的副 本启动即可
config实例 端口 数据路径 日志路径
config1(主) 4002 D:\config\config1\data D:\config\config1\log
config2(从) 4003 D:\config\config2\data D:\config\config2\log

老样子,每个文件夹添加datalog

步骤二:启动config server

启动config1

进入到bin目录中,启动cmd

\bin>mongod --configsvr --replSet confset -port 4002 -dbpath D:\config\config1\data -logpath D:\config\config1\log\conf1.log
--configsvr 这里我们完全可以像启动普通 mongodb 服务一样启动,不需要添加 —shardsvr configsvr 参数。因为这两个参数的作用就是改变启动端口的,所以我们自行指定了端口就可以。

启动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) 端口
指定要分片的数据库
mongos> sh.enableSharding("test")
指定数据库里需要分片的集合和片键,片键根据实际情况选择
mongos> sh.shardCollection("test.c2",{"id":"hashed"})

上述指令指定分片集合为c2,分片字段为“id”,分片形式是哈希分片,若改成“1”则为范围分片

如果集合已经包含数据,则必须在分片集合之前创建一个支持分片键的索引,如果集合为空,则
mongodb 将创建索引。

插入数据验证

mongos> for(var i=1;i<=10000;i++){
... db.c2.save({id:i,name:"a"+i});}

robo 3T查看分片集

连接两个 shard 端口查看分片情况,若两个片段不同,则说明分片成功!!