mongodb的高可用集群設計實戰(zhàn)
環(huán)境:
CentOS 6.0 x64
md01: 10.0.0.11
md02: 10.0.0.12
md03: 10.0.0.14
md04: 10.0.0.15
md05: 10.0.0.16
md06: 10.0.0.17
設計思路:
md01、md02、md03 做一組復制集
md04、md05、md06 做一組復制集
再把這兩組復制集用分片做成 shard1、shard2 用LVS 調(diào)用
下載安裝mongodb
cd /root/tools wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz tar zxvf mongodb-linux-x86_64-2.0.0.tgz mv mongodb-linux-x86_64-2.0.0 /elain/apps/mongodb-linux-x86_64-2.0.0 ln -s /elain/apps/mongodb-linux-x86_64-2.0.0 /elain/apps/mongodb ln -s /elain/apps/mongodb/bin/* /bin/
#添加用戶組
/usr/sbin/groupadd -g 690 mongodb /usr/sbin/useradd -g mongodb mongodb -u 690 -s /sbin/nologin
各節(jié)點hosts文件 添加:
true > /etc/hosts echo -ne " 10.0.0.11 md01 10.0.0.12 md02 10.0.0.14 md03 10.0.0.15 md04 10.0.0.16 md05 10.0.0.17 md06 " >>/etc/hosts
副本集配置:
啟動各節(jié)點:
md01
mkdir -p /elain/logs/mongodb/ mkdir -p /elain/data/mongodb/db /elain/apps/mongodb/bin/mongod --rest --replSet elain/md01:27017 --fork --port 27017 --dbpath /elain/data/mongodb/db/ --logpath /elain/logs/mongodb/mongodb.log
md02
mkdir -p /elain/logs/mongodb/ mkdir -p /elain/data/mongodb/db /elain/apps/mongodb/bin/mongod --rest --replSet elain/md02:27017 --fork --port 27017 --dbpath /elain/data/mongodb/db/ --logpath /elain/logs/mongodb/mongodb.log
md03
mkdir -p /elain/logs/mongodb/ mkdir -p /elain/data/mongodb/db /elain/apps/mongodb/bin/mongod --rest --replSet elain/md03:27017 --fork --port 27017 --dbpath /elain/data/mongodb/db/ --logpath /elain/logs/mongodb/mongodb.log
#啟動仲裁節(jié)點
在md02上
mkdir /elain/data/mongodb/arb /elain/apps/mongodb/bin/mongod --fork --rest --replSet elain --dbpath /elain/data/mongodb/arb --port 27015 --logpath /elain/logs/mongodb/mongodb.log
初始化節(jié)點:
md01:(登錄其中任何一個節(jié)點操作皆可)
> rs.initiate({ _id : "elain", members : [ {_id : 1, host : "md01:27017"}, {_id : 2, host : "md02:27017"}, {_id : 3, host : "md03:27017"}, {_id : 4, host : "md02:27015", "arbiterOnly": true}, ] });
#驗證
PRIMARY> rs.status()
也可瀏覽:http://10.0.0.11:28017/_replSet 查看狀態(tài)
查看副本集狀態(tài)
>rs.status() >user local; >rs.isMaster() >db.system.replset.find()
查看當前主庫:
>db.$cmd.findOne({ismaster:1});
另一組副本集同理操作即可;這里為省篇幅就不再寫出,但切記兩復制集名稱不可以重復
啟動并配置三臺Config Server
#md01,03,05上執(zhí)行
mkdir -p /elain/data/mongodb/config/ /elain/apps/mongodb/bin/mongod --configsvr --dbpath /elain/data/mongodb/config/ --port 20000 --logpath /elain/logs/mongodb/config.log --logappend --fork
5、部署并配置三臺Routing Server
指定所有的config sever地址參數(shù),chunkSize是分割數(shù)據(jù)時每塊(Chunk)的單位大小
#md02,md04,md06
/elain/apps/mongodb/bin/mongos --configdb md01:20000,md03:20000,md05:20000 --port 30000 --chunkSize 100 --logpath /elain/logs/mongodb/mongos.log --logappend --fork
6、命令行添加分片
連接到mongs服務器,并切換到admin
/elain/apps/mongodb/bin/mongo 10.0.0.11:30000/admin
db.runCommand( { addshard : "elain/md01:27017", name:"shard1", maxsize:2048, allowLocal:true } ); db.runCommand( { addshard : "chujq/md04:27017", name:"shard2", maxsize:2048, allowLocal:true } );
注:添加復制集elain,其中包含一個服務器md01:27017(還餓別的服務器,如md02、md03),如果md01掛了,mongos會知道它所連接的是一個復制集,并會使用新的主節(jié)點(md02或md03)
db.runCommand( { listshards : 1 } );
如果列出(sharding)了以上二個你加的shards,表示shards已經(jīng)配置成功
導入數(shù)據(jù):
mongoimport -d elain -c elain --type csv --headerline --file /root/bak/test.csv --host md02:30000
注:數(shù)據(jù)自備,我這里是從生產(chǎn)環(huán)境下MYSQL里導出的一些真實數(shù)據(jù)來做測試
#激活數(shù)據(jù)庫分片
db.runCommand( { enablesharding : "elain" } ); mongos> db.runCommand( { enablesharding : "elain" } ); { "ok" : 1 } mongos> show dbs config 0.1875GB elain 0.453125GB test (empty)
#進入數(shù)據(jù)庫,建立索引,為分片做準備
/elain/apps/mongodb/bin/mongo 10.0.0.12:30000/admin use elain; db.elain.ensureIndex({"client_userid":1});
查看索引:
mongos> db.elain.find({"client_userid" : 151512}).explain(); { "cursor" : "BtreeCursor client_userid_1", "nscanned" : 3, "nscannedObjects" : 3, "n" : 3, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { "client_userid" : [ [ 151512, 151512 ] ] } }
#添加分片:
use admin; db.runCommand( { shardcollection : "elain.elain",key : {client_userid: 1} } ) mongos> db.runCommand( { shardcollection : "elain.elain",key : {client_userid: 1} } ) { "collectionsharded" : "elain.elain", "ok" : 1 }
#查看分片狀態(tài)
use elain; db.elain.stats(); mongos> db.elain.stats(); { "sharded" : true, "flags" : 1, "ns" : "elain.elain", "count" : 507372, "numExtents" : 10, "size" : 84375328, "storageSize" : 97849344, "totalIndexSize" : 29253728, "indexSizes" : { "_id_" : 16474640, "client_userid_1" : 12779088 }, "avgObjSize" : 166.29874727024747, "nindexes" : 2, "nchunks" : 1, "shards" : { "shard2" : { "ns" : "elain.elain", "count" : 507372, "size" : 84375328, "avgObjSize" : 166.29874727024747, "storageSize" : 97849344, "numExtents" : 10, "nindexes" : 2, "lastExtentSize" : 26099712, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 29253728, "indexSizes" : { "_id_" : 16474640, "client_userid_1" : 12779088 }, "ok" : 1 } }, "ok" : 1 }