mongodb 集群搭建 分片
简介
一共有三部分,配置服务器,分片服务器,mongos服务。
配置服务器:
配置服务器相当于集群的大脑,保存这集群和分片元数据。mongos需要从配置服务器获取配置信息因此配置服务器要先于任何mongos启动。(注意:3.4版本以后配置服务器必须是一个副本集)。
分片服务器:
分片要建立在副本集之上,要先搭建一个副本集才能进行分片。每个副本集是一个分片。
mongos:
mongos是用来处理客户端请求的,实现数据读写自动路由等功能。
配置服务器
配置服务器:
先创建配置服务器,配置服务器为一个副本集。
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # Where and how to store data. storage: dbPath: /var/lib/mongo journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile # network interfaces net: port: 27019 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: replication: replSetName: configserver sharding: clusterRole: configsvr ## Enterprise-Only Options #auditLog: #snmp:
配置文件支持两种格式,一种是键值模式,另一种是 yaml 格式,当前的就是 yaml 格式。
clusterRole: configsvr 这项表示当前配置的是配置服务器,这项的值是固定的几个,值为configsvr 时表示当前配置的为配置服务器,当值为 shardsvr 时说明配置的是分片服务器。
repliSetName: configserver 这项是用来配置和副本集的,因为分片要基于副本集来配置。
bindIp: 0.0.0.0 设置为 0.0.0.0 表示开发端口,便于连接。
port: 27019 这个端口可以任意,配置服务器要使用同一个端口。
键值模式的配置:
## 配置文件内容 pidfilepath = /var/run/mongodb/configsrv.pid dbpath = /var/lib/mongo logpath = /var/log/congigsrv.log logappend = true bind_ip = 0.0.0.0 port = 27019 fork = true # 配置服务器 configsvr = true # 副本集名称 replSet=configs
分片服务器
分片服务器配置:
创建完配置服务器后再创建分片服务,一个副本集为一个分片服务。
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/shard2.log # Where and how to store data. storage: dbPath: /var/lib/mongo journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/shard2.pid # location of pidfile # network interfaces net: port: 27002 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: replication: replSetName: shard2 sharding: clusterRole: shardsvr ## Enterprise-Only Options #auditLog: #snmp:
mongos
mongos配置:
创建完副本集和分片后启动 mongo shell 将分片逐个加入到集群中。
pidfilepath = /var/run/mongodb/mongos.pid logpath = /var/log/mongo/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true configdb = configserver/192.168.1.121:27019,192.168.1.122:27019,192.168.1.123:27019 maxConns = 2000
configdb 这项用来指定配置服务器,configserver 这个名字就是上面 repliSetName: configserver 设置的名称。这个服务器只能有一台或三台。
yaml格式的配置:
systemLog: destination: file logAppend: true path: /root/log/mongos.log processManagement: fork: true pidFilePath: /var/run/mongodb/mongos.pid net: port: 20000 bindIp: 0.0.0.0 sharding: configDB: configserver/192.168.1.113:27019,192.168.1.114:27019,192.168.1.115:27019
将分片添加到集群:
sh.addShard("shard1/192.168.1.113:27001,192.168.1.114:27017,192.168.1.115:27001") { "shardAdded" : "shard1", "ok" : 1 } sh.addShard("shard2/192.168.1.124:27002,192.168.1.125:27002,192.168.1.126:27002") { "shardAdded" : "shard2", "ok" : 1 } sh.addShard("shard3/192.168.1.121:27003,192.168.1.122:27003,192.168.1.123:27003") { "shardAdded" : "shard2", "ok" : 1 }
查看分片状态:
mongo --port 20000 sh.status()
对集合分片
对集合进行分片:
搭建好分片集群后此时并没有对集合进行分片,需要制定要分片的集合,根据那个字段进行分片。
指定testdb分片生效:
use admin db.runCommand( { enablesharding :"testdb"});
指定数据库里需要分片的集合和片键:
db.runCommand({ shardcollection: 'testdb.user', key:{ username: 1}})