一.分片簡介
1.為什么需要Sharded cluster筛圆?
MongoDB目前3大核心優(yōu)勢:『靈活模式』+ 『高可用性』 + 『可擴(kuò)展性』卡骂,通過json文檔來實(shí)現(xiàn)靈活模式崔慧,通過復(fù)制集來保證高可用狠半,通過Sharded cluster來保證可擴(kuò)展性宰翅。
2.有了副本集一铅,為什么還需要分片?
副本集資源利用率不高
分片可以提高資源利用率
3.分片的缺點(diǎn)
理想情況下需要機(jī)器比較多
配置和運(yùn)維變得復(fù)雜且困難
提前規(guī)劃好特別重要堕油,一旦建立后在想改變架構(gòu)變得很困難
二.分片的工作原理
Mongos是Sharded cluster的訪問入口潘飘,
Mongos本身并不持久化數(shù)據(jù),Sharded cluster所有的元數(shù)據(jù)都會存儲到Config Server
而用戶的數(shù)據(jù)則會分散存儲到各個shard掉缺。Mongos啟動后卜录,會從config server加載元數(shù)據(jù),開始提供服務(wù)眶明,將用戶的請求正確路由到對應(yīng)的Shard艰毒。
1.路由服務(wù)mongos
路由服務(wù),提供代理搜囱,替用戶去向后請求shard分片的數(shù)據(jù)
即使數(shù)據(jù)已經(jīng)被分散存儲到不同的物理服務(wù)器中丑瞧,但對程序而已,數(shù)據(jù)還是完整的蜀肘,并未被拆分绊汹。
數(shù)據(jù)存儲過程中相關(guān)復(fù)雜工作,mongos都幫你完成了扮宠。
2.分片配置信息服務(wù)器config
保存說明數(shù)據(jù)結(jié)構(gòu)的元數(shù)據(jù)
保存所有shard的配置信息
提供給mongos查詢服務(wù)
3.數(shù)據(jù)節(jié)點(diǎn)shard
負(fù)責(zé)處理數(shù)據(jù)的節(jié)點(diǎn)西乖,每個shard都是分片集群的一部分
每一個分片都是一個完整的副本集。
三.數(shù)據(jù)分布策略(片鍵 shard key )
分片支持單個集合的數(shù)據(jù)分散在多數(shù)據(jù)存放到哪個shard的區(qū)分規(guī)則
片鍵可以是一個或多個字段的組合
片鍵就是集合的索引
選擇片鍵的依據(jù):
能夠被經(jīng)常訪問到的字段
索引字段基數(shù)夠大個分片上。
目前主要有兩種數(shù)據(jù)分片的策略:
區(qū)間片鍵(Range based sharding)
hash片鍵(Hash based sharding)
區(qū)間片鍵
區(qū)間片鍵適合滿足在一定范圍內(nèi)的查找获雕,例如查找X的值在【100-200】之間的數(shù)據(jù)薄腻,mongo 路由根據(jù)Config server中存儲的元數(shù)據(jù),可以直接定位到指定的shard的Chunk中
缺點(diǎn) 如果shardkey有明顯遞增(或者遞減)趨勢届案,則新插入的文檔多會分布到同一個chunk庵楷,無法擴(kuò)展寫的能力
Hash片鍵
Hash片鍵是根據(jù)用戶的shard key計算hash值(64bit整型),根據(jù)hash值按照『范圍分片』的策略將文檔分布到不同的chunk
優(yōu)點(diǎn)Hash分片與范圍分片互補(bǔ)楣颠,能將文檔隨機(jī)的分散到各個chunk尽纽,充分的擴(kuò)展寫能力,彌補(bǔ)了范圍分片的不足球碉,
缺點(diǎn)但不能高效的服務(wù)范圍查詢蜓斧,所有的范圍查詢要分發(fā)到后端所有的Shard才能找出滿足條件的文檔仓蛆。
合理的選擇shard key
選擇shard key時睁冬,要根據(jù)業(yè)務(wù)的需求及『范圍分片』和『Hash分片』2種方式的優(yōu)缺點(diǎn)合理選擇,要根據(jù)字段的實(shí)際原因?qū)?shù)據(jù)進(jìn)行分片看疙,否則會產(chǎn)生過大的Chunk
四.常用操作說明
Mongos
Mongos作為Sharded cluster的訪問入口豆拨,所有的請求都由mongos來路由、分發(fā)能庆、合并施禾,這些動作對客戶端driver透明,用戶連接mongos就像連接mongod一樣使用搁胆。
查詢請求
查詢請求不包含shard key弥搞,則必須將查詢分發(fā)到所有的shard,然后合并查詢結(jié)果返回給客戶端
查詢請求包含shard key渠旁,則直接根據(jù)shard key計算出需要查詢的chunk攀例,向?qū)?yīng)的shard發(fā)送查詢請求
寫請求
寫操作必須包含shard key,mongos根據(jù)shard key算出文檔應(yīng)該存儲到哪個chunk顾腊,然后將寫請求發(fā)送到chunk所在的shard粤铭。
更新/刪除請求
更新、刪除請求的查詢條件必須包含shard key或者_(dá)id杂靶,如果是包含shard key梆惯,則直接路由到指定的chunk,如果只包含_id吗垮,則需將請求發(fā)送至所有的shard垛吗。
Config Server
config database
Config server存儲Sharded cluster的所有元數(shù)據(jù),所有的元數(shù)據(jù)都存儲在config數(shù)據(jù)庫
Config Server可部署為一個獨(dú)立的復(fù)制集烁登,極大的方便了Sharded cluster的運(yùn)維管理职烧。
config.shards
config.shards集合存儲各個Shard的信息,可通過addShard、removeShard命令來動態(tài)的從Sharded cluster里增加或移除shard
config.databases
config.databases集合存儲所有數(shù)據(jù)庫的信息蚀之,包括DB是否開啟分片蝗敢,primary shard信息,對于數(shù)據(jù)庫內(nèi)沒有開啟分片的集合足删,所有的數(shù)據(jù)都會存儲在數(shù)據(jù)庫的primary shard上寿谴。
config.colletions
數(shù)據(jù)分片是針對集合維度的,某個數(shù)據(jù)庫開啟分片功能后失受,如果需要讓其中的集合分片存儲讶泰,則需調(diào)用shardCollection命令來針對集合開啟分片。
config.chunks
集合分片開啟后拂到,默認(rèn)會創(chuàng)建一個新的chunk痪署,shard key取值[minKey, maxKey]內(nèi)的文檔(即所有的文檔)都會存儲到這個chunk。當(dāng)使用Hash分片策略時兄旬,也可以預(yù)先創(chuàng)建多個chunk狼犯,以減少chunk的遷移。
config.settings
config.settings集合里主要存儲sharded cluster的配置信息领铐,比如chunk size悯森,是否開啟balancer等
其他集合
config.tags主要存儲sharding cluster標(biāo)簽(tag)相關(guān)的信息
config.changelog主要存儲sharding cluster里的所有變更操作,比如balancer遷移chunk的動作就會記錄到changelog里
config.mongos存儲當(dāng)前集群所有mongos的信息
config.locks存儲鎖相關(guān)的信息绪撵,對某個集合進(jìn)行操作時瓢姻,比如moveChunk,需要先獲取鎖音诈,避免多個mongos同時遷移同一個集合的chunk幻碱。
四.Sharded cluster部署
1.部署步驟
1.先搭建副本集
2.搭建config副本集
3.搭建mongos副本集
4.數(shù)據(jù)庫啟用分片功能
5.集合設(shè)置片鍵
6.插入測試數(shù)據(jù)
7.檢查是否分片
8.安裝圖形化工具
2. IP端口目錄規(guī)劃
1.IP端口規(guī)劃
mongodb01
10.0.0.73
Shard1_Master 28100
Shard2_Slave 28200
Shard3_Arbiter 28300
Config Server 40000
mongos Server 60000
mongodb02
10.0.0.74
Shard2_Master 28100
Shard3_Slave 28200
Shard1_Arbiter 28300
Config Server 40000
mongos Server 60000
mongodb03
10.0.0.75
Shard3_Master 28100
Shard1_Slave 28200
Shard2_Arbiter 28300
Config Server 40000
mongos Server 60000
2.目錄規(guī)劃
服務(wù)目錄:
/opt/master/{conf,log,pid}
/opt/slave//{conf,log,pid}
/opt/arbiter/{conf,log,pid}
/data/config/{conf,log,pid}
/data/mongos/{conf,log,pid}
數(shù)據(jù)目錄:
/data/master/
/data/slave/
/data/arbiter/
/data/config/
3.分片集群搭建副本集
1.安裝軟件
注意:三臺服務(wù)器都操作!O附Α褥傍!
tar xf mongodb-linux-x86_64-rhel70-4.0.14.tgz -C /opt/
ln -s /opt/mongodb-linux-x86_64-rhel70-4.0.14 /opt/mongodb
echo 'export PATH=$PATH:/opt/mongodb/bin' >> /etc/profile
source /etc/profile
2.創(chuàng)建目錄
注意:三臺服務(wù)器都操作!Z诵帧摔桦!
mkdir -p /opt/master/{conf,log,pid}
mkdir -p /opt/slave/{conf,log,pid}
mkdir -p /opt/arbiter/{conf,log,pid}
mkdir -p /data/master/
mkdir -p /data/slave/
mkdir -p /data/arbiter/
3.mongodb01創(chuàng)建配置文件
### master節(jié)點(diǎn)配置文件
cat >/opt/master/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/master/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/master/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/master/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28100
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
EOF
### slave節(jié)點(diǎn)配置文件
cat >/opt/slave/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/slave/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/slave/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/slave/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28200
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
EOF
### aribiter節(jié)點(diǎn)配置文件
cat >/opt/arbiter/conf/mongod.conf<<EOF
systemLog:
destination: file
logAppend: true
path: /opt/arbiter/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/arbiter/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/arbiter/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28300
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
EOF
##############################################################################
4.mongodb02創(chuàng)建配置文件
### master節(jié)點(diǎn)配置文件
cat >/opt/master/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/master/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/master/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/master/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28100
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
EOF
### slave節(jié)點(diǎn)配置文件
cat >/opt/slave/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/slave/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/slave/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/slave/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28200
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
EOF
### aribiter節(jié)點(diǎn)配置文件
cat >/opt/arbiter/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/arbiter/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/arbiter/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/arbiter/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28300
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
EOF
##############################################################################
5.mongodb03創(chuàng)建配置文件
### master節(jié)點(diǎn)配置文件
cat >/opt/master/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/master/log/mongod.log
storage:
journal:
enabled: true
dbPath: /data/master/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/master/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28100
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
EOF
### slave節(jié)點(diǎn)配置文件
cat >/opt/slave/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/slave/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/slave/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/slave/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28200
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
EOF
### aribiter節(jié)點(diǎn)配置文件
cat >/opt/arbiter/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/arbiter/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/arbiter/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/arbiter/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28300
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
EOF
6.優(yōu)化警告
注意!三臺服務(wù)器都操作3衅!A诟!
useradd mongod -s /sbin/nologin -M
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
7.創(chuàng)建配置文件并啟動服務(wù)
注意燕鸽!三臺服務(wù)器都操作P质馈!啊研!
### master啟動文件
cat >/lib/systemd/system/mongod-master.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/master/conf/mongod.conf"
ExecStart=/opt/mongodb/bin/mongod \$OPTIONS
ExecStartPre=/usr/bin/chown -R mongod:mongod /opt/master
ExecStartPre=/usr/bin/chown -R mongod:mongod /data/master
PermissionsStartOnly=true
PIDFile=/opt/master/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
EOF
### slave啟動文件
cat >/lib/systemd/system/mongod-slave.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/slave/conf/mongod.conf"
ExecStart=/opt/mongodb/bin/mongod \$OPTIONS
ExecStartPre=/usr/bin/chown -R mongod:mongod /opt/slave
ExecStartPre=/usr/bin/chown -R mongod:mongod /data/slave
PermissionsStartOnly=true
PIDFile=/opt/slave/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
EOF
### arbiter啟動文件
cat >/lib/systemd/system/mongod-arbiter.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/arbiter/conf/mongod.conf"
ExecStart=/opt/mongodb/bin/mongod \$OPTIONS
ExecStartPre=/usr/bin/chown -R mongod:mongod /opt/arbiter
ExecStartPre=/usr/bin/chown -R mongod:mongod /data/arbiter
PermissionsStartOnly=true
PIDFile=/opt/arbiter/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
EOF
重新載入配置
systemctl daemon-reload
systemctl start mongod-master.service
systemctl start mongod-slave.service
systemctl start mongod-arbiter.service
netstat -lntup|grep mongod
8.初始化副本集
## mongodb01 master節(jié)點(diǎn)初始化shard1的副本
mongo --port 28100
rs.initiate()
等一等
rs.add("10.0.0.75:28200")
rs.addArb("10.0.0.74:28300")
## mogodb02 master節(jié)點(diǎn)初始化shard2的副本
mongo --port 28100
rs.initiate()
等一等
rs.add("10.0.0.73:28200")
rs.addArb("10.0.0.75:28300")
## mongodb03 master節(jié)點(diǎn)初始化shard3的副本
mongo --port 28100
rs.initiate()
等一等
rs.add("10.0.0.74:28200")
rs.addArb("10.0.0.73:28300")
9.檢查命令
mongo --port 28100
rs.status()
rs.isMaster()
4.分片集群搭建config
注意御滩!三臺服務(wù)器操作一樣E概 !削解!
1.創(chuàng)建目錄
mkdir -p /opt/config/{conf,log,pid}
mkdir -p /data/config/
2.創(chuàng)建配置文件
cat >/opt/config/conf/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/config/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/config/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/config/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 40000
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
replication:
replSetName: configset
sharding:
clusterRole: configsvr
EOF
3.啟動
cat >/lib/systemd/system/mongod-config.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/config/conf/mongod.conf"
ExecStart=/opt/mongodb/bin/mongod \$OPTIONS
ExecStartPre=/usr/bin/chown -R mongod:mongod /opt/config
ExecStartPre=/usr/bin/chown -R mongod:mongod /data/config
PermissionsStartOnly=true
PIDFile=/opt/config/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mongod-config.service
#只在mongodb01上操作8幌摇!7胀浴M蠊瘛!
4.mongodb01上初始化副本集
mongo --port 40000
rs.initiate({
_id:"configset",
configsvr: true,
members:[
{_id:0,host:"10.0.0.73:40000"},
{_id:1,host:"10.0.0.74:40000"},
{_id:2,host:"10.0.0.75:40000"},
] })
5.檢查
rs.status()
rs.isMaster()
configset:PRIMARY> rs.isMaster()
{
"hosts" : [
"10.0.0.73:40000",
"10.0.0.74:40000",
"10.0.0.75:40000"
],
"setName" : "configset",
5.mongos配置
#三臺全部操作
1.創(chuàng)建目錄
mkdir -p /opt/mongos/{conf,log,pid}
2.創(chuàng)建配置文件
cat >/opt/mongos/conf/mongos.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongos/log/mongos.log
processManagement:
fork: true
pidFilePath: /opt/mongos/pid/mongos.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 60000
bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')
sharding:
configDB:
configset/10.0.0.73:40000,10.0.0.74:40000,10.0.0.75:40000
EOF
3.啟動
cat >/lib/systemd/system/mongod-mongos.service<<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/mongos/conf/mongos.conf"
ExecStart=/opt/mongodb/bin/mongos \$OPTIONS
ExecStartPre=/usr/bin/chown -R mongod:mongod /opt/mongos
PermissionsStartOnly=true
PIDFile=/opt/mongos/pid/mongos.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mongod-mongos.service
#僅此一臺
4.登錄期中一臺mongos
mongo --port 60000
5.添加分片成員
use admin
db.runCommand({addShard:'shard1/10.0.0.73:28100,10.0.0.75:28200,10.0.0.74:28300'})
db.runCommand({addShard:'shard2/10.0.0.74:28100,10.0.0.73:28200,10.0.0.75:28300'})
db.runCommand({addShard:'shard3/10.0.0.75:28100,10.0.0.74:28200,10.0.0.73:28300'})
6.查看分片信息
db.runCommand( { listshards : 1 } )
6.分片配置
1.區(qū)間分片:
數(shù)據(jù)庫開啟分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "test" } )
創(chuàng)建集合索引
use test
db.range.ensureIndex( { id: 1 } )
對集合開啟分片矫废,片鍵是id
use admin
db.runCommand( { shardcollection : "test.range",key : {id: 1} } )
插入測試數(shù)據(jù)
use test
for(i=1;i<10000;i++){ db.range.insert({"id":i,"name":"shanghai","age":28,"date":new Date()}); }
db.range.stats()
db.range.count()
hash分片:
數(shù)據(jù)庫開啟分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "hash" } )
集合創(chuàng)建索引
use hash
db.hash.ensureIndex( { id: "hashed" } )
集合開啟哈希分片
use admin
sh.shardCollection( "hash.hash", { id: "hashed" } )
生成測試數(shù)據(jù)
use test1
for(i=1;i<10000;i++){ db.hash.insert({"id":i,"name":"shanghai","age":70}); }
分片驗證
shard1
mongo db01:28100
use test1
db.hash.count()
33755
shard2
mongo db02:28100
use test1
db.hash.count()
33142
shard3
mongo db03:28100
use test1
db.hash.count()
33102
7.分片集群常用管理命令
1列出分片所有詳細(xì)信息
db.printShardingStatus()
sh.status()
2列出所有分片成員信息
use admin
db.runCommand({ listshards : 1})
3列出開啟分片的數(shù)據(jù)庫
use config
db.databases.find({"partitioned": true })
4查看分片的片鍵
use config
db.collections.find().pretty()