集群基本結(jié)構(gòu)
搭建
分片策略應(yīng)用
balancer管理
一, 圖解與規(guī)劃
1. 圖解
2. 規(guī)劃 10個(gè)實(shí)例:38017-38026
(1)configserver:
3臺(tái)構(gòu)成的復(fù)制集(1主兩從瑞凑,不支持arbiter)38018-38020(復(fù)制集名字configsvr)
(2)shard節(jié)點(diǎn):
sh1:38021-23 (1主兩從,其中一個(gè)節(jié)點(diǎn)為arbiter,復(fù)制集名字sh1)
sh2:38024-26 (1主兩從,其中一個(gè)節(jié)點(diǎn)為arbiter,復(fù)制集名字sh2)
二, 配置過(guò)程
1. shard復(fù)制集配置:
-
目錄創(chuàng)建:
mkdir -p /mongodb/38021/conf /mongodb/38021/log /mongodb/38021/data mkdir -p /mongodb/38022/conf /mongodb/38022/log /mongodb/38022/data mkdir -p /mongodb/38023/conf /mongodb/38023/log /mongodb/38023/data mkdir -p /mongodb/38024/conf /mongodb/38024/log /mongodb/38024/data mkdir -p /mongodb/38025/conf /mongodb/38025/log /mongodb/38025/data mkdir -p /mongodb/38026/conf /mongodb/38026/log /mongodb/38026/data
-
修改配置文件:
sh1: cat >> /mongodb/38021/conf/mongodb.conf <<EOF systemLog: destination: file path: /mongodb/38021/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/38021/data directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true net: port: 38021 replication: oplogSizeMB: 2048 replSetName: sh1 sharding: clusterRole: shardsvr processManagement: fork: true EOF cp /mongodb/38021/conf/mongodb.conf /mongodb/38022/conf/ cp /mongodb/38021/conf/mongodb.conf /mongodb/38023/conf/ sed 's#38021#38022#g' /mongodb/38022/conf/mongodb.conf -i sed 's#38021#38023#g' /mongodb/38023/conf/mongodb.conf -i sh2: cat >> /mongodb/38024/conf/mongodb.conf << EOF systemLog: destination: file path: /mongodb/38024/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/38024/data directoryPerDB: true wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true net: port: 38024 replication: oplogSizeMB: 2048 replSetName: sh2 sharding: clusterRole: shardsvr processManagement: fork: true EOF cp /mongodb/38024/conf/mongodb.conf /mongodb/38025/conf/ cp /mongodb/38024/conf/mongodb.conf /mongodb/38026/conf/ sed 's#38024#38025#g' /mongodb/38025/conf/mongodb.conf -i sed 's#38024#38026#g' /mongodb/38026/conf/mongodb.conf -i
-
啟動(dòng)所有節(jié)點(diǎn)
mongod -f /mongodb/38021/conf/mongodb.conf mongod -f /mongodb/38022/conf/mongodb.conf mongod -f /mongodb/38023/conf/mongodb.conf mongod -f /mongodb/38024/conf/mongodb.conf mongod -f /mongodb/38025/conf/mongodb.conf mongod -f /mongodb/38026/conf/mongodb.conf
-
搭建復(fù)制集:
mongo --port 38021 use admin config = {_id: 'sh1', members: [ {_id: 0, host: '10.0.0.200:38021'}, {_id: 1, host: '10.0.0.200:38022'}, {_id: 2, host: '10.0.0.200:38023',"arbiterOnly":true}] } rs.initiate(config) mongo --port 38024 use admin config = {_id: 'sh2', members: [ {_id: 0, host: '10.0.0.200:38024'}, {_id: 1, host: '10.0.0.200:38025'}, {_id: 2, host: '10.0.0.200:38026',"arbiterOnly":true}] } rs.initiate(config)
2. config節(jié)點(diǎn)配置:
-
目錄創(chuàng)建:
mkdir -p /mongodb/38018/conf /mongodb/38018/log /mongodb/38018/data mkdir -p /mongodb/38019/conf /mongodb/38019/log /mongodb/38019/data mkdir -p /mongodb/38020/conf /mongodb/38020/log /mongodb/38020/data
-
修改配置文件:
cat >> /mongodb/38018/conf/mongodb.conf << EOF systemLog: destination: file path: /mongodb/38018/log/mongodb.conf logAppend: true storage: journal: enabled: true dbPath: /mongodb/38018/data directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true net: port: 38018 replication: oplogSizeMB: 2048 replSetName: configReplSet sharding: clusterRole: configsvr processManagement: fork: true EOF cp /mongodb/38018/conf/mongodb.conf /mongodb/38019/conf/ cp /mongodb/38018/conf/mongodb.conf /mongodb/38020/conf/ sed 's#38018#38019#g' /mongodb/38019/conf/mongodb.conf -i sed 's#38018#38020#g' /mongodb/38020/conf/mongodb.conf -i
-
啟動(dòng)節(jié)點(diǎn)困曙,并配置復(fù)制集
mongod -f /mongodb/38018/conf/mongodb.conf mongod -f /mongodb/38019/conf/mongodb.conf mongod -f /mongodb/38020/conf/mongodb.conf mongo --port 38018 use admin config = {_id: 'configReplSet', members: [ {_id: 0, host: '10.0.0.200:38018'}, {_id: 1, host: '10.0.0.200:38019'}, {_id: 2, host: '10.0.0.200:38020'}] } rs.initiate(config) 注:configserver 可以是一個(gè)節(jié)點(diǎn),官方建議復(fù)制集。 configserver不能有arbiter悯仙。 新版本中,要求必須是復(fù)制集。 注:mongodb 3.4之后荒勇,雖然要求config server為replica set题画,但是不支持arbiter
3. mongos節(jié)點(diǎn)配置:
-
創(chuàng)建目錄:
mkdir -p /mongodb/38017/conf /mongodb/38017/log
-
配置文件:
cat >> /mongodb/38017/conf/mongos.conf <<EOF systemLog: destination: file path: /mongodb/38017/log/mongos.log logAppend: true net: port: 38017 sharding: configDB: configReplSet/10.0.0.200:38018,10.0.0.200:38019,10.0.0.200:38020 processManagement: fork: true EOF
-
啟動(dòng)mongos
mongos -f /mongodb/38017/conf/mongos.conf
三. 分片集群操作:
連接到其中一個(gè)mongos(10.0.0.200),做以下配置
-
連接到mongs的admin數(shù)據(jù)庫(kù)
# su - mongod $ mongo 10.0.0.200:38017/admin
-
添加分片
db.runCommand( { addshard : "sh1/10.0.0.200:38021,10.0.0.200:38022,10.0.0.200:38023",name:"shard1"} ) db.runCommand( { addshard : "sh2/10.0.0.200:38024,10.0.0.200:38025,10.0.0.200:38026",name:"shard2"} )
-
列出分片
db.runCommand( { listshards : 1 } )
-
整體狀態(tài)查看
sh.status();
四. 使用分片集群
1. RANGE分片配置及測(cè)試( 用的不多, 主要用hash分片 )
test庫(kù)下的vast大表進(jìn)行手工分片
- 激活數(shù)據(jù)庫(kù)分片功能
mongo --port 38017 admin
admin> ( { enablesharding : "數(shù)據(jù)庫(kù)名稱" } )
eg:
admin> db.runCommand( { enablesharding : "test" } )
- 指定分片建對(duì)集合分片
eg:范圍片鍵
--創(chuàng)建索引
use test
> db.vast.ensureIndex( { id: 1 } )
--開(kāi)啟分片
use admin
> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
- 集合分片驗(yàn)證
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()
- 分片結(jié)果測(cè)試
shard1:
mongo --port 38021
db.vast.count();
shard2:
mongo --port 38024
db.vast.count();
2. Hash分片例子:(主要用這種)
對(duì)oldguo庫(kù)下的vast大表進(jìn)行hash
創(chuàng)建哈希索引
-
對(duì)于oldguo開(kāi)啟分片功能
mongo --port 38017 admin use admin admin> db.runCommand( { enablesharding : "oldguo" } )
-
對(duì)于oldguo庫(kù)下的vast表建立hash索引
use oldguo oldguo> db.vast.ensureIndex( { id: "hashed" } )
-
開(kāi)啟分片
use admin admin > sh.shardCollection( "oldguo.vast", { id: "hashed" } )
-
錄入10w行數(shù)據(jù)測(cè)試
use oldguo for(i=1;i<=100000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
-
hash分片結(jié)果測(cè)試
mongo --port 38021 use oldguo db.vast.count(); mongo --port 38024 use oldguo db.vast.count();
3. 判斷是否Shard集群
admin> db.runCommand({ isdbgrid : 1})
4. 列出所有分片信息
admin> db.runCommand({ listshards : 1})
5. 列出開(kāi)啟分片的數(shù)據(jù)庫(kù)
use config
config> db.databases.find( { "partitioned": true } )
或者:
config> db.databases.find() //列出所有數(shù)據(jù)庫(kù)分片情況
6. 查看分片的片鍵
db.collections.find().pretty()
7. 查看分片的詳細(xì)信息
admin> db.printShardingStatus()
或
admin> sh.status()
8. 刪除分片節(jié)點(diǎn)(謹(jǐn)慎)
(1)確認(rèn)blance是否在工作
sh.getBalancerState()
(2)刪除shard2節(jié)點(diǎn)(謹(jǐn)慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:刪除操作一定會(huì)立即觸發(fā)blancer货岭。
9. balancer操作
介紹:
mongos的一個(gè)重要功能,自動(dòng)巡查所有shard節(jié)點(diǎn)上的chunk的情況疾渴,自動(dòng)做chunk遷移千贯。
什么時(shí)候工作?
1搞坝、自動(dòng)運(yùn)行搔谴,會(huì)檢測(cè)系統(tǒng)不繁忙的時(shí)候做遷移
2、在做節(jié)點(diǎn)刪除的時(shí)候桩撮,立即開(kāi)始遷移工作
3敦第、balancer只能在預(yù)設(shè)定的時(shí)間窗口內(nèi)運(yùn)行
有需要時(shí)可以關(guān)閉和開(kāi)啟blancer(備份的時(shí)候)
mongos> sh.stopBalancer()
mongos> sh.startBalancer()
10. 自定義 自動(dòng)平衡進(jìn)行的時(shí)間段
https://docs.mongodb.com/manual/tutorial/manage-sharded-cluster-balancer/#schedule-the-balancing-window
// 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()
五. 關(guān)于集合的balance(了解下)
關(guān)閉某個(gè)集合的balance
sh.disableBalancing("students.grades")
打開(kāi)某個(gè)集合的balance
sh.enableBalancing("students.grades")
確定某個(gè)集合的balance是開(kāi)啟或者關(guān)閉
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;