相关概念
Sharding 是一种跨多台计算机分布数据的方法。MongoDB 使用Sharing支持具有非常大的数据集和高吞吐量的部署。
MongoDB Sharding集群组件组成:
下图描述了Sharding集群中的组件之间的交互作用:

shard: 每个shard都包含切分数据的子集。每个碎片可以部署为replica set。
个人理解:就是个库文件,分散在各服务器上,replica set为各库文件的备份,以防止某个库文件服务器宕机或损坏,提供健壮性
mongos: 充当查询路由器, 提供客户端应用程序和切分群集之间的接口。
个人理解:就是个数据库引擎,客户端的访问入口。不过有一点不是很明白,配置多个mongos的情况下,客户端直连的那台挂掉了,还是不能提供健壮性。有可能还是需要IP live + VIP 把部署的多个mongos组合下,客户端指向VIP,才好用
博客写着写着就感觉不对,他们其实绑定的都是同一个IP和端口,我觉得应该集成了我上边说的功能、
config servers: 存储集群的元数据和配置设置。从 MongoDB 3.4, 配置服务器必须部署为副本集 (CSRS)。
个人理解:这东西就是个配置中心,把 shard 的关系下存储起来,由mongos调用。mongos本身并不存储任何信息,配置信息都由这东西保管。 这玩意也可以用客户端连进去,可以去看看结构。3.4版本以上,要求这东西也要做冗余。
Shard Keys:若要使用Shard,则必须制定Shard Keys。 Shard Keys由目标集合中每个文档中存在的字段组成。
这东西并没有体现在上图中,但是如果要利用Sharding集群的情况下,必须设定他,且设定后无法更改。作为平衡各个Shard之间的关键。 这里就是提一下,后边着重在聊。
环境准备
操作系统: CentOs 7.4
三台服务器:10.1.4.140/147/36
安装包: mongodb-linux-x86_64-rhel70-3.6.3.tgz
服务器规划
| 服务器140 | 服务器147 | 服务器36 |
| mongos | mongos | mongso |
| configs | configs | configs |
| shard1 | shard1 | shard1 |
| shard2 | shard2 | shard2 |
| shard3 | shard3 | shard3 |
端口分配:
mongos: 20000 configs: 21000 shard1: 27001 shard2: 27002 shard3: 27003
MongoDb安装
1. 安装mongodb
#解压 tar -xzvf mongodb-linux-x86_64-rhel70-3.6.3.tgz -C /usr/local/ #改名 mv mongodb-linux-x86_64-rhel70-3.6.3 mongodb
2.创建相关目录
根据服务器的规范,分别在对应的服务器上建立conf、mongos、config、shard1、shard2、shard3等目录,因为mongos不存储数据,只需要建立日志文件目录即可。
mkdir -p /usr/local/mongodb/conf mkdir -p /data/mongos/log mkdir -p /data/config/data mkdir -p /data/config/log mkdir -p /data/shard1/data mkdir -p /data/shard1/log mkdir -p /data/shard2/data mkdir -p /data/shard2/log mkdir -p /data/shard3/data mkdir -p /data/shard3/log
3.环境变量
为了后续方便操作,配置mongodb的环境变量
vi /etc/profile # 内容 export MONGODB_HOME=/usr/local/mongodb export PATH=$MONGODB_HOME/bin:$PATH # 使立即生效,在安装用户下(youknow)执行 source /etc/profile
查看mongodb版本信息,输出版本信息表明配置环境变量成功
mongod -v
集群搭建
1. configs server配置
在每台服务器上都做如下配置
vi /usr/local/mongodb/conf/config.conf ## content systemLog: destination: file logAppend: true path: /data/config/log/config.log # Where and how to store data. storage: dbPath: /data/config/data journal: enabled: true # how the process runs processManagement: fork: true pidFilePath: /data/config/log/configsrv.pid # network interfaces net: port: 21000 bindIp: 10.1.4.140 #operationProfiling: replication: replSetName: config sharding: clusterRole: configsvr
其中replSetName、bindIp、port三个参数比较重要
replSetName为配置别名 与 bindIp、port这三个组成该配置的唯一定位
启动三台服务器的config server
mongod --config /usr/local/mongodb/conf/config.conf
登录任意一台配置服务器,初始化配置副本集
#连接 mongo 192.168.0.33:21000 #config变量 config = { ... _id : "config", ... members : [ ... {_id : 0, host : "10.1.4.140:21000" }, ... {_id : 1, host : "10.1.4.147:21000" }, ... {_id : 2, host : "10.1.4.36:21000" } ... ] ... } #初始化副本集 rs.initiate(config) #查看分区状态 rs.status();
2. 配置分片
shard1
在服务器140,147,36上分别做如下配置
配置文件
vi /usr/local/mongodb/conf/shard1.conf #配置文件内容 # where to write logging data. systemLog: destination: file logAppend: true path: /data/shard1/log/shard1.log # Where and how to store data. storage: dbPath: /data/shard1/data journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 20 # how the process runs processManagement: fork: true pidFilePath: /data/shard1/log/shard1.pid # network interfaces net: port: 27001 bindIp: 10.1.4.147 #operationProfiling: replication: replSetName: shard1 sharding: clusterRole: shardsvr
启动三台服务器的shard1 server
mongod --config /usr/local/mongodb/conf/shard1.conf
登陆任意一台服务器,初始化副本集
mongo 10.1.4.147:27001 #使用admin数据库 use admin #定义副本集配置 config = { ... _id : "shard1", ... members : [ ... {_id : 0, host : "10.1.4.147:27001" }, ... {_id : 1, host : "10.1.4.140:27001" }, ... {_id : 2, host : "10.1.4.36:27001" } ... ] ... } #初始化副本集配置 rs.initiate(config); #查看分区状态 rs.status();
shard2
在服务器140,147,36上分别做如下配置
配置文件
vi /usr/local/mongodb/conf/shard2.conf #配置文件内容 # where to write logging data. systemLog: destination: file logAppend: true path: /data/shard2/log/shard2.log # Where and how to store data. storage: dbPath: /data/shard2/data journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 20 # how the process runs processManagement: fork: true pidFilePath: /data/shard2/log/shard2.pid # network interfaces net: port: 27002 bindIp: 10.1.4.140 #operationProfiling: replication: replSetName: shard2 sharding: clusterRole: shardsvr
启动三台服务器的shard2 server
mongod --config /usr/local/mongodb/conf/shard2.conf
登陆任意一台服务器,初始化副本集
mongo 10.1.4.147:27001 #使用admin数据库 use admin #定义副本集配置 config = { ... _id : "shard2", ... members : [ ... {_id : 0, host : "10.1.4.147:27002" }, ... {_id : 1, host : "10.1.4.36:27002" }, ... {_id : 2, host : "10.1.4.140:27002" } ... ] ... } #初始化副本集配置 rs.initiate(config); #查看分区状态 rs.status();
shard3
在服务器140,147,36上分别做如下配置
配置文件
vi /usr/local/mongodb/conf/shard3.conf #配置文件内容 # where to write logging data. systemLog: destination: file logAppend: true path: /data/shard3/log/shard3.log # Where and how to store data. storage: dbPath: /data/shard3/data journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 20 # how the process runs processManagement: fork: true pidFilePath: /data/shard3/log/shard3.pid # network interfaces net: port: 27003 bindIp: 10.1.4.36 #operationProfiling: replication: replSetName: shard3 sharding: clusterRole: shardsvr
启动三台服务器的shard3 server
mongod --config /usr/local/mongodb/conf/shard3.conf
登陆任意一台服务器,初始化副本集
mongo 10.1.4.147:27001
#使用admin数据库
use admin
#定义副本集配置
config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "10.1.4.36:27003" },
... {_id : 1, host : "10.1.4.140:27003" },
... {_id : 2, host : "10.1.4.147:27003" }
... ]
... }
#初始化副本集配置
rs.initiate(config);
#查看分区状态
rs.status();
3. mongos 配置
在服务器140,147,36上分别做如下配置
注意:先启动配置服务器和分片服务器,后启动路由实例
vi /usr/local/mongodb/conf/mongos.conf systemLog: destination: file logAppend: true path: /data/mongos/log/mongos.log processManagement: fork: true # pidFilePath: /usr/local/mongodb/mongos.pid # network interfaces net: port: 20000 bindIp: 10.1.4.140 #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字 sharding: configDB: config/10.1.4.140:21000,10.1.4.147:21000,10.1.4.36:21000
启动二台服务器的mongos server
mongos --config /usr/local/mongodb/conf/mongos.conf
启用分片
配置分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
mongo 10.1.4.140:20000 #使用admin数据库 use admin #串联路由服务器与分配副本集 sh.addShard("shard1/10.1.4.147:27001,10.1.4.140:27001,10.1.4.36:27001") sh.addShard("shard2/10.1.4.140:27002,10.1.4.36:27002,10.1.4.147:27002") sh.addShard("shard3/10.1.4.36:27003,10.1.4.147:27003,10.1.4.140:27003") #查看集群状态 sh.status()
为集合设置分片
配置分片后,每个集合都会默认有一个Primary分片,此时集合是没有启用分片功能的
通过命令为集合与文档启用分片
mongo 10.1.4.140:20000 use admin #为ShardTest集合启用分片 sh.enableSharding("ShardTest") #为ShardTest集合的Test文档,按照Id进行Hash分片,id为sharding keys(片键) sh.shardCollection("ShardTest.Test",{"_id":"hashed"})
在分片集合时选择片键。分片后, 无法更改分片键的选择。分片集合只能有一个分片键。
若要对非空集合进行分片, 集合必须具有以分片键开头的索引 。对于空集合, 如果集合没有指定的分片键的适当索引, MongoDB 将创建索引
分片键的选择会影响切分集群的性能、效率和可伸缩性。分片键及其支持索引的选择还会影响群集可以使用的分片策略 。
关于分片的内容还有很多,后续会在开一篇文章对分片进行单独介绍,也可参照下方url地址进行参考
https://docs.mongodb.com/manual/core/sharding-shard-key/
引用内容:
https://docs.mongodb.com/manual/sharding/
http://www.cnblogs.com/ityouknow/p/7566682.html
浙公网安备 33010602011771号