您好,登錄后才能下訂單哦!
高數(shù)據(jù)量和吞吐量的數(shù)據(jù)庫應(yīng)用會(huì)對(duì)單機(jī)的性能造成較大壓力,大的查詢量會(huì)將單機(jī)的CPU耗盡,大的數(shù)據(jù)量對(duì)單機(jī)的存儲(chǔ)壓力較大,最終會(huì)耗盡系統(tǒng)的內(nèi)存而將壓力轉(zhuǎn)移到磁盤IO上。
為了解決這些問題,有兩個(gè)基本的方法: 垂直擴(kuò)展和水平擴(kuò)展。
垂直擴(kuò)展:增加更多的CPU和存儲(chǔ)資源來擴(kuò)展容量。
水平擴(kuò)展:將數(shù)據(jù)集分布在多個(gè)服務(wù)器上。水平擴(kuò)展即分片。
分片為應(yīng)對(duì)高吞吐量與大數(shù)據(jù)量提供了方法。使用分片減少了每個(gè)分片需要處理的請求數(shù),因此,通過水平擴(kuò)展,集群可以提高自己的存儲(chǔ)容量和吞吐量。舉例來說,當(dāng)插入一條數(shù)據(jù)時(shí),應(yīng)用只需要訪問存儲(chǔ)這條數(shù)據(jù)的分片,使用分片減少了每個(gè)分片存儲(chǔ)的數(shù)據(jù)。
(1)mongos :數(shù)據(jù)路由,和客戶端打交道的模塊。mongos本身沒有任何數(shù)據(jù),他也不知道該怎么處理這數(shù)據(jù),去找config server
(2)config server:所有存、取數(shù)據(jù)的方式,所有shard節(jié)點(diǎn)的信息,分片功能的一些配置信息??梢岳斫鉃檎鎸?shí)數(shù)據(jù)的元數(shù)據(jù)。
(3)shard:真正的數(shù)據(jù)存儲(chǔ)位置,以chunk為單位存數(shù)據(jù)。
架構(gòu)圖
系統(tǒng)版本:CentOS Linux release 7.5.1804 (Core)
防火墻SE關(guān)閉
mongodb版本:mongodb-linux-x86_64-3.2.1.tgz
采用Mongodb多實(shí)例進(jìn)行
解壓移動(dòng)mongodb安裝包
tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
cd /opt/
mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb
建立軟連接方便管理
cd /usr/local/mongodb/
cd bin/
ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo
ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
mkdir -p /data/mongodb/mongodb{1,2,3,4}
cd /data/mongodb/
mkdir logs
cd logs/
touch mongodb{1,2,3,4}.log
chmod 777 *.log
優(yōu)化進(jìn)程以及打開文件數(shù)量
ulimit -u 25000
ulimit -n 25000
編輯配置文件(修改加粗項(xiàng)目)
cd /usr/local/mongodb/bin/
vim mongodb1.confport=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/mongodb/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true //指定為配置服務(wù)器
內(nèi)存分?jǐn)偅耗彻?jié)點(diǎn)內(nèi)存不足時(shí),從其他節(jié)點(diǎn)分配內(nèi)存
sysctl -w vm.zone_reclaim_mode=0
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
復(fù)制修改2,3的配置文件(修改粗體處)
cp -p mongodb1.conf mongodb2.conf
vim mongodb2.conf
port=47017
dbpath=/data/mongodb/mongodb2
logpath=/data/mongodb/logs/mongodb2.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
cp -p mongodb2.conf mongodb3.conf
vim mongodb3.conf
port=47018
dbpath=/data/mongodb/mongodb3
logpath=/data/mongodb/logs/mongodb3.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
啟動(dòng)服務(wù)
mongod -f mongodb1.conf
mongod -f mongodb2.conf
mongod -f mongodb3.conf
開啟路由模式
./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.137.13:37017 --chunkSize 1
到此復(fù)制集配置完成
登陸
[root@cent bin]# mongo
MongoDB shell version: 3.2.1
connecting to: test
Welcome to the MongoDB shell.
…
mongos> //這里顯示用mongs登陸成功(配置服務(wù)器)
查看是否又分片服務(wù)器
mongos> sh.status()
]--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5b9e394051c099916cc7b236")
}
shards: //這里沒有東西,那就是還沒有創(chuàng)建分片服務(wù)器
active mongoses:
"3.2.1" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
添加分片服務(wù)器
mongos> sh.addShard("192.168.137.13:47017") //添加47017
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.137.13:47018") //添加47018
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5b9e394051c099916cc7b236")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.137.13:47017" } //看到添加成功了
{ "_id" : "shard0001", "host" : "192.168.137.13:47018" }
active mongoses:
"3.2.1" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
給數(shù)據(jù)庫添加一些東西,看看它是否會(huì)分片。
mongos> use mood
switched to db mood
mongos> for(var i=1;i<=10000;i++)db.info.insert({"id":i,"name":"makee"+i})
WriteResult({
"writeError" : {
"code" : 70,
"errmsg" : "unable to target write op for collection mood.info :: caused by :: ShardNotFound No shards found"
}
})
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ba24eeef6c5933feb7b6cf5")
}
shards:
active mongoses:
"3.2.1" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases: //并沒有
因?yàn)槲覀冃枰獑?dòng)分片才可以,下面啟動(dòng)它
mongos> sh.enableSharding("mood") //開啟數(shù)據(jù)庫分片
{ "ok" : 1 }
mongos> db.info.createIndex({"id":1}) //創(chuàng)建表索引
{
"raw" : {
"192.168.137.13:47017" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1
}
mongos> sh.shardCollection("mood.info",{"id":1}) //表分片{ "collectionsharded" : "mood.info", "ok" : 1 }
mongos> sh.status() //查看結(jié)果
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ba3b64dcc3cc1318414a128")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.137.13:47017" }
{ "_id" : "shard0001", "host" : "192.168.137.13:47018" }
active mongoses:
"3.2.1" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{ "_id" : "mood", "primary" : "shard0000", "partitioned" : true }
mood.info
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0001 Timestamp(2, 0) //已經(jīng)開始分片了
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0000 Timestamp(2, 1)
{ "id" : 9364 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。