阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MongoDB 搭建分片集群

141次阅读
没有评论

共计 4676 个字符,预计需要花费 12 分钟才能阅读完成。

在 MongoDB(版本 3.2.9)中,分片是指将 collection 分散存储到不同的 Server 中,每个 Server 只存储 collection 的一部分,服务分片的所有服务器组成分片集群。分片集群(Sharded Clustered)的服务器分为三中类型:Router(mongos),Config Server 和 Shard(Replica Set 或 Standalone mongod)。使用分片集群,不需要使用强大的计算机,就能存储更多的数据,处理更大的负载。分布式数据库系统的设计目的是:水平分片,将负载分配到多台 Server,减少单机查询的负载。

MongoDB 搭建分片集群

一,配置服务器

config server 存储分片的元数据,元数据包括每个分片的块(chunk)列表和每个 chunk 包含数据的范围。路由服务区(Router)从 config server 上获取分片的元数据,使用元数据将读写操作路由到正确的分片上。

The metadata includes the list of chunks on every shard and the ranges that define the chunks. The mongos instances cache this data and use it to route read and write operations to the correct shards.

config server 的读写操作是非常少的,config server 将分片的元数据存储在 config 数据库中,只有当分片的元数据变化时,比如 chunk migration,chunk split,才会修改 config server 中的数据。只有在 mongos 第一次启动或重启时,或者分片的元数据变化时,mongos 才会读取 config server 中的数据。mongos 在读取分片的元数据之后,会缓存在本地。

Config servers store the cluster’s metadata in the config database. The mongos instances cache this data and use it to route reads and writes to shards. MongoDB only writes data to the config servers when the metadata changes, such as

  • after a chunk migration, or
  • after a chunk split.

MongoDB reads data from the config server in the following cases:

  • A new mongos starts for the first time, or an existing mongos restarts.
  • After change in the cluster metadata, such as after a chunk migration.

实际上,config server 是 mongod,只不过设置 –configsvr 选项。

–configsvr 指定 mongod 作为一个 config server

二,mongos 路由服务器

mongos 为 MongoDB 提供路由服务,处理从 application layer 发送的查询请求,定位数据所在的分片,对分片上的查询结果进行 combine,以完成分布式数据查询。从 Application 来看,mongos 担当的角色是一个 MongoDB Instance,隐藏了从分片上 query 和 combine 数据的复杂过程。

mongos 的重要参数

–config <filename>, -f <filename> 指定 mongos 运行的参数

–configdb 指定 config server 列表,格式是:config-svr:port,config-svr:port

–chunkSize 指定 data block 的大小,单位是 MB,默认值是 64

–port 指定 mongos 监听的 TCP 的端口号,默认值是 27017

–logpath 指定 mongos 记录日志的路径,默认情况下,MongoDB 将现存的日志文件重命名,而不是重写。By default, MongoDB will move any existing log file rather than overwrite it. To instead append to the log file, set the –logappend option.

三,搭建分片集群

1,Shard

分片(Shard)用于存储数据,可以是 Replica Set,也可以是 Standalone,由于每个 Shard 都保存 collection 的一部分数据,如果 shard 出现故障,那么 collection 就会变得不完整。在产品环境中,每一个 shard 都是一个 replica set。

2,config server

config server 保存着每个分片和数据之间的映射,即数据存储在哪个分片上,或者说,每个分片上存储哪些数据,一个 doc 只能存储在一个分片上。分片的元数据极端重要,必须为 config server 启用日志功能,确保元数据保存到 disk 中。最好使用 3 台 config server,每台 config server 都应该位于单独的物理机上,最好是分布在不同地理位置的机器。

创建三台 config server:cfg-srv1,cfg-svr2,cfg-svr3,其配置文件分别位于:

  • cfg-svr1,C:\data\config\cfgsvr_1.conf
  • cfg-svr2,C:\data\config\cfgsvr_2.conf
  • cfg-svr3,C:\data\config\cfgsvr_3.conf
 
--config server 1
dbpath=C:\data\config\
logpath=C:\data\config\cfgsvr_1.log
journal=true
port=50001
configsvr=true

--config server 2
dbpath=C:\data\config\
logpath=C:\data\config\cfgsvr_2.log
journal=true
port=50002
configsvr=true

--config server 3
dbpath=C:\data\config\
logpath=C:\data\config\cfgsvr_3.log
journal=true
port=50003
configsvr=true

启动 config server,启动配置服务器时,不要使用 –replset 参数,config server 不是 replica set;–configsvr 参数指定 mongod 为 config server。

--config server 1
mongod -f C:\data\config\cfgsvr_1.conf
--config server 2
mongod -f C:\data\config\cfgsvr_2.conf
--config server 3
mongod -f C:\data\config\cfgsvr_3.conf

3,Router
mongos 是路由服务器(Router),mongos 需要 config server 的地址列表,通过 –configdb 指定 router 能够访问的 config server 列表。mongos 不保存数据,不需要指定 dbpath 参数,mongos 在启动时从 config server 加载集群数据,可以启动任意数量的 mongos,每个 mongos 使用相同的 config server 列表。

在 router-svr1 上创建 mongos,将配置文档存储在 C:\data\mongos\cfg_mongos.conf,使用 –port 参数指定 mongos 进程监听的端口。

--mongos 1
logpath=C:\data\mongos\mongos_log.log port=60001 configdb=cfg-svr1:50001,cfg-svr2:50002,cfg-svr2:50003

启动 mongos

mongos -f C:\data\mongos\cfg_mongos.conf

四,增加 Shard

1,连接到 mongos

mongo --host router-svr1 --port 60001

查看分片的状态,分片集群中并没有任何一个 shard

sh.status()

2,增加 Shard

每一个 shard 用于存储数据的一个分片,存储数据的 Server 可以是 Replica Set,也可以是 Standalone mongod。

为分片集群增加一个 Replica Set 分片

sh.addShard("replica_set_name/host:port")

为分片集群增加一个 Standalone mongod

sh.addShard("host:port")

3,使数据库启用分片存储

sh.enableSharding("database name")

4,使数据库中的一个集合启用分片存储
在将 collection 启用分片存储之前,必须在 collection 上创建单键或双键 index。

db.collection_name.createIndex({field:1})

sh.shardCollection("dbname.collection_name",{field:1})

5,向集合中插入,MongoDB 将自动管理分片

db.collection_name.insert({....})

Application 连接 mongos,写入或读取数据,由 mongos 路由到相应的 shard,这个过程是自动完成的。

更多 MongoDB 相关教程见以下内容

CentOS 编译安装 MongoDB 与 mongoDB 的 php 扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm

CentOS 6 使用 yum 安装 MongoDB 及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm

Ubuntu 13.04 下安装 MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm

MongoDB 入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm

Ubunu 14.04 下 MongoDB 的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Nagios 监控 MongoDB 分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm

基于 CentOS 6.5 操作系统搭建 MongoDB 服务 http://www.linuxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

参考文档:

Sharded Cluster Administration

Sharding

MongoDB – Sharding

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135648.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计4676字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中