一、概念
搭建mongodb分片的主要原因是为了提高数据的读写性能,分片将数据库拆分,将其分散在不同的机器上,每个片只负责总数据的一部分,最后通过路由来对各个分片进行数据访问。
搭建分片前要了解以下三个组件:
image.png
- Shard:分片服务器,用于存储实际的数据块,实际生产环境中一个shard server 角色可以由几台服务器组成一个Peplica Set 承担,防止主机单点故障.
- Config Server:配置服务器,存储了整个分片群集的配置信息,其中包括chunk信息。
- Routers:前端路由,客户端由此接入,且让整个群集看上去像单一数据库,前端应用可以透明使用。
二、部署安装
使用docker进行部署,总共部署三个环境:
三台配置服务(27018)形成复制集,分片1、2、3也在各机器都部署一个实例,它们之间形成复制集,客户端直接连接3个路由服务与之交互,配置服务和分片服务对客户端是透明的。
image.png安装之前先生存KeyFile文件进行认证
openssl rand -base64 741 > /mongodb/key/mongodb-keyfile
chown -R 999 key/
chmod 600 /mongodb/key/mongodb-keyfile
1、安装配置服务器
配置服务器 | 配置服务端口 |
---|---|
10.42.200.101 | 27018 |
10.42.200.102 | 27018 |
10.42.200.103 | 27018 |
1、新建mongodb.conf配置文件
配置文件中没有使用logpath,因为docker的目录映射问题,一直解决不了。所以最后使用了syslog方式
sudo vim /mongodb/config/config.conf
#数据目录
dbpath=/data/db
#日志文件
#logpath=/data/mongodb.log
#日志追加
logappend=true
#端口
port = 27018
#守护进程模式
#fork = true
#最大连接数
maxConns = 5000
#配置服务器
configsvr = true
#副本集名称
replSet = configs
#日志,redo log
journal = true
#刷写提交机制
journalCommitInterval = 200
#刷写数据到日志的频率
syncdelay = 60
#引擎
storageEngine = wiredTiger
#操作日志,单位M
oplogSize = 1000
#命名空间的文件大小,默认16M,最大2G。
nssize = 16
#绑定地址
bind_ip=0.0.0.0
#认证
auth = true
#keyfile
keyFile = /key/mongodb-keyfile
2、使用配置文件启动三个容器,并设置固定ip
MongodbConfig mongodb分片集群配置服务器
编排文件:
rancher-compose.yml
version: '2'
services:
config2:
scale: 1
start_on_create: true
config3:
scale: 1
start_on_create: true
config1:
retain_ip: true
scale: 1
start_on_create: true
docker-compose.yml
version: '2'
services:
config2:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/configs/config2:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/config.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.102
config3:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/configs/config3:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/config.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.103
config1:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/configs/config1:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/config.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.101
3、 配置复制集
然后连接任意一台mongo
mongo --port 27018
#切换数据库:
use admin
#认证
db.auth('jfwang','123457')
#初始化复制集:
rs.initiate({_id:"configs",members:[{_id:0,host:"10.42.200.101:27018"},{_id:1,host:"10.42.200.102:27018"}, {_id:2,host:"10.42.200.103:27018"}]})
2、分片服务部署
总共启动9个容器。一个容器一个实例,每三个容器形成一个复制集分片。总共三个分片。
分片服务器 | 分片服务端口 |
---|---|
10.42.200.111 | 27001 |
10.42.200.112 | 27001 |
10.42.200.113 | 27001 |
10.42.200.121 | 27002 |
10.42.200.122 | 27002 |
10.42.200.123 | 27002 |
10.42.200.131 | 27003 |
10.42.200.132 | 27003 |
10.42.200.133 | 27003 |
1、 新建shard.conf配置文件
sudo vim /mongodb/config/shard1.conf
#数据目录
dbpath=/data/db
#日志追加
logappend=true
#端口
port = 27001 #其他2个分片对应修改为27002、27003
#最大连接数
maxConns = 5000
#引擎
storageEngine=mmapv1
#分片服务器
shardsvr=true
replSet=shard1 #其他2个分片对应修改为shard2、shard3
#绑定地址
bind_ip=0.0.0.0
#认证
auth = true
#keyfile
keyFile = /key/mongodb-keyfile
2、启动容器
MongodbShard mongodb分片集群分片服务器
编排文件:
rancher-compose.yml
version: '2'
services:
shard13:
scale: 1
start_on_create: true
shard31:
scale: 1
start_on_create: true
shard21:
scale: 1
start_on_create: true
shard32:
scale: 1
start_on_create: true
shard11:
scale: 1
start_on_create: true
shard22:
scale: 1
start_on_create: true
shard33:
scale: 1
start_on_create: true
shard12:
scale: 1
start_on_create: true
shard23:
scale: 1
start_on_create: true
docker-compose.yml
version: '2'
services:
shard13:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard13:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard1.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.113
shard31:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard31:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard3.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.131
shard21:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard21:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard2.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.121
shard32:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard32:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard3.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.132
shard11:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard11:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard1.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.111
shard22:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard22:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard2.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.122
shard33:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard33:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard3.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.133
shard12:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
stdin_open: true
volumes:
- /mongodb/data/shards/shard12:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard1.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.112
shard23:
environment:
MONGO_INITDB_ROOT_USERNAME: jfwang
MONGO_INITDB_ROOT_PASSWORD: 123457
image: mongo
stdin_open: true
volumes:
- /mongodb/data/shards/shard23:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongod
- --config
- /etc/config/shard2.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.200.123
3、将分片配置为复制集
连接mongo,只需在任意一台机器执行即可:
mongo --port 27001 //这里以shard1为例
# 切换数据库
use admin
#认证
db.auth('jfwang','123457')
# 初始化复制集
rs.initiate({_id:"shard1",members:[{_id:0,host:"10.42.200.111:27001"},{_id:1,host:"10.42.200.112:27001"},{_id:2,host:"10.42.200.113:27001"}]})
以上是基于分片1来操作,同理,其他2个分片也要连到各自的端口来执行一遍上述的操作,让3个分片各自形成1主2从的复制集,注意端口及仲裁节点的问题即可,操作完成后3个分片都启动完成,并完成复制集模式。
3、路由服务部署
启动3个容器。形成一个复制集
路由服务器 | 服务端口 |
---|---|
10.42.250.111 | 27017 |
10.42.250.112 | 27017 |
10.42.250.113 | 27017 |
1、新建mongos.conf配置文件
sudo vim /mongodb/config/mongos.conf
#日志追加
logappend=true
#端口
port = 27017
#最大连接数
maxConns = 20000
configdb = aconfigs/10.42.200.101:27018,10.42.200.102:27018,10.42.200.103:27018
#绑定地址
bind_ip=0.0.0.0
#keyfile
keyFile = /key/mongodb-keyfile
2、启动容器
编排文件:
rancher-compose.yml
version: '2'
services:
route3:
scale: 1
start_on_create: true
route2:
scale: 1
start_on_create: true
route1:
scale: 1
start_on_create: true
docker-compose.yml
version: '2'
services:
route3:
image: mongo
stdin_open: true
volumes:
- /mongodb/data/routes/route3:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongos
- --config
- /etc/config/mongos.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.250.113
route2:
image: mongo
stdin_open: true
volumes:
- /mongodb/data/routes/route2:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongos
- --config
- /etc/config/mongos.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.250.112
route1:
image: mongo
stdin_open: true
volumes:
- /mongodb/data/routes/route1:/data
- /mongodb/config:/etc/config
- /mongodb/key:/key
- /etc/localtime:/etc/localtime
tty: true
command:
- mongos
- --config
- /etc/config/mongos.conf
labels:
io.rancher.container.pull_image: always
io.rancher.container.requested_ip: 10.42.250.111
3、启动分片功能
#连接mongo
mongo --port 27017
#切换数据库
use admin
#添加分片,只需在一台机器执行即可:
sh.addShard("shard1/10.42.200.111:27001,10.42.200.112:27001,10.42.200.113:27001")
sh.addShard("shard2/10.42.200.121:27002,10.42.200.122:27002,10.42.200.123:27002")
sh.addShard("shard3/10.42.200.131:27003,10.42.200.132:27003,10.42.200.133:27003")
#查看集群状态
sh.status()
4、配置分片功能
# 块的大小默认是64M,可以修改
use config
db.settings.save({"_id":"chunksize","value":32})
5、实现分片功能
sh.enableSharding("库名")
sh.shardCollection("库名.集合名",{"key":1})
6、删除片键
use config
db.collections.remove({ _id: "NGA.Replay" })
db.chunks.remove({ ns: "NGA.Replay" })
db.locks.remove({ _id: "NGA.Replay" })
use admin
db.adminCommand("flushRouterConfig") #刷新配置服务器
4、测试
略过
网友评论