MongoDB单机分片集群配置
2019-06-21 本文已影响0人
广州秀才
MongoDB单机分片集群配置
一、规划
- 服务器IP:172.18.210.1
- mongos:20000
- config server:21001-21003(1主两从,不支持arbiter,复制集名字configReplSet)
- shard节点:
shard1:22011-22013(1主两从,其中一个节点为arbiter,复制集名字shard1)
shard2:22021-22023(1主两从,其中一个节点为arbiter,复制集名字shard2)
二、配置
1. shard复制集配置:
1.1. 目录创建:
# mkdir -p /mongodb/22011/conf /mongodb/22011/log /mongodb/22011/data
# mkdir -p /mongodb/22012/conf /mongodb/22012/log /mongodb/22012/data
# mkdir -p /mongodb/22013/conf /mongodb/22013/log /mongodb/22013/data
# mkdir -p /mongodb/22021/conf /mongodb/22021/log /mongodb/22021/data
# mkdir -p /mongodb/22022/conf /mongodb/22022/log /mongodb/22022/data
# mkdir -p /mongodb/22023/conf /mongodb/22023/log /mongodb/22023/data
1.2. 修改配置文件:
shard1:
# cat >> /mongodb/22011/conf/mongodb.conf <<EOF
systemLog:
destination: file
path: /mongodb/22011/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/22011/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 22011
replication:
oplogSizeMB: 2048
replSetName: shard1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
EOF
# cp /mongodb/22011/conf/mongodb.conf /mongodb/22012/conf/
# cp /mongodb/22011/conf/mongodb.conf /mongodb/22013/conf/
# sed 's#22011#22012#g' /mongodb/22012/conf/mongodb.conf -i
# sed 's#22011#22013#g' /mongodb/22013/conf/mongodb.conf -i
shard2:
# cat >> /mongodb/22021/conf/mongodb.conf << EOF
systemLog:
destination: file
path: /mongodb/22021/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/22021/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 22021
replication:
oplogSizeMB: 2048
replSetName: shard2
sharding:
clusterRole: shardsvr
processManagement:
fork: true
EOF
# cp /mongodb/22021/conf/mongodb.conf /mongodb/22022/conf/
# cp /mongodb/22021/conf/mongodb.conf /mongodb/22023/conf/
# sed 's#22021#22022#g' /mongodb/22022/conf/mongodb.conf -i
# sed 's#22021#22023#g' /mongodb/22023/conf/mongodb.conf -i
1.3. 启动所有节点:
# mongod -f /mongodb/22011/conf/mongodb.conf
# mongod -f /mongodb/22012/conf/mongodb.conf
# mongod -f /mongodb/22013/conf/mongodb.conf
# mongod -f /mongodb/22021/conf/mongodb.conf
# mongod -f /mongodb/22022/conf/mongodb.conf
# mongod -f /mongodb/22023/conf/mongodb.conf
1.4. 搭建复制集:
# mongo --port 22011
use admin
config = {_id: 'shard1', members: [
{_id: 0, host: '172.18.210.1:22011'},
{_id: 1, host: '172.18.210.1:22012'},
{_id: 2, host: '172.18.210.1:22013',"arbiterOnly":true}]
}
rs.initiate(config)
# mongo --port 22021
use admin
config = {_id: 'shard2', members: [
{_id: 0, host: '172.18.210.1:22021'},
{_id: 1, host: '172.18.210.1:22022'},
{_id: 2, host: '172.18.210.1:22023',"arbiterOnly":true}]
}
rs.initiate(config)
2. config节点配置:
2.1. 目录创建:
# mkdir -p /mongodb/21001/conf /mongodb/21001/log /mongodb/21001/data
# mkdir -p /mongodb/21002/conf /mongodb/21002/log /mongodb/21002/data
# mkdir -p /mongodb/21003/conf /mongodb/21003/log /mongodb/21003/data
2.2. 修改配置文件:
# cat >> /mongodb/21001/conf/mongodb.conf << EOF
systemLog:
destination: file
path: /mongodb/21001/log/mongodb.conf
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/21001/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 21001
replication:
oplogSizeMB: 2048
replSetName: configReplSet
sharding:
clusterRole: configsvr
processManagement:
fork: true
EOF
# cp /mongodb/21001/conf/mongodb.conf /mongodb/21002/conf/
# cp /mongodb/21001/conf/mongodb.conf /mongodb/21003/conf/
# sed 's#21001#21002#g' /mongodb/21002/conf/mongodb.conf -i
# sed 's#21001#21003#g' /mongodb/21003/conf/mongodb.conf -i
2.3. 启动节点,并配置复制集
# mongod -f /mongodb/21001/conf/mongodb.conf
# mongod -f /mongodb/21002/conf/mongodb.conf
# mongod -f /mongodb/21003/conf/mongodb.conf
# mongo --port 21001
use admin
config = {_id: 'configReplSet', members: [
{_id: 0, host: '172.18.210.1:21001'},
{_id: 1, host: '172.18.210.1:21002'},
{_id: 2, host: '172.18.210.1:21003'}]
}
rs.initiate(config)
注:configserver 可以是一个节点,官方建议复制集。configserver不能有arbiter。新版本中,要求必须是复制集。
注:mongodb 3.4之后,虽然要求config server为replica set,但是不支持arbiter
3. mongos节点配置:
3.1. 创建目录:
# mkdir -p /mongodb/20000/conf /mongodb/20000/log
3.2. 配置文件:
# cat >> /mongodb/20000/conf/mongos.conf <<EOF
systemLog:
destination: file
path: /mongodb/20000/log/mongos.log
logAppend: true
net:
port: 20000
sharding:
configDB: configReplSet/172.18.210.1:21001,172.18.210.1:21002,172.18.210.1:21003
processManagement:
fork: true
EOF
3.3. 启动mongos
# mongos -f /mongodb/20000/conf/mongos.conf
三、分片集群操作:
连接到其中一个mongos(172.18.210.1),做以下配置
1. 连接到mongs的admin数据库
# su - mongod
$ mongo 172.18.210.1:20000/admin
2. 添加分片
db.runCommand( { addshard : "shard1/172.18.210.1:22011,172.18.210.1:22012,172.18.210.1:22013",name:"shard1"} )
db.runCommand( { addshard : "shard2/172.18.210.1:22021,172.18.210.1:22022,172.18.210.1:22023",name:"shard2"} )
3. 列出分片
db.runCommand( { listshards : 1 } )
4. 整体状态查看
sh.status();
四、使用分片集群
1. RANGE分片配置及测试( 用的不多, 主要用hash分片 )
test库下的vast大表进行手工分片
1.1. 激活数据库分片功能
# mongo --port 20000 admin
admin> db.runCommand( { enablesharding : "test" } )
1.2. 指定分片键对集合分片
--创建索引
use test
> db.vast.ensureIndex( { id: 1 } )
--开启分片
use admin
> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
1.3. 集合分片验证
use test
test> for(i=1;i<500000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
test> db.vast.stats()
1.4. 分片结果测试
--shard1:
# mongo --port 22011 test
db.vast.count();
--shard2:
# mongo --port 22021 test
db.vast.count();
2. Hash分片例子:(主要用这种)
对oldguo库下的vast大表进行hash
2.1. 激活数据库分片功能
# mongo --port 20000 admin
admin> db.runCommand( { enablesharding : "oldguo" } )
2.2. 指定分片键对集合分片
--创建hash索引
use oldguo
oldguo> db.vast.ensureIndex( { id: "hashed" } )
--开启分片
use admin
admin > sh.shardCollection( "oldguo.vast", { id: "hashed" } )
2.3. 集合分片验证
use oldguo
for(i=1;i<=100000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
2.4. 分片结果测试
# mongo --port 22011 oldguo
db.vast.count();
# mongo --port 22021 oldguo
db.vast.count();
3. 判断是否Shard集群
# mongo --port 20000 admin
admin> db.runCommand({ isdbgrid : 1})
4. 列出所有分片信息
admin> db.runCommand({ listshards : 1})
5. 列出开启分片的数据库
use config
config> db.databases.find( { "partitioned": true } )
或者:
config> db.databases.find() //列出所有数据库分片情况
6. 查看分片的片键
db.collections.find().pretty()
7. 查看分片的详细信息
admin> db.printShardingStatus()
或
admin> sh.status()
8. 删除分片节点(谨慎)
--确认blance是否在工作
sh.getBalancerState()
--删除shard2节点(谨慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即触发blancer。
9. balancer操作
介绍:
mongos的一个重要功能,自动巡查所有shard节点上的chunk的情况,自动做chunk迁移。
什么时候工作?
1、自动运行,会检测系统不繁忙的时候做迁移
2、在做节点删除的时候,立即开始迁移工作
3、balancer只能在预设定的时间窗口内运行
有需要时可以关闭和开启blancer(备份的时候)
mongos> sh.stopBalancer()
mongos> sh.startBalancer()
10. 自定义 自动平衡进行的时间段
// connect to mongos
use config
sh.setBalancerState( true )
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "3:00", stop : "5:00" } } }, true )
sh.getBalancerWindow()
sh.status()
五. 关于集合的balance(了解下)
关闭某个集合的balance
sh.disableBalancing("students.grades")
打开某个集合的balance
sh.enableBalancing("students.grades")
确定某个集合的balance是开启或者关闭
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;