MongoDB分片群集
标签: MongoDB分片群集 MongoDB博客 51CTO博客
2023-07-28 18:24:29 137浏览
一、MongoDB分片群集
1、MongoDB分片群集架构
1)Mongos
路由节点
访问MongoDB群集的入口
将用户读取请求转发到指定的分片复制群集
合并多个MongoDB复制群集节点的数据
2)Config Server
配置节点
提供群集数据元存储
帮助客户端定位分片群集所在的MongoDB节点
3)shards数据节点
存储分片后的数据
shards节点需要配置复制群集避免单节点故障数据丢失
分片不能超过1024个,一个分片数据不超过2TB
2、分片群集数据分布的方式
1)基于范围的
查询性能高速度快
分片不均匀
2)基于hash
数据分布均匀
适合高并发写入使用
读取效率低
二、配置MongoDB分片复制群集
1、配置config server群集(三台01~03)
1)修改主配置文件(另外两台只需要修改ip)
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.10,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: configReplset
sharding:
clusterRole: configsvr
processManagement:
fork: true
2)启动服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
3)将config server添加到复制群集中(另外两台需要启动服务才能加入群集)
[root@centos01 ~]# mongo
> use admin
> config={_id: 'configReplset',members:[
... {_id:0,host:'192.168.100.10:27017'},
... {_id:1,host:'192.168.100.20:27017'},
... {_id:2,host:'192.168.100.30:27017'}]
... }
4)初始化群集
> rs.initiate(config)
2、配置Mongos节点(1台 04)
1)修改主配置文件
[root@centos04 ~]# vim /usr/local/mongodb/conf/mongos.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongos.log
logAppend: true
net:
bindIp: 192.168.100.40,127.0.0.1
port: 27017
sharding:
configDB: configReplset/192.168.100.10:27017,192.168.100.20:27017,192.168.100.30:27017
processManagement:
fork: true
2)启动服务
[root@centos04 ~]# mongos -f /usr/local/mongodb/conf/mongos.conf
3)查看服务运行状态
[root@centos04 ~]# netstat -anptu | grep mongos
3、配置分片群集sh1(三台05~07)
1)修改主配置文件(另外两台只需要修改IP即可)
[root@centos05 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.50,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: sh1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
2)启动服务
mongod -f /usr/local/mongodb/conf/mongo.conf
3)将sh1添加到复制群集中(另外两台需要启动服务才能加入群集)
[root@centos05 ~]# mongo
> use admin
> config={_id: 'sh1',members:[
... {_id:0,host:'192.168.100.50:27017'},
... {_id:1,host:'192.168.100.60:27017'},
... {_id:2,host:'192.168.100.70:27017'}]
... }
4)初始化群集
> rs.initiate(config)
4、配置分片群集sh2(三台08~10)
1)修改主配置文件(另外两台只需要修改IP即可)
[root@centos08 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /usr/local/mongodb/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 192.168.100.80,127.0.0.1
port: 27017
replication:
oplogSizeMB: 2048
replSetName: sh2
sharding:
clusterRole: shardsvr
processManagement:
fork: true
2)启动服务
mongod -f /usr/local/mongodb/conf/mongo.conf
3)将sh2添加到复制群集中(另外两台需要启动服务才能加入群集)
[root@centos08 ~]# mongo
> use admin
> config={_id: 'sh2',members:[
... {_id:0,host:'192.168.100.80:27017'},
... {_id:1,host:'192.168.100.90:27017'},
... {_id:2,host:'192.168.100.100:27017'}]
... }
4)初始化群集
> rs.initiate(config)
5、在mongos节点配置分片群集
1)登录Mongos节点
[root@centos04 ~]# mongo 192.168.100.40:27017/admin
2)连接到分片复制群集
mongos> db.runCommand( {addshard:"sh1/192.168.100.50:27017,192.168.100.60:27017,192.168.100.70:27017",name:"shard1"} )
mongos> db.runCommand( {addshard:"sh2/192.168.100.80:27017,192.168.100.90:27017,192.168.100.100:27017",name:"shard2"} )
3)查看mongos链接的分片群集
mongos> db.runCommand({listshards:1})
4)查看分片群集运行状态
mongos> sh.status()
6、应用分片群集
1)切换到test库
mongos> use test
2)对test库的st集合的id列开启分片
mongos> db.st.ensureIndex({id:1})
3)切换到admin库开启分片
mongos> use admin
mongos> db.runCommand({enablesharding:"test"})
mongos> db.runCommand({shardcollection:"test.st",key:{id:1}})
4)写入测试数据
mongos> use test
mongos> for(i=1;i<50000;i++){db.st.insert({"id":i,"name":"bob","age":70,"date":new Date()});}
5)查看数据发现群集sh2上都有数据
7、配置启动hash分片
查看数据发现数据一部分分配到sh1一部分分配到了sh2
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
展开评论