MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/articleuid=28266791id=5758139 )
環(huán)境準(zhǔn)備
1.本例使用3臺(tái)Linux主機(jī),IP地址如下:
一、 點(diǎn)擊(此處)折疊或打開(kāi)
Server A: 192.168.106.101
Server B: 192.168.106.102
Server C: 192.168.106.103
2.根據(jù)需要,開(kāi)啟相應(yīng)主機(jī)防火墻的相關(guān)端口。本次需要用到3臺(tái)主機(jī),所以開(kāi)啟這3臺(tái)主機(jī)的以下端口:
點(diǎn)擊(此處)折疊或打開(kāi)
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27017 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27018 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27019 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 20000 -j ACCEPT
二、搭建分布式集群
運(yùn)用MongoDB的復(fù)制集(Replica Sets)+分片(Sharding)就能實(shí)現(xiàn)MongoDB分布式高可用的集群。
分片(sharding)能夠增加更多的機(jī)器來(lái)應(yīng)對(duì)不斷增加的負(fù)載和數(shù)據(jù),也不影響應(yīng)用。
復(fù)制集(Replica Sets)能確保每個(gè)分片節(jié)點(diǎn)都具有自動(dòng)備份、自動(dòng)故障恢復(fù)能力。
1.集群的環(huán)境
架構(gòu)圖:
主機(jī)設(shè)計(jì):
2.配置Replica Sets
創(chuàng)建數(shù)據(jù)目錄:
#在serverA上 mkdir -p /data/{shard1-1,shard2-1,config} #在serverB上 mkdir -p /data/{shard1-2,shard2-2,config} #在serverC上 mkdir -p /data/{shard1-3,shard2-3,config}2.1配置shard1的Replica Sets:
#在serverA上啟動(dòng)shard1-1 mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-1/ --logpath /data/shard1-1/shard1-1.log --logappend --replSet shard1 --directoryperdb #在serverB上啟動(dòng)shard1-2 mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-2/ --logpath /data/shard1-2/shard1-2.log --logappend --replSet shard1 --directoryperdb #在serverC上啟動(dòng)shard1-3 mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-3/ --logpath /data/shard1-3/shard1-3.log --logappend --replSet shard1 --directoryperdb用mongo連接其中一臺(tái)主機(jī)的27018端口的mongod,初始化復(fù)制集shard1:
mongo –port 27018
點(diǎn)擊(此處)折疊或打開(kāi)
>config={_id:'shard1',members:[ {_id:0,host:'192.168.106.101:27018',priority:2}, {_id:1,host:'192.168.106.102:27018',priority:1}, {_id:2,host:'192.168.106.103:27018',arbiterOnly:true}] }>rs.initiate(config)2.2 配置shard2的Replica Sets
#在serverA上啟動(dòng)shard2-1 mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-1/ --logpath /data/shard2-1/shard2-1.log --logappend --replSet shard2 --directoryperdb #在serverB上啟動(dòng)shard2-2 mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-2/ --logpath /data/shard2-2/shard2-2.log --logappend --replSet shard2 --directoryperdb #在serverC上啟動(dòng)shard2-3 mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-3/ --logpath /data/shard2-3/shard2-3.log --logappend --replSet shard2 --directoryperdb用mongo連接其中一臺(tái)主機(jī)(建議在ServerB上)的27019端口的mongod,初始化復(fù)制集shard2:
mongo --port 27019>config={_id:'shard2',members:[ {_id:0,host:'192.168.106.101:27019',arbiterOnly:true}, {_id:1,host:'192.168.106.102:27019',priority:1}, {_id:2,host:'192.168.106.103:27019',priority:2}] }>rs.initiate(config)3.配置config server
#在3臺(tái)主機(jī)中分別啟動(dòng)配置服務(wù) mongod --configsvr --port 20000 --dbpath /data/config/ --logpath /data/config/config.log --replSet docdetection --logappend --fork用mongo連接其中一臺(tái)主機(jī)的20000端口的mongod,初始化復(fù)制集docdetection:
mongo --port 20000configdb1={_id:'docdetection',members:[ {_id:0,host:'192.168.106.101:20000',priority:3}, {_id:1,host:'192.168.106.102:20000',priority:1}, {_id:2,host:'192.168.106.103:20000',priority:2}] }>rs.initiate(configdb1)4.配置router server
#在3臺(tái)主機(jī)中分別運(yùn)行mongos服務(wù) mongos --configdb docdetection/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork5.配置分片(shard cluster)
#連接到其中一臺(tái)機(jī)器的端口27017的mongos進(jìn)程,并切換到admin數(shù)據(jù)庫(kù)添加分片shard1和shard2: mongo --port 27017>use admin >db.runCommand({addshard:"shard1/192.168.106.101:27018,192.168.106.102:27018,192.168.106.103:27018"}) >db.runCommand({addshard:"shard2/192.168.106.101:27019,192.168.106.102:27019,192.168.106.103:27019"}) #激活數(shù)據(jù)庫(kù)(work)和集合(status)的分片功能: >db.runCommand({enablesharding:"work"}) >db.runCommand({shardcollection:"work.status",key:{_id:1}})將準(zhǔn)備好的mongodb的數(shù)據(jù)導(dǎo)入到mongodb中 cd /usr/local/mongodb/bin mongorestore -h localhost:27017 -d docdetection --dir /usr/local/mongodb/docdetection/6.驗(yàn)證
#查看shard1的集合work.status和shard2的文檔數(shù)幾乎均分,且之和等于路由節(jié)點(diǎn)下查看的文檔數(shù): mongo --port 27018 work >db.status.count() ./mongo --port 27019 work>db.status.count()./mongo work >db.status.count()#查看分片的狀態(tài):需要在mongs進(jìn)程下訪問(wèn): mongo --port 27017 > printShardingStatus()三、常見(jiàn)問(wèn)題
1.【問(wèn)題描述】執(zhí)行初始化副本集時(shí)報(bào)錯(cuò):
點(diǎn)擊(此處)折疊或打開(kāi)
> rs.initiate(config) {"ok" : 0,"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.106.102:27018 failed with No route to host, 192.168.106.103:27018 failed with No route to host","code" : 74,"codeName" : "NodeNotFound" }【解決方法】
開(kāi)啟防火墻的相關(guān)端口
點(diǎn)擊(此處)折疊或打開(kāi)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 27018 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 27019 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 20000 -j ACCEPT2.【問(wèn)題描述】初始化副本集報(bào)錯(cuò):
點(diǎn)擊(此處)折疊或打開(kāi)
> rs.initiate(config) {"ok" : 0,"errmsg" : "This node, 192.168.106.101:27019, with _id 0 is not electable under the new configuration version 1 for replica set shard2","code" : 93,"codeName" : "InvalidReplicaSetConfig" }【解決方法】
如果設(shè)置主機(jī)A為仲裁節(jié)點(diǎn),那么不要在A主機(jī)上執(zhí)行初始化操作,否則會(huì)報(bào)上述錯(cuò)誤。
3.【問(wèn)題描述】啟動(dòng)路由器mongos報(bào)錯(cuò):
點(diǎn)擊(此處)折疊或打開(kāi)
./mongos --configdb 192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --forkFailedToParse: mirrored config server connections are not supported; for config server replica sets be sure to use the replica set connection stringtry './mongos --help' for more information【解決方法】
從Mongodb3.2之后,啟動(dòng)mongos時(shí)需使用副本集名稱,否則會(huì)報(bào)上述錯(cuò)誤。例如:
點(diǎn)擊(此處)折疊或打開(kāi)
./mongos –configdb 副本集名稱/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork總結(jié)
以上是生活随笔為你收集整理的MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/articleuid=28266791id=5758139 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 减肥可以吃瓜子吗邱医生(减肥可以吃瓜子吗
- 下一篇: Linux增加虚拟内存的配置方案