Linux ·

MongoDB 搭建分片集群

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

MongoDB 搭建分片集群 Linux 第1张

一,配置服务器

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,这个过程是自动完成的。

更多

参与评论