在Mongodb里面存在另一種集群,就是分片技術(shù),可以滿足MongoDB數(shù)據(jù)量大量增長的需求。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)渭濱免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
高數(shù)據(jù)量和吞吐量的數(shù)據(jù)庫應(yīng)用會對單機(jī)的性能造成較大壓力,大的查詢量會將單機(jī)的CPU耗盡,大的數(shù)據(jù)量對單機(jī)的存儲壓力較大,最終會耗盡系統(tǒng)的內(nèi)存而將壓力轉(zhuǎn)移到磁盤IO上。
垂直擴(kuò)展:增加更多的CPU和存儲資源來擴(kuò)展容量。
Shard:
用于存儲實(shí)際的數(shù)據(jù)塊,實(shí)際生產(chǎn)環(huán)境中一個shard server角色可由幾臺機(jī)器組個一個replica set承擔(dān),防止主機(jī)單點(diǎn)故障
Config Server:
mongod實(shí)例,存儲了整個 ClusterMetadata,其中包括 chunk信息。
Query Routers:
前端路由,客戶端由此接入,且讓整個集群看上去像單一數(shù)據(jù)庫,前端應(yīng)用可以透明使用。
vim mongodb1.conf
port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true #開啟配置服務(wù)
mongod -f /usr/local/mongodb/bin/mongodb1.conf #開啟配置實(shí)例
vim mongodb2.conf
port=47017
dbpath=/data/mongodb/mongodb2
logpath=/data/logs/mongodb2.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true #開啟分片服務(wù)
vim mongodb3.conf
port=47018
dbpath=/data/mongodb/mongodb3
logpath=/data/logs/mongodb3.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true #開啟分片服務(wù)
mongod -f /usr/local/mongodb/bin/mongodb2.conf #開啟分片實(shí)例
mongod -f /usr/local/mongodb/bin/mongodb3.conf
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.217.134:37017 --chunkSize 1
2018-07-23T14:15:28.185+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 15337
child process started successfully, parent exiting
[root@localhost bin]# mongo
MongoDB shell version: 3.2.1
......
mongos> show dbs
config 0.031GB
mongos> sh.status() #查看分片狀態(tài)
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5b557280f9effb757fd31cdb")
}
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:
mongos> sh.addShard("192.168.217.134:47017") #添加分片
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.217.134:47018")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status() #查看分片狀態(tài)
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5b557280f9effb757fd31cdb")
}
shards: #分片信息
{ "_id" : "shard0000", "host" : "192.168.217.134:47017" }
{ "_id" : "shard0001", "host" : "192.168.217.134: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:
mongos> use test
switched to db test
mongos> for(var i=1;i<=10000;i++)db.users.insert({"id":i,"name":"tom"+i}) #添加數(shù)據(jù)
WriteResult({ "nInserted" : 1 })
mongos> sh.status()
.......
databases:
{ "_id" : "test", "primary" : "shard0000", "partitioned" : false }
#partitioned 值為false 表示數(shù)據(jù)庫尚未分片。
mongos> sh.enableSharding("test") #啟用數(shù)據(jù)庫分片
mongos> db.users.createIndex({"id":1}) #創(chuàng)建索引
mongos> sh.shardCollection("test.users",{"id":1}) #表分片
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos> sh.status()
......
{ "id" : { "$minKey" : 1 } } -->> { "id" : 2341 } on : shard0001 Timestamp(5, 1)
{ "id" : 2341 } -->> { "id" : 4682 } on : shard0001 Timestamp(3, 0)
{ "id" : 4682 } -->> { "id" : 7023 } on : shard0000 Timestamp(6, 1)
{ "id" : 7023 } -->> { "id" : 9364 } on : shard0000 Timestamp(1, 3)
{ "id" : 9364 } -->> { "id" : 13407 } on : shard0000 Timestamp(3, 2)
{ "id" : 13407 } -->> { "id" : 21295 } on : shard0000 Timestamp(3, 3)
{ "id" : 21295 } -->> { "id" : 25976 } on : shard0001 Timestamp(4, 2)
{ "id" : 25976 } -->> { "id" : 33545 } on : shard0001 Timestamp(4, 3)
{ "id" : 33545 } -->> { "id" : 38226 } on : shard0000 Timestamp(5, 2)
{ "id" : 38226 } -->> { "id" : 45910 } on : shard0000 Timestamp(5, 3)
{ "id" : 45910 } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(6, 0)
#數(shù)據(jù)存放在兩個分片服務(wù)器上即:shard0000、shard0001中。
mongos> sh.status()
......
shards:
{ "_id" : "shard0000", "host" : "192.168.217.134:47017" }
{ "_id" : "shard0001", "host" : "192.168.217.134:47018" }
mongos> sh.addShardTag("shard0000","sales00") #添加標(biāo)簽
mongos> sh.addShardTag("shard0001","sales01")
mongos> sh.status()
......
shards:
{ "_id" : "shard0000", "host" : "192.168.217.134:47017", "tags" : [ "sales00" ] }
{ "_id" : "shard0001", "host" : "192.168.217.134:47018", "tags" : [ "sales01" ] }
mongos> use admin
mongos> db.runCommand({"removeshard":"192.168.217.134:47018"}) #刪除分片節(jié)點(diǎn)
ps:MongoDB 4以上的版本做分片,需要先把實(shí)例做成復(fù)制集。