一、mongo分片集群技术概要
1、mongodb分片技术
mongodb分片技术能够把数据分成两份存储,假设db.myuser里面有1亿条数据,分片能够实现5千万左右存储在data1,5千万左右存储在data2
data1、data2需要使用副本集的形式,预防数据丢失
2、mongodb分片集群由三种角色组成
1>router角色: mongodb的路由,提供入口,使得分片集群对外透明。router不存储数据
configsvr角色: mongodb的配置角色,存储元数据信息。分片集群后端有多份存储,读取数据该去哪个存储上读取,依赖于配置角色。配置角色建议使用副本集
shardsvr角色: mongodb的存储角色,存储真正的数据,建议使用副本集
3、依赖关系
当用户通过router角色插入数据时,通过configsvr获取这份数据插入到哪个节点,然后执行插入动作插入数据到sharedsvr
当用户通过router角色获取数据时,通过configsvr获取这份数据是存储在哪个节点,然后再去sharedsvr获取数据
分片概念学习:很详细的分片集群知识结构体系
二、集群环境介绍
IP地址 | 配置规格 | 主机名 | 系统版本 |
---|---|---|---|
192.168.86.11 | 1核2G | router01 | centos7.7 |
192.168.86.12 | 1核2G | router02 | centos7.7 |
192.168.86.13 | 1核2G | router03 | centos7.7 |
192.168.86.14 | 1核2G | configsvr01 | centos7.7 |
192.168.86.15 | 1核2G | configsvr02 | centos7.7 |
192.168.86.16 | 1核2G | configsvr03 | centos7.7 |
192.168.86.17 | 1核2G | shardsvr-one-01 | centos7.7 |
192.168.86.18 | 1核2G | shardsvr-one-02 | centos7.7 |
192.168.86.19 | 1核2G | shardsvr-one-03 | centos7.7 |
192.168.86.10 | 1核2G | shardsvr-two-01 | centos7.7 |
192.168.86.11 | 1核2G | shardsvr-two-02 | centos7.7 |
192.168.86.12 | 1核2G | shardsvr-two-03 | centos7.7 |
三、搭建configsrv集群
1、部署configsrv角色集群
基础环境配置部署参考:mongodb部署和优化
2、修改configsrv集群配置/etc/mongod.conf
# 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
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0configsvr
sharding:
clusterRole: configsvr
3、启动mongo
systemctl restart mongod.service
netstat -anptu|grep mongo
4、分片集群的配置角色副本集搭建(_id需要和replSetName保持一致)
mongo 192.168.86.14:27017
use admin
config = { _id:"rs0configsvr",
configsvr: true,
members:[
{_id:0,host:"192.168.86.14:27017"},
{_id:1,host:"192.168.86.15:27017"},
{_id:2,host:"192.168.86.16:27017"}
]
}
rs.initiate(config)
#查看集群状态和"configsvr"是否为true
rs.status()
四、搭建route集群
1、部署router角色集群
router说明
mongodb中的router角色只负责提供一个入口,不存储任何的数据
router最重要的配置
指定configsvr的地址,使用副本集id+ip端口的方式指定
配置多个router,任何一个都能正常的获取数据
基础环境配置部署参考:mongodb部署和优化
2、修改router集群配置/etc/mongos.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
sharding:
configDB: rs0configsvr/192.168.86.14:27017,192.168.86.15:27017,192.168.86.16:27017
3、各个节点启动route集群
mongos -f /etc/mongos.conf
五、搭建shardsvr集群
0、部署shardsvr角色集群
基础环境配置部署参考:mongodb部署和优化
1、shardone集群修改 /etc/mongod.conf为以下内容
# 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
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#副本集名称需要保持一致,包括扩容节点
replication:
replSetName: shardone
sharding:
clusterRole: shardsvr
2、启动shardone mongo集群
systemctl restart mongod.service
netstat -anptu|grep 27017
3、shardone mongodb集的初始化并查看状态
mongo 192.168.86.17:27017
use admin
config = { _id:"shardone", members:[
{_id:0,host:"192.168.86.17:27017"},
{_id:1,host:"192.168.86.18:27017"},
{_id:2,host:"192.168.86.19:27017"}]
}
rs.initiate( config )
rs.status()
4、shardtwo集群修改 /etc/mongod.conf为以下内容
# 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
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#副本集名称需要保持一致,包括扩容节点
replication:
replSetName: shardtwo
sharding:
clusterRole: shardsvr
6、启动shardtwo mongo集群
systemctl restart mongod.service
netstat -anptu|grep 27017
7、shardtwo mongodb集的初始化并查看状态
mongo 192.168.86.20:27017
use admin
config = { _id:"shardtwo", members:[
{_id:0,host:"192.168.86.20:27017"},
{_id:1,host:"192.168.86.21:27017"},
{_id:2,host:"192.168.86.22:27017"}]
}
rs.initiate( config )
rs.status()
六、分片集群配置
1、分片集群添加数据角色,连接到路由角色里面配置,数据角色为副本集的方式
mongo 192.168.86.11:27017
sh.status()
sh.addShard("shardone/192.168.86.17:27017,192.168.86.18:27017,192.168.86.19:27017")
sh.addShard("shardtwo/192.168.86.20:27017,192.168.86.21:27017,192.168.86.22:27017")
2、默认添加数据没有分片存储,操作都是在路由角色里面
use duanshuaixing-mongodb
for(i=1; i<=500;i++){
db.user.insert( {name:'mytest'+i, age:i} )
}
sh.status()
db.dropDatabase() #验证完后删除
3、针对某个数据库的某个表使用hash分片存储,分片存储就会同一个colloection分配两个数据角色
在路由角色操作
mongo 192.168.86.11:27017
use admin
db.runCommand( { enablesharding :"duanshuaixing-mongodb"});
db.runCommand( { shardcollection : "duanshuaixing-mongodb.user",key : {_id: "hashed"} } )
插入数据校验,分布在两个数据角色上
use duanshuaixing-mongodb
for(i=1; i<=500;i++){ db.user.insert( {name:'mytest'+i, age:i} ) }
七、可用性验证
1、配置角色如果挂掉一台会不会有影响
2、验证mongos多个入口是否能够正常使用