本篇文章为大家展示了MongoDB中怎么搭建分片集群,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

MongoDB分片集群基本组件介绍

mongos:数据库集群请求的入口,所有请求需要经过mongos进行协调,无需在应用层面利用程序来进行路由选择,mongos其自身是一个请求分发中心,负责将外部的请求分发到对应的shard服务器上,mongos作为统一的请求入口,为防止mongos单节点故障,一般需要对其做HA。

config server:配置服务器,存储所有数据库元数据(分片,路由)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是存缓存在内存中来读取数据,mongos在第一次启动或后期重启时候,就会从config server中加载配置信息,如果配置服务器信息发生更新会通知所有的mongos来更新自己的状态,从而保证准确的请求路由,生产环境中通常也需要多个config server,防止配置文件存在单节点丢失问题。

shard:在传统意义上来讲,如果存在海量数据,单台服务器存储1T压力非常大,无论考虑数据库的硬盘,网络IO,又有CPU,内存的瓶颈,如果多台进行分摊1T的数据,到每台上就是可估量的较小数据,在mongodb集群只要设置好分片规则,通过mongos操作数据库,就可以自动把对应的操作请求转发到对应的后端分片服务器上。

replica set:在总体mongodb集群架构中,对应的分片节点,如果单台机器下线,对应整个集群的数据就会出现部分缺失,这是不能发生的,因此对于shard节点需要replica set来保证数据的可靠性,生产环境通常为2个副本+1个仲裁。

MongoDB分片集群搭建架构

架构:我们这次搭建的是三分片,每个分片三个副本集的分片集群

端口分布:mongos:20000 config:21000 shard1:22001 shard2:22002 shard3:22003

MongoDB分片集群搭建集群步骤

1. 各个机器上分别安装MongoDb

安装过程省略。只需要将安装包COPY到各个服务器解压即可

2. 分别在每台机器建立conf、mongos、config、shard1、shard2、shard3六个目录,因为mongos不存储数据,只需要建立日志文件目录即可

mkdir-p/data/mongodb/mongos/{log,conf}mkdir-p/data/mongodb/mongoconf/{data,log,conf}mkdir-p/data/mongodb/shard1/{data,log,conf}mkdir-p/data/mongodb/shard2/{data,log,conf}mkdir-p/data/mongodb/shard3/{data,log,conf}touch/data/mongodb/mongos/log/mongos.logtouch/data/mongodb/mongoconf/log/mongoconf.logtouch/data/mongodb/mongoconf/conf/mongoconf.conftouch/data/mongodb/shard1/log/shard1.logtouch/data/mongodb/shard2/log/shard2.logtouch/data/mongodb/shard3/log/shard3.log

3.关闭防火墙

systemctlstopfirewalld.service

4. Config server搭建副本集,添加如下内容

cat>/data/mongodb/mongoconf/conf/mongoconf.conf<<EOFdbpath=/data/mongodb/mongoconf/datalogpath=/data/mongodb/mongoconf/log/mongoconf.loglogappend=truebind_ip=0.0.0.0port=21000maxConns=1000#链接数journal=true#日志开启journalCommitInterval=200fork=true#后台执行syncdelay=60oplogSize=1000configsvr=true#配置服务器replSet=replconf#configserver配置集replconfEOF

5. 分别启动三台服务器的config server

mongod-f/data/mongodb/mongoconf/conf/mongoconf.conf

6. 登录任意一台配置服务器,初始化配置副本集

mongo--port21000useadminconfig={_id:"replconf",members:[{_id:0,host:"111.111.111.93:21000"},{_id:1,host:"111.111.111.54:21000"},{_id:2,host:"111.111.111.252:21000"}]}

7. 初始化config服务器的副本集

rs.initiate(config)

这一步非常重要,必须初始化成功。不成功的话,路由服务器与配置服务器连接不上。

其中,”_id” : “configs”应与配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 为三个节点的 ip 和 port。

8. 三台分片服务器搭建副本集,配置分片副本集(三台机器)。

cat>/data/mongodb/shard1/conf/shard.conf<<EOFdbpath=/data/mongodb/shard1/datalogpath=/data/mongodb/shard1/log/shard1.logport=22001bind_ip=0.0.0.0logappend=truenohttpinterface=truefork=trueoplogSize=51200journal=truesmallfiles=trueshardsvr=true#shard服务器replSet=shard1#副本集名称shard1EOF

9. 启动三台服务器的shard1 server

mongod-f/data/mongodb/shard1/conf/shard.conf

登陆任意一台服务器,初始化副本集

mongo--port22001useadmin

10. 定义副本集配置,第三个节点的 “arbiterOnly”:true 代表其为仲裁节点。

config={_id:"shard1",members:[{_id:0,host:"111.111.111.93:22001"},{_id:1,host:"111.111.111.54:22001",arbiterOnly:true},{_id:2,host:"111.111.111.252:22001"},]}

初始化副本集配置

rs.initiate(config);

此时shard1 副本集已经配置完成,mongodb-1为primary,mongodb-2为arbiter,mongodb-3为secondary。

同样的操作进行shard2配置和shard3配置

注意:进行shard2的副本集初始化,在mongodb-2,初始化shard3副本集在mongodb-3上进行操作。

同理,我们设置第二个分片

cat>/data/mongodb/shard2/conf/shard.conf<<EOFdbpath=/data/mongodb/shard2/datalogpath=/data/mongodb/shard2/log/shard2.logport=22002bind_ip=0.0.0.0logappend=truenohttpinterface=truefork=trueoplogSize=51200journal=truesmallfiles=trueshardsvr=truereplSet=shard2EOF

并依次启动三台服务器的shard2 server

##启动shard2副本集mongod-f/data/mongodb/shard2/conf/shard.conf

初始化副本集配置

##进入到shard2副本集mongo--port22002useadminconfig={_id:"shard2",members:[{_id:0,host:"111.111.111.93:22002"},{_id:1,host:"111.111.111.54:22002"},{_id:2,host:"111.111.111.252:22002",arbiterOnly:true},]}rs.initiate(config);

最后设置第三个分片副本集 ,配置文件配置项

cat>/data/mongodb/shard3/conf/shard.conf<<EOFdbpath=/data/mongodb/shard3/datalogpath=/data/mongodb/shard3/log/shard3.logport=22003logappend=truenohttpinterface=truefork=trueoplogSize=51200journal=truesmallfiles=trueshardsvr=truereplSet=shard3EOF

启动三台服务器的shard3 server

mongod-f/data/mongodb/shard3/conf/shard.conf

##进入到shard3副本集useadminconfig={_id:"shard3",members:[{_id:0,host:"111.111.111.93:22003",arbiterOnly:true},{_id:1,host:"111.111.111.54:22003"},{_id:2,host:"111.111.111.252:22003"},]}rs.initiate(config);

11.配置路由服务器mongos

目前三台服务器的config服务器和shard服务器均已启动,现在开始配置mongos服务的三副本集

由于mongos服务器的配置是从内存中加载,所以自己没有存在数据目录configdb连接为config服务器集群

先启动config服务器和shard服务器,然后启动路由实例:(三台机器)

mongs服务器配置文件

cat>/data/mongodb/mongos/conf/mongos.conf<<EOFlogpath=/data/mongodb/mongos/log/mongos.loglogappend=trueport=20000maxConns=1000configdb=replconf/111.111.111.93:21000,111.111.111.54:21000,111.111.111.252:21000#指定configserver集群fork=trueEOF

启动三台服务器的mongos server

mongos-f/data/mongodb/mongos/conf/mongos.conf

目前搭建了mongodb config服务器、mongos服务器,各个shard服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效,意即将mongos,shard,config三类服务器串起来即为分片服务器了

登陆任意一台mongos

mongo--port20000

使用admin数据库

useadmin

串联路由服务器与分配副本集

useadmindb.runCommand({addshard:"shard1/111.111.111.93:22001,111.111.111.54:22001,111.111.111.252:22001"})db.runCommand({addshard:"shard2/111.111.111.93:22002,111.111.111.54:22002,111.111.111.252:22002"})db.runCommand({addshard:"shard3/111.111.111.93:22003,111.111.111.54:22003,111.111.111.252:22003"})

查看集群状态

sh.status()

12. 配置分片

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。

指定prod_sharding分片生效

db.runCommand({enablesharding:"prod_sharding"});

指定数据库里需要分片的集合和片键

db.runCommand({shardcollection:"prod_sharding.sharding_tbl",key:{"albumId":1,"itemId":1}})db.runCommand({shardcollection:"prod_sharding.sharding_tbl_2",key:{"albumId":"hashed"}})db.runCommand({shardcollection:"prod_sharding.sharding_tbl_3",key:{"albumId":1,"itemId":1}})db.runCommand({shardcollection:"prod_sharding.sharding_tbl_5",key:{"albumId":"hashed"}})

查看分配状态

db.sharding_tbl.stats();

创建用户

useprod_shardingdb.createUser({user:"prod_sharding",pwd:"prod_sharding_123",roles:["dbAdmin","dbOwner"]})

连接DB

mongo111.111.111.93:20000/prod_test-uprod_test-pprod_test123

上述内容就是MongoDB中怎么搭建分片集群,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。