跳到主要内容

二十七、MongoDB 分片集群技术

海量的数据使得单台 MongoDB 存储显得吃力且难以满足提供可接受的读写吞吐量

通过将数据分割到多台服务器就变得越来越重要,使得 MongoDB 数据库系统能存储和处理更多的数据

为什么使用分片

1、 复制所有的写入操作到主节点;
2、 延迟的敏感数据会在主节点查询;
3、 单个副本集限制在12个节点;
4、 当请求量巨大时会出现内存不足;

5、 本地磁盘不足;
6、 垂直扩展价格昂贵;

MongoDB分片

下图展示了 MongoDB 分片集群结构分布

 

MongoDB 分片集群技术主要有三个组件

1、 ShardServer

用于存储实际的数据块  
一个 shard server 角色可由几台机器组一个负载均衡承担,防止主机单点故障

2、 ConfigServer

mongod 实例,存储了整个 Cluster Metadata,包括 chunk 信息

3、 QueryRouters

前端路由  
客户端由此接入,且让整个集群看上去像单一数据库

MongoDB 分片技术搭建范例

假设 MongoDB 分片集群结构组件如下所示

Shard Server

Shard Server 1:localhost:27020
Shard Server 2:localhost:27021
Shard Server 3:localhost:27022
Shard Server 4:localhost:27023

Config Server

Config Server :localhost:27100

Query Routers Server

Route Process:localhost:40000

MongoDB 分片集群搭建步骤