首页 > 代码库 > Mongodb集群与分片 2
Mongodb集群与分片 2
前面我们介绍了简单的集群配置实例。在简单实例中,虽然MongoDB auto-Sharding解决了海量存储问题,和动态扩容问题,但是离我们在真实环境下面所需要的高可靠性和高可用性还有一定的距离。
下面我们就介绍一个接近实际的解决方案:
- Shard:使用Replica Sets,来确保数据的可靠性。通过这个方案,可以在每个节点有数据的备份、实现自动控制容错转移和自动恢复
- Config:使用3个配置服务器,确保元数据完整性
- Route:配合LVS,实现负载均衡,提高接入性能。
1、配置集群目录
数据存储目录:
1 $ mkdir -p /home/scotte.ye/data/10001 2 $ mkdir -p /home/scotte.ye/data/10002 3 $ mkdir -p /home/scotte.ye/data/10003 4 5 $ mkdir -p /home/scotte.ye/data/10011 6 $ mkdir -p /home/scotte.ye/data/10012 7 $ mkdir -p /home/scotte.ye/data/10013 8 9 $ mkdir -p /home/scotte.ye/data/config110 $ mkdir -p /home/scotte.ye/data/config211 $ mkdir -p /home/scotte.ye/data/config3
日志目录:
1 $ mkdir -p /home/scotte.ye/data/logs
2、配置 Shard Replica Sets
配置第一组Sets
1 //100012 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10001/ -port=10001 --nohttpinterface --replSet set13 4 //100025 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10002/ -port=10002 --nohttpinterface --replSet set16 7 //100038 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10003/ -port=10003 --nohttpinterface --replSet set1
配置集群
1 $ ./mongo -port 10001 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10001/test 4 >#配置集合1 5 >#构建参数 6 >cfg={_id:‘set1‘, 7 members:[{_id:1,host:‘192.168.35.106:10001‘}, 8 {_id:1,host:‘192.168.35.106:10002‘}, 9 {_id:1,host:‘192.168.35.106:10003‘}]10 };11 {12 "_id":"set1",13 "members":[14 {15 "_id":1,16 "host":"192.168.35.106:10001"17 },18 {19 "_id":2,20 "host":"192.168.35.106:10002"21 },22 {23 "_id":3,24 "host":"192.168.35.106:10003"25 }26 ]27 }28 >#让参数生效29 >rs.initiate(cfg);30 {31 "info":"Config now saved locally. Should come online in about aminute.",32 "OK":133 }34 >#查看运行状态35 >rs.status();36 {37 "set" : "set1",38 "date" : ISODate("2012-02-29T12:02:46Z"),39 "myState" : 2,40 "syncingTo" : "192.168.35.106:10003",41 "members" : [42 {43 "_id" : 1,44 "name" : "192.168.35.106:10001",45 "health" : 1,46 "state" : 2,47 "stateStr" : "PRIMARY",48 "optime" : {49 "t" : 1330435339000,50 "i" : 241351 },52 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),53 "self" : true54 },55 {56 "_id" : 2,57 "name" : "192.168.35.106:10002",58 "health" : 1,59 "state" : 2,60 "stateStr" : "SECONDARY",61 "uptime" : 1112736,62 "optime" : {63 "t" : 1330435339000,64 "i" : 241365 },66 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),67 "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"),68 "pingMs" : 069 },70 {71 "_id" : 3,72 "name" : "192.168.35.106:10003",73 "health" : 1,74 "state" : 1,75 "stateStr" : "SECONDARY",76 "uptime" : 1112736,77 "optime" : {78 "t" : 1330435339000,79 "i" : 241380 },81 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),82 "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"),83 "pingMs" : 084 }85 ],86 "ok" : 187 }
配置第二组sets
1 //100112 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10011/ -port=10011 --nohttpinterface --replSet set23 4 //100125 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10012/ -port=10012 --nohttpinterface --replSet set26 7 //100138 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10013/ -port=10013 --nohttpinterface --replSet set2
配置集群
1 $ ./mongo -port 10011 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10011/test 4 >#配置集合1 5 >#构建参数 6 >cfg={_id:‘set2‘, 7 members:[{_id:1,host:‘192.168.35.106:10011‘}, 8 {_id:1,host:‘192.168.35.106:10012‘}, 9 {_id:1,host:‘192.168.35.106:10013‘}]10 };11 {12 "_id":"set2",13 "members":[14 {15 "_id":1,16 "host":"192.168.35.106:10011"17 },18 {19 "_id":2,20 "host":"192.168.35.106:10012"21 },22 {23 "_id":3,24 "host":"192.168.35.106:10013"25 }26 ]27 }28 >#让参数生效29 >rs.initiate(cfg);30 {31 "info":"Config now saved locally. Should come online in about aminute.",32 "OK":133 }34 >#查看运行状态35 >rs.status();36 {37 "set" : "set2",38 "date" : ISODate("2012-02-29T12:12:46Z"),39 "myState" : 2,40 "syncingTo" : "192.168.35.106:10011",41 "members" : [42 {43 "_id" : 1,44 "name" : "192.168.35.106:10011",45 "health" : 1,46 "state" : 2,47 "stateStr" : "PRIMARY",48 "optime" : {49 "t" : 1330435339000,50 "i" : 241351 },52 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),53 "self" : true54 },55 {56 "_id" : 2,57 "name" : "192.168.35.106:10012",58 "health" : 1,59 "state" : 2,60 "stateStr" : "SECONDARY",61 "uptime" : 1112736,62 "optime" : {63 "t" : 1330435339000,64 "i" : 241365 },66 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),67 "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"),68 "pingMs" : 069 },70 {71 "_id" : 3,72 "name" : "192.168.35.106:10013",73 "health" : 1,74 "state" : 1,75 "stateStr" : "SECONDARY",76 "uptime" : 1112736,77 "optime" : {78 "t" : 1330435339000,79 "i" : 241380 },81 "optimeDate" : ISODate("2012-02-28T13:22:19Z"),82 "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"),83 "pingMs" : 084 }85 ],86 "ok" : 187 }
启用config servier
1 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config1/ -port=20000 --nohttpinterface2 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config2/ -port=20001 --nohttpinterface3 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config3/ -port=20002 --nohttpinterface
启用Route server
1 $ ./mongos -configdb="192.168.35.106:20000,192.168.35.106:20001,192.168.35.106:20002" --fork -logpath=/home/data/logs/null2 >#查看是否正常运行3 >ps aux|grep mongos|grep -v grep4 root 2726 0.0 0.1 174192 3392 ? Sl Feb16 3:20 ./mongos -configdb=192.168.35.106:20000,192.168.35.106:20001,192.168.35.106:20002 --fork -logpath=/home/data/logs/null
开始配置Sharding
1 $ ./mongo -port 10001 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10001/test 4 >#进入管理数据库 5 >use admin 6 switched to db admin 7 >#添加Sharding 8 > db.runcommand({addshard:‘set1/192.168.35.106:10001,192.168.35.106:10002,192.168.35.106:10003‘}) 9 {"shardAdded":"set1","ok":1}10 > db.runCommand({addshard:‘set2/192.168.35.106:10011,192.168.35.106:10012,192.168.35.106:10013‘})11 {"shardAdded":"set2","ok":1}12 >#让test数据库支持Sharding13 >db.runCommand({enablesharding:‘test‘})14 {"ok":1}15 >#让数据库中的一个集合生效,且根据ID来进行分片16 >db.runCommand({shardcollection:‘test.user‘,key:{_id:1}})17 {"collectionshrded":"test.user","ok":1}18 19 注:另外可以通过下面两个命令查看sharding情况:20 >db.runCommand({listshards:1}21 >printShardingStatus();
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。