平台支撑:Centos7.3
mongo版本:mongodb-linux-x86_64-3.4.0
| IP |
| 192.168.56.110 |
| 192.168.56.111 |
| 192.168.56.112 |
一、概念:
分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器)。大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差(+++里面的说明+++),查询则尽量避免跨分片查询。使用分片的时机:
1,机器的磁盘不够用了。使用分片解决磁盘空间的问题。
2,单个mongod已经不能满足写数据的性能要求。通过分片让写压力分散到各个分片上面,使用分片服务器自身的资源。
3,想把大量数据放到内存里提高性能。和上面一样,通过分片使用分片服务器自身的资源。
一、架构:
规划5个组件对应的端口号,由于每台机器均需要同时部署 mongos、config server 、shard1、shard2、shard3,所以需要用端口进行区分。
端口可以自由定义,本架构中 mongos为 20000, config server 为 21000, shard1为 22001 , shard2为22002, shard3为22003.
一、操作:
1)创建目录
mkdir mongos shard1 shard2 shard3 conf config db logs
需要创建mongos shard1 shard2 shard3 conf config db logs目录来存放数据及日志和PID文件等。
2)配置conf文件
需要先进入conf目录在创建相应文件
创建config.conf文件
vim config.conf
dbpath=/home/mongo/db/config #数据目录
logpath=/home/mongo/logs/config.log #日志文件
pidfilepath=/home/mongo/config/config.pid
replSet=cfgset #replica set的名字
configsvr=true
port=21000
fork=true #守护进程模式
logappend=true #增长式日志
storageEngine=wiredTiger #存储引擎,配置服务器只能为wiredTiger
profile=1
slowms=200 #慢查询限制,200ms
directoryperdb=true #每个数据单独目录
maxConns=20000 #最大连接数
创建mongos.conf文件
vim mongos.conf
configdb=cfgset/192.168.56.110:21000,192.168.56.111:21000,192.168.56.112:21000
logpath=/home/mongo/logs/mongos.log
pidfilepath=/home/mongo/mongos/mongos.pid
port=20000
fork=true
logappend=true
maxConns=20000#最大连接数
创建shard1.conf
vim shard1.conf
dbpath=/home/mongo/shard1
logpath=/home/mongo/logs/shard1.log
pidfilepath=/home/mongo/shard1/shard.pid
shardsvr=true
logappend=true
replSet=shard1
storageEngine=wiredTiger
slowms=200#慢查询限制,200ms
directoryperdb=true#每个数据单独目录
maxConns=20000
bind_ip=192.168.56.110
port=22001
oplogSize=10000
fork=true
noprealloc=true
profile=1
创建shard2.conf和创建shard3.conf只需要拷贝shard1.conf把
dbpath=数据路径
logpath=日志路径
pidfilepath=PID存放路径
replSet=#replica set的名字
port=#监听端口(shard2=22002,shard3=22003)
3)启动
创建好之后可以把此目录打包,分别上传至其他两台机器启动mongo。(分别在其他机器上按顺序启动)
mongodb-linux-x86_64-3.4.0/bin/mongod -f conf/config.conf
mongodb-linux-x86_64-3.4.0/bin/mongod -f conf/shard1.conf
mongodb-linux-x86_64-3.4.0/bin/mongod -f conf/shard2.conf
mongodb-linux-x86_64-3.4.0/bin/mongod -f conf/shard3.conf
mongodb-linux-x86_64-3.4.0/bin/mongos -f conf/mongodb.conf
提示如下为启动成功:
about to fork child process, waiting until server is ready for connections.
forked process: 8545
child process started successfully, parent exiting
4)配置副本集
连接到任意一台配置服务器上
/app/mongo/bin/mongo --host 192.168.56.110 --port 21000
创建配置服务器副本集
rs.initiate({_id:"cfgset",configsvr:true,members:[{_id:0,host:"192.168.56.110:21000"},{_id:1,host:"192.168.56.111:21000"},{_id:2,host:"192.168.56.112:21000"}]})
连接任意一台分片服务器
/app/mongo/bin/mongo --host 192.168.56.110 --port 22001
创建副本集并初始化
rs.initiate({_id:"shard1",members:[{_id:0,host:"192.168.56.110:22001,arbiterOnly:true"},{_id:1,host:"192.168.56.111:22001"},{_id:2,host:"192.168.56.112:22001"}]})
连接任意一台分片服务器
/app/mongo/bin/mongo --host 192.168.56.111 --port 22002
创建副本集并初始化
rs.initiate({_id:"shard2",members:[{_id:0,host:"192.168.56.110:22002"},{_id:1,host:"192.168.56.111:22002,arbiterOnly:true"},{_id:2,host:"192.168.56.112:22002"}]})
连接任意一台分片服务器
/app/mongo/bin/mongo --host 192.168.56.112 --port 22003
创建副本集并初始化
rs.initiate({_id:"shard3",members:[{_id:0,host:"192.168.56.110:22003"},{_id:1,host:"192.168.56.111:22003"},{_id:2,host:"192.168.56.112:22003,arbiterOnly:true"}]})
5)添加分片到集群
登录路由服务客户端
/app/mongo/bin/mongo --host 192.168.56.110 --port 20000
添加分片到集群
sh.addShard("shard1/192.168.56.110:22001,192.168.56.112:22001,192.168.56.113:22001")
依次添加shard2 shard3
sh.addShard("shard2/192.168.56.110:22002,192.168.56.112:22002,192.168.56.113:22002")
sh.addShard("shard3/192.168.56.110:22003,192.168.56.112:22003,192.168.56.113:22003")
--Enable Sharding for a Database
sh.enableSharding("test")
--Shard a Collection
sh.shardCollection("test.testdoc", { id: 1})
二、测试:
插入测试数据
/app/mongo/bin/mongo --host 192.168.56.111 --port 20000
mongos> use test
for(var i = 1; i <= 1000; i++){
db.testdoc.save({id:i,"message":"message"+i});
}
mongos> db.testdoc.status()
文献:
https://www.cnblogs.com/vadim/p/7100683.html