mongodb的高可用集群设计实战
环境:
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 调用
下载安装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
各节点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
副本集配置:
启动各节点:
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
#启动仲裁节点
在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
初始化节点:
md01:(登录其中任何一个节点操作皆可)
> 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 查看状态
查看副本集状态
>rs.status() >user local; >rs.isMaster() >db.system.replset.find()
查看当前主库:
>db.$cmd.findOne({ismaster:1});
另一组副本集同理操作即可;这里为省篇幅就不再写出,但切记两复制集名称不可以重复
启动并配置三台Config Server
#md01,03,05上执行
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地址参数,chunkSize是分割数据时每块(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会知道它所连接的是一个复制集,并会使用新的主节点(md02或md03)
db.runCommand( { listshards : 1 } );
如果列出(sharding)了以上二个你加的shards,表示shards已经配置成功
导入数据:
mongoimport -d elain -c elain --type csv --headerline --file /root/bak/test.csv --host md02:30000
注:数据自备,我这里是从生产环境下MYSQL里导出的一些真实数据来做测试
#激活数据库分片
db.runCommand( { enablesharding : "elain" } ); mongos> db.runCommand( { enablesharding : "elain" } ); { "ok" : 1 } mongos> show dbs config 0.1875GB elain 0.453125GB test (empty)
#进入数据库,建立索引,为分片做准备
/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 }
#查看分片状态
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 }