前言
數(shù)據(jù)量大了或者并發(fā)量上來了肥照,單機(jī)肯定是抗不住的,這個(gè)時(shí)候要開始考慮使用集群了勤众。mongodb目前為止支持三種集群模式:主從集群舆绎,副本集集群,分片集群们颜。
主從集群
特性
1. 一主多從
2. 主負(fù)責(zé)讀寫
3. 從負(fù)責(zé)讀
4. 從通過異步同步主op日志同步主數(shù)據(jù)
5. 主掛無法自動(dòng)恢復(fù)
架構(gòu)圖
實(shí)踐
規(guī)劃
一主兩從
ip端口配置
主節(jié)點(diǎn):127.0.0.1:27021
從節(jié)點(diǎn)1:127.0.0.1:27022
從節(jié)點(diǎn)2:127.0.0.1:27023
啟動(dòng)主節(jié)點(diǎn)
su -s /bin/bash -c "/usr/bin/mongod --master -f /etc/mongodb/27021.conf" mongod
啟動(dòng)從節(jié)點(diǎn)
su -s /bin/bash -c "/usr/bin/mongod --slave --source 127.0.0.1:27021 -f /etc/mongodb/27022.conf" mongod
su -s /bin/bash -c "/usr/bin/mongod --slave --source 127.0.0.1:27021 -f /etc/mongodb/27023.conf" mongod
設(shè)置從節(jié)點(diǎn)可用
use admin
rs.slaveOk()
查看是否主節(jié)點(diǎn)
use admin
rs.isMaster()
查看主節(jié)點(diǎn)復(fù)制狀態(tài)信息
use admin
rs.printReplicationInfo()
查看從節(jié)點(diǎn)復(fù)制狀態(tài)信息
use admin
rs.printSlaveReplicationInfo()
replica set集群
特性
- n個(gè)不同類型節(jié)點(diǎn)組成
- 每個(gè)節(jié)點(diǎn)數(shù)據(jù)相同
- 建議至少有一個(gè)primary和兩個(gè)secondary節(jié)點(diǎn)
- 集群中只能有一個(gè)primary節(jié)點(diǎn)
- 寫請求都通過primary節(jié)點(diǎn)
- 支持自動(dòng)故障恢復(fù)
- primary節(jié)點(diǎn)不可用時(shí)吕朵,通過投票選舉從secondary節(jié)點(diǎn)列表中選出primary節(jié)點(diǎn)猎醇,因此最好節(jié)點(diǎn)數(shù)量是奇數(shù)
- secondary節(jié)點(diǎn)從primary節(jié)點(diǎn)通過oplog異步方式同步數(shù)據(jù) 節(jié)點(diǎn)類型
primary
主節(jié)點(diǎn),負(fù)責(zé)集群數(shù)據(jù)維護(hù)努溃,所有數(shù)據(jù)更新操作都通過主節(jié)點(diǎn)
**secondary **
從節(jié)點(diǎn)硫嘶,從主節(jié)點(diǎn)復(fù)制數(shù)據(jù),提供讀請求響應(yīng)
arbiter
選舉節(jié)點(diǎn)梧税,不保存數(shù)據(jù)沦疾,只參與primary投票選舉
架構(gòu)圖
節(jié)點(diǎn)屬性說明
priority: 0 表示不是候選人,可以投票
hidden:true 對用戶端不可見第队,不可見的節(jié)點(diǎn)priority必須設(shè)置為0
votes: 1 投票權(quán)限 0表示不能投票
實(shí)踐
規(guī)劃
搭建3個(gè)節(jié)點(diǎn)的集群哮塞,一個(gè)primary,兩個(gè)secondary
ip和端口配置
host1: 127.0.0.1:27018
host2: 127.0.0.1:27019
host3: 127.0.0.1:27020
集群名: replSetTest0
啟動(dòng)3個(gè)事例
單個(gè)實(shí)例的配置
#系統(tǒng)日志配置
systemLog:
destination: file
path: /var/log/mongodb/mongod_27018.log
logAppend: true
#quiet模式運(yùn)行凳谦,建議設(shè)置為false忆畅,方便排查錯(cuò)誤
quiet: false
#進(jìn)程管理
processManagement:
#進(jìn)程后臺(tái)運(yùn)行
fork: true
#進(jìn)程pid文件
pidFilePath: /var/log/mongodb/mongod_27018.pid
#網(wǎng)絡(luò)配置
net:
#監(jiān)聽端口
port: 27018
#監(jiān)聽網(wǎng)卡 多個(gè)使用英文逗號(hào)隔開
bindIp: 127.0.0.1
#最大并發(fā)連接數(shù) 默認(rèn)65535
maxIncomingConnections: 65535
#驗(yàn)證客戶端傳過來的數(shù)據(jù),文檔嵌套多時(shí)尸执,對性能會(huì)有些影響
wireObjectCheck: true
#是否啟用ipv6家凯,默認(rèn)不啟用
ipv6: false
unixDomainSocket:
#是否啟用socket監(jiān)聽 默認(rèn)true
enabled: true
#socket保存目錄,默認(rèn)/tmp
pathPrefix: /var/log/mongodb
#socket文件權(quán)限如失,默認(rèn)0700
filePermissions: 0700
http:
#是否啟用http服務(wù)绊诲,默認(rèn)false,安全考慮線上環(huán)境要關(guān)閉
enabled: false
#是否啟用http jsonp褪贵,默認(rèn)false驯镊,即使http.enabled為false,只要此項(xiàng)為true竭鞍,一樣可以訪問板惑,安全考慮線上環(huán)境要關(guān)閉
JSONPEnabled: false
#是否啟用rest api接口,默認(rèn)false偎快,安全考慮線上環(huán)境要關(guān)閉
RESTInterfaceEnabled: false
compression:
#是否啟用數(shù)據(jù)壓縮
compressors: snappy
#安全配置
security:
#type:string
#密鑰路徑冯乘,副本集和分片集群節(jié)點(diǎn)間授權(quán)時(shí)使用的密鑰
keyFile: /var/log/mongodb/.replSetTest0Key
#type:string
#集群授權(quán)模式,默認(rèn)keyFile晒夹,值列表:keyFile,sendKeyFile,sendX509,x509
clusterAuthMode: keyFile
#type:string
#是否開啟數(shù)據(jù)庫訪問RBAC權(quán)限控制裆馒,默認(rèn):disabled,僅對mongod命令有效
authorization: enabled
#type:boolean
#是否開啟服端js執(zhí)行丐怯,默認(rèn)true喷好,如果未開啟$where,group,mapreduce都不能使用
javascriptEnabled: true
#存儲(chǔ)配置
storage:
#type:string
#數(shù)據(jù)庫數(shù)據(jù)存儲(chǔ)目錄,默認(rèn)/data/db
dbPath: /data/mongodb/27018
#type:boolean
#啟動(dòng)時(shí)是否嘗試重建索引读跷,默認(rèn)true
indexBuildRetry: true
#journal日志
journal:
#type:boolean
#Enable or disable the durability journal to ensure data files remain valid and recoverable.
enabled: true
#type:int
#日志同步間隔梗搅,Values can range from 1 to 500 milliseconds.
commitIntervalMs: 100
#type:boolean
#是否開啟一數(shù)據(jù)庫一目錄,默認(rèn)是false
directoryPerDB: false
#type:int
#數(shù)據(jù)落地時(shí)間間隔,默認(rèn)為60秒无切,不能設(shè)置為0荡短,一般使用默認(rèn)值即可
syncPeriodSecs: 60
#type:string
##存儲(chǔ)引擎,默認(rèn)wiredTiger哆键,可選值 mmapv1,wiredTiger,inMemory
engine: wiredTiger
wiredTiger:
engineConfig:
#type:float
#單個(gè)實(shí)例可用的數(shù)據(jù)緩存內(nèi)存大小掘托,version >= 3.4默認(rèn):50% of RAM minus 1 GB, or 256 MB.
cacheSizeGB: 0.25
#type:string
#WiredTiger journal數(shù)據(jù)壓縮格式,默認(rèn)snappy籍嘹,可用的壓縮類型: none, snappy, zlib
journalCompressor: snappy
#type:boolean
#索引文件分目錄存儲(chǔ)碾褂,默認(rèn)false宴猾,version >= 3.0后版本可用
directoryForIndexes: false
collectionConfig:
#type:string
#塊數(shù)據(jù)壓縮格式贞瞒,默認(rèn)snappy笼痹,可用的壓縮類型:none, snappy, zlib
blockCompressor: snappy
indexConfig:
#type:boolean
#是否開啟索引prefix compression,默認(rèn)true
prefixCompression: true #是否開啟索引prefix compression识补,默認(rèn)true
#operationProfiling操作性能分析
operationProfiling:
#type:int
#慢查詢時(shí)間單位毫秒族淮,默認(rèn)100辫红,如果開啟了profile凭涂,日志會(huì)保存到system.profile集合中
slowOpThresholdMs: 100
#type:string
#性能分析模式,開啟會(huì)影響性能贴妻,謹(jǐn)慎操作切油。默認(rèn)off.
#可選值1:off: Off. No profiling.
#可選值2: slowOp:On. Only includes slow operations.
#可選值3: all:On. Includes all operations.
mode: slowOp
#replication復(fù)制配置
replication:
#type:int
#數(shù)字類型(單位M) replication op log 大小,64位系統(tǒng)默認(rèn)為可用磁盤的5%
oplogSizeMB: 512
#type:string
#所屬replica set集群名稱
replSetName: replSetTest0
#setParameter配置
setParameter:
enableLocalhostAuthBypass: false
啟動(dòng)
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27018.conf" mongod
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27019.conf" mongod
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27020.conf" mongod
mongod -f /etc/mongodb/27018.conf --shutdown
集群初始化
mongo --port 27018
rs.initiate(
{
_id: "replSetTest0",
version: 1,
members: [
{ _id: 0, host : "127.0.0.1:27018" },
{ _id: 1, host : "127.0.0.1:27019" },
{ _id: 2, host : "127.0.0.1:27020" }
]
}
)
設(shè)置從節(jié)點(diǎn)可用
mongo --port 27018(填寫實(shí)際的從節(jié)點(diǎn)地址)
rs.slaveOk()
查看集群狀態(tài)
mongo --port 27018
rs.status()
查看是否primary節(jié)點(diǎn)
mongo --port 27018
rs.isMaster()
查看集群配置
mongo --port 27018
rs.conf()
添加節(jié)點(diǎn)
mongo 主節(jié)點(diǎn)地址
rs.add({} | host地址)
刪除節(jié)點(diǎn)
mongo 主節(jié)點(diǎn)地址
刪除前名惩,建議先停止這個(gè)節(jié)點(diǎn)的服務(wù)
rs.remove(hostname)
更改集群配置
mongo 主節(jié)點(diǎn)地址
rs.reconfig({},{})
分片集群
選定一個(gè)或多個(gè)key澎胡,按照選定的key進(jìn)行數(shù)據(jù)分割,分割后的數(shù)據(jù)分別保存在不同的mongodb副本集中娩鹉,這個(gè)是分片的基本思路攻谁。
分片思路可以水平擴(kuò)展機(jī)器,根據(jù)業(yè)務(wù)需要擴(kuò)展任意多的機(jī)器弯予。讀寫壓力分散到集群不同的機(jī)器中戚宦。
架構(gòu)圖
組件
配置server
副本集集群,分片信息锈嫩,用戶授權(quán)信息受楼,配置信息等都保存在這里
分片server
單個(gè)獨(dú)立的mongod實(shí)例或者副本集集群,存放真實(shí)的數(shù)據(jù)
路由server
處理客戶端連接請求呼寸,路由到存放數(shù)據(jù)的分片server艳汽,合并分片server的數(shù)據(jù),返回給客戶端
分片策略
支持類型
哈希分片
對分片key使用某種哈希算法对雪,根據(jù)哈希值確定數(shù)據(jù)插入哪個(gè)分片server中
特點(diǎn)
數(shù)據(jù)離散性好河狐,能均勻分布在不同的分片server中
不適合根據(jù)范圍查詢的情況
范圍分片
對分片key的值通過范圍確定存儲(chǔ)的分片server
特點(diǎn)
數(shù)據(jù)離散性可能不好,可能會(huì)造成熱點(diǎn)數(shù)據(jù)在某個(gè)分片server中
適合范圍查詢的情況
限制
分片key確定后不能更改
分片key必須設(shè)置索引,如果不是甚牲,設(shè)置分片key時(shí)义郑,mongod會(huì)自動(dòng)創(chuàng)建
原則
1. 選擇值多樣性的key,盡可能分散丈钙,避免數(shù)據(jù)文檔集中到某些分片server中
2. 考慮以后是否會(huì)數(shù)據(jù)分組非驮,數(shù)據(jù)分組時(shí)分組key需要是分片key或者分片組合key的前綴
3. 選擇合適的分片類型,不同的分片類型適用的場景不一樣
4. 當(dāng)必須選擇值重復(fù)頻率高的key時(shí)雏赦,可以考慮選擇組合key
實(shí)踐
規(guī)劃
考慮到只是實(shí)踐使用劫笙,配置server副本集只用一臺(tái),分片server副本集也只用一臺(tái)
1臺(tái)配置server: 127.0.0.1:27024(副本集集群星岗,集群名:shardClusterCfgServerReplSet0)
1臺(tái)路由server: 127.0.0.1:27025
2臺(tái)分片server:127.0.0.1:27026(副本集集群填大,集群名:shardClusterShardServerReplSet0),127.0.0.1:27027(副本集集群俏橘,集群名:shardClusterShardServerReplSet1)
啟動(dòng)配置server
sharding:
clusterRole: configsvr
replication:
replSetName: shardClusterCfgServerReplSet0
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27024.conf" mongod
rs.initiate(
{
_id: "shardClusterCfgServerReplSet0",
configsvr: true,
members: [
{ _id : 0, host : "127.0.0.1:27024" }
]
}
)
啟動(dòng)分片server
sharding:
clusterRole: shardsvr
replication:
replSetName: shardClusterShardServerReplSet0
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27026.conf" mongod
su -s /bin/bash -c "/usr/bin/mongod -f /etc/mongodb/27027.conf" mongod
rs.initiate(
{
_id: "shardClusterShardServerReplSet1",
members: [
{ _id : 0, host : "127.0.0.1:27027" }
]
}
)
啟動(dòng)路由server
su -s /bin/bash -c "/usr/bin/mongos -f /etc/mongodb/27025.conf" mongod
增加或者移除分片最好停服操作允华,減少數(shù)據(jù)不一致的可能性
使用配置server用戶登錄mongos進(jìn)行操作
#系統(tǒng)日志配置
systemLog:
destination: file
path: /var/log/mongodb/mongod_27025.log
logAppend: true
#quiet模式運(yùn)行,建議設(shè)置為false寥掐,方便排查錯(cuò)誤
quiet: false
#進(jìn)程管理
processManagement:
#進(jìn)程后臺(tái)運(yùn)行
fork: true
#進(jìn)程pid文件
pidFilePath: /var/log/mongodb/mongod_27025.pid
#網(wǎng)絡(luò)配置
net:
#監(jiān)聽端口
port: 27025
#監(jiān)聽網(wǎng)卡 多個(gè)使用英文逗號(hào)隔開
bindIp: 127.0.0.1
#最大并發(fā)連接數(shù) 默認(rèn)65535
maxIncomingConnections: 65535
#驗(yàn)證客戶端傳過來的數(shù)據(jù)靴寂,文檔嵌套多時(shí),對性能會(huì)有些影響
wireObjectCheck: true
#是否啟用ipv6召耘,默認(rèn)不啟用
ipv6: false
unixDomainSocket:
#是否啟用socket監(jiān)聽 默認(rèn)true
enabled: true
#socket保存目錄百炬,默認(rèn)/tmp
pathPrefix: /var/log/mongodb
#socket文件權(quán)限,默認(rèn)0700
filePermissions: 0700
http:
#是否啟用http服務(wù)污它,默認(rèn)false剖踊,安全考慮線上環(huán)境要關(guān)閉
enabled: false
compression:
#是否啟用數(shù)據(jù)壓縮
compressors: snappy
#安全配置
security:
#type:string
#密鑰路徑,副本集和分片集群節(jié)點(diǎn)間授權(quán)時(shí)使用的密鑰
keyFile: /var/log/mongodb/.replSetTest0Key
#type:string
#集群授權(quán)模式衫贬,默認(rèn)keyFile德澈,值列表:keyFile,sendKeyFile,sendX509,x509
clusterAuthMode: keyFile
#setParameter配置
setParameter:
enableLocalhostAuthBypass: false
#分片配置
sharding:
#type:string
#配置server,多個(gè)使用英文逗號(hào)分開
configDB: shardClusterCfgServerReplSet0/127.0.0.1:27024
添加分片server
添加分片操作會(huì)觸發(fā)數(shù)據(jù)遷移固惯,遷移過程中對數(shù)據(jù)庫性能會(huì)有些影響
sh.addShard("shardClusterShardServerReplSet0/127.0.0.1:27026")
sh.addShard("shardClusterShardServerReplSet1/127.0.0.1:27027")
開啟數(shù)據(jù)庫分片
sh.enableSharding("test")
設(shè)置集合分片key
sh.shardCollection("test.people",{_id:1})
移除分片server
移除分片server前需要先遷移分區(qū)數(shù)據(jù)到其它分片梆造,為了保證整個(gè)集群性能,遷移過程中會(huì)占用比較小的資源缝呕,網(wǎng)絡(luò)帶寬和數(shù)據(jù)量大小會(huì)影響遷移時(shí)間澳窑,
可能需要幾分鐘到幾天不等
移除步驟
step1 確定遷移進(jìn)程開啟
sh.getBalancerState()
返回true表示開啟
step2 確定需要?jiǎng)h除分片的名字
db.adminCommand( { listShards : 1 } )
_id字段值為分片名稱
step3 遷移分片數(shù)據(jù)到其它分片
db.adminCommand({removeShard:"shardClusterShardServerReplSet1"})
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shardClusterShardServerReplSet1",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"test"
],
"ok" : 1
}
step4 查看遷移狀態(tài)
db.adminCommand({removeShard:"shardClusterShardServerReplSet1"})
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(0),
"dbs" : NumberLong(1)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"test"
],
"ok" : 1
}
step5 遷移未分片的數(shù)據(jù)
分片數(shù)據(jù)遷移完成后執(zhí)行
返回的狀態(tài)列表字段remaining.chunks為0時(shí)表示遷移完成
如果沒有未分片的數(shù)據(jù)庫,不需要執(zhí)行這個(gè)步驟
返回的狀態(tài)列表有一個(gè)字段dbsToMove供常,這個(gè)字段的內(nèi)容為需要遷移的未分片的數(shù)據(jù)庫摊聋,不為空時(shí)需要執(zhí)行遷移操作
use admin
db.runCommand({movePrimary:"test", to:"shardClusterShardServerReplSet0"})
db.runCommand({flushRouterConfig:1})
step6 數(shù)據(jù)遷移完成確認(rèn)
前面步驟都執(zhí)行完成后執(zhí)行
db.adminCommand({removeShard:"shardClusterShardServerReplSet1"})
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shardClusterShardServerReplSet1",
"ok" : 1
}
集群相關(guān)操作
連接mongos
mongo --host mongos_host --port mongos_port
查看狀態(tài)
sh.status()
顯示開啟分區(qū)的數(shù)據(jù)庫
use config
db.databases.find({partitioned:true})
顯示分片server列表
db.adminCommand( { listShards : 1 } )
查看數(shù)據(jù)遷移進(jìn)程狀態(tài)
sh.getBalancerState()
備注
- 第一次初始化時(shí)禁用權(quán)限判斷,添加好用戶之后再開啟
- 增刪改只能在主節(jié)點(diǎn)操作
- 集群使用的密碼不能超過1K且只能是base64編碼字符集
- keyFile文件權(quán)限不能開放給所在組成員和其他組成員
- 生成隨機(jī)密碼:openssl rand -base64 741
參考資料
【1】Replication
https://docs.mongodb.com/manual/replication/
【2】Read Preference
https://docs.mongodb.com/manual/core/read-preference/
【3】Replica Set Members
https://docs.mongodb.com/manual/core/replica-set-members/
【4】Replica Set Elections
https://docs.mongodb.com/manual/core/replica-set-elections/
【5】Replica Set Data Synchronization
https://docs.mongodb.com/manual/core/replica-set-sync/
【6】Replica Set Oplog
https://docs.mongodb.com/manual/core/replica-set-oplog/
【7】Replica Set Deployment Architectures
https://docs.mongodb.com/manual/core/replica-set-architectures/
【8】Mongo Shell - Replication Methods
https://docs.mongodb.com/manual/reference/method/js-replication/
【9】Deploy a Replica Set
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
【10】Replica Set Configuration
https://docs.mongodb.com/manual/reference/replica-configuration/
【11】Rollbacks During Replica Set Failover
https://docs.mongodb.com/manual/core/replica-set-rollbacks/
【12】Change the Size of the Oplog
https://docs.mongodb.com/manual/tutorial/change-oplog-size/
【13】Resync a Member of a Replica Set
https://docs.mongodb.com/manual/tutorial/resync-replica-set-member/
【14】Replication Reference
https://docs.mongodb.com/manual/reference/replication/
【15】Write Concern
https://docs.mongodb.com/manual/reference/write-concern/
【16】Read Isolation, Consistency, and Recency
https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/
【17】Master Slave Replication
https://docs.mongodb.com/manual/core/master-slave/
【18】分片集群
https://docs.mongodb.com/manual/sharding/
【19】分片-區(qū)域
https://docs.mongodb.com/manual/core/zone-sharding/
【20】分片-參考
https://docs.mongodb.com/manual/reference/sharding/
【21】分片-管理
https://docs.mongodb.com/manual/administration/sharded-cluster-administration/
【22】分片-均衡
https://docs.mongodb.com/manual/core/sharding-balancer-administration/
【23】分片-組件
https://docs.mongodb.com/manual/core/sharded-cluster-components/