2018年11月14日 11:05:50 Full Stack Developer 閱讀數 331前端
版權聲明:本文爲博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接和本聲明。linux
本文連接:https://blog.csdn.net/cron_zzx/article/details/84060221nginx
在以前有說過關於MongoDB的複製集,複製集主要用來實現自動故障轉移從而達到高可用的目的,然而,隨着業務規模的增加和時間的推移,業務數據量會愈來愈大,當前業務數據可能只有幾百GB不到,一臺DB服務器足以搞定全部的工做,而一旦業務數據量擴充大幾個TB幾百個TB時,就會產生一臺服務器沒法存儲的狀況,此時,須要將數據按照必定的規則分配到不一樣的服務器進行存儲、查詢等,即爲分片集羣。分片集羣要作到的事情就是數據分佈式存儲。mongodb
MongoDB分片羣集主要有以下三個組件:數據庫
mongodb4.0.2壓縮包官網地址 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.2.tgzvim
mongo1:192.168.247.141 shard1:27001 shard2:27002 shard3:27003 configs:27018 mongos:27017服務器
mongo2:192.168.247.142 shard1:27001 shard2:27002 shard3:27003 configs:27018 mongos:27017架構
mongo3:192.168.247.143 shard1:27001 shard2:27002 shard3:27003 configs:27018 mongos:27017app
說明:分佈式
一、啓動順序:configs---》shard(1,2,3)---》mongos
二、configs和shard一、shard二、shard3由mongod啓動和管理,mongos:27017 由mongos啓動管理。
tar -xf mongodb-linux-x86_64-4.0.2.tgz
mkdir /usr/local/mongo
mv mongodb-linux-x86_64-4.0.2/* /usr/local/mongo/
echo 'export PATH=$PATH:/usr/local/mongo/bin' >> /etc/profile
source /etc/profile
cd /usr/local/mongo/
mkdir conf log
mkdir -p data/config
mkdir -p data/shard1
mkdir -p data/shard2
mkdir -p data/shard3
touch log/config.log
touch log/mongos.log
touch log/shard1.log
touch log/shard2.log
touch log/shard3.log
touch conf/config.conf
touch conf/mongos.conf
touch conf/shard1.conf
touch conf/shard2.conf
touch conf/shard3.conf
vim conf/config.conf
dbpath=/usr/local/mongo/data/config
logpath=/usr/local/mongo/log/config.log
port=27018
logappend=true
fork=true
maxConns=5000
#複製集名稱
replSet=configs
#置參數爲true
configsvr=true
#容許任意機器鏈接
bind_ip=0.0.0.0
vim conf/shard1.conf
dbpath=/usr/local/mongo/data/shard1 #其餘2個分片對應修改成shard二、shard3文件夾
logpath=/usr/local/mongo/log/shard1.log #其餘2個分片對應修改成shard2.log、shard3.log
port=27001 #其餘2個分片對應修改成2700二、27003
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard1 #其餘2個分片對應修改成shard二、shard3
bind_ip=0.0.0.0
vim conf/shard2.conf
dbpath=/usr/local/mongo/data/shard2
logpath=/usr/local/mongo/log/shard2.log
port=27002
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard2
bind_ip=0.0.0.0
vim conf/shard3.conf
dbpath=/usr/local/mongo/data/shard3
logpath=/usr/local/mongo/log/shard3.log
port=27003
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard3
bind_ip=0.0.0.0
vim conf/mongos.conf
systemLog:
destination: file
logAppend: true
path: /usr/local/mongo/log/mongos.log
processManagement:
fork: true
# pidFilePath: /var/log/nginx/mongodbmongos.pid
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#監聽的配置服務器,只能有1個或者3個 configs爲配置服務器的副本集名字
sharding:
configDB: configs/mongo1:27018,mongo2:27018,mongo3:27018
scp /usr/local/mongo/* mongo2:/usr/local/mongo/
scp /usr/local/mongo/* mongo3:/usr/local/mongo/
啓動服務(全部主機):
啓動配置服務器副本集:mongod -f /usr/local/mongo/conf/config.conf
mongo --port 27018
配置副本集:
config = {
_id : "configs",
members : [
{_id : 0, host : "192.168.247.141:27018" },
{_id : 1, host : "192.168.247.142:27018" },
{_id : 2, host : "192.168.247.143:27018" }
]
}
初始化命令:
rs.initiate(config);
rs.status()
啓動3個分片副本集:
mongod -f /usr/local/mongo/conf/shard1.conf
mongo --port 27001
配置副本集:
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.247.141:27001" },
{_id : 1, host : "192.168.247.142:27001" },
{_id : 2, host : "192.168.247.143:27001" }
]
}
mongod -f /usr/local/mongo/conf/shard2.conf
mongo --port 27002
配置副本集:
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.247.141:27002" },
{_id : 1, host : "192.168.247.142:27002" },
{_id : 2, host : "192.168.247.143:27002" }
]
}
mongod -f /usr/local/mongo/conf/shard3.conf
mongo --port 27003
配置副本集:
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.247.141:27003" },
{_id : 1, host : "192.168.247.142:27003" },
{_id : 2, host : "192.168.247.143:27003" }
]
}
啓動路由服務器副本集:
mongos -f /usr/local/mongo/conf/mongos.conf
mongo --port 27017
啓用分片(只需在任意一臺執行便可):
use admin
sh.addShard("shard1/192.168.247.141:27001,192.168.247.142:27001,192.168.247.143:27001")
sh.addShard("shard2/192.168.247.141:27002,192.168.247.142:27002,192.168.247.143:27002")
sh.addShard("shard3/192.168.247.141:27003,192.168.247.142:27003,192.168.247.143:27003")
----------------------------------------------------------
設置分片chunk大小
use config
db.setting.save({"_id":"chunksize","value":1}) # 設置塊大小爲1M是方便實驗,否則須要插入海量數據
模擬寫入數據
use mytest
for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"zzx"+i})} #模擬往mytest數據庫的user表寫入5萬數據
七、啓用數據庫分片
sh.enableSharding("mytest")
八、建立索引,對錶進行分片
db.user.createIndex({"id":1}) # 以"id"做爲索引
sh.shardCollection(mytest.user",{"id":1}) # 根據"id"對user表進行分片
sh.status() # 查看分片狀況
到此,MongoDB分佈式集羣就搭建完畢。