码迷,mamicode.com
首页 > 数据库 > 详细

Mongodb集群与分片 2

时间:2014-08-15 10:39:28      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   strong   

前面我们介绍了简单的集群配置实例。在简单实例中,虽然MongoDB auto-Sharding解决了海量存储问题,和动态扩容问题,但是离我们在真实环境下面所需要的高可靠性和高可用性还有一定的距离。
下面我们就介绍一个接近实际的解决方案:
  • Shard:使用Replica Sets,来确保数据的可靠性。通过这个方案,可以在每个节点有数据的备份、实现自动控制容错转移和自动恢复
  • Config:使用3个配置服务器,确保元数据完整性
  • Route:配合LVS,实现负载均衡,提高接入性能。
 

1、配置集群目录

 
数据存储目录:
bubuko.com,布布扣
 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/config1
10 $ mkdir -p /home/scotte.ye/data/config2
11 $ mkdir -p /home/scotte.ye/data/config3
View Code

日志目录:

bubuko.com,布布扣
1 $ mkdir -p /home/scotte.ye/data/logs
View Code

 

2、配置 Shard Replica Sets
 
配置第一组Sets
bubuko.com,布布扣
1 //10001
2 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10001/ -port=10001 --nohttpinterface --replSet set1
3 
4 //10002
5 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10002/ -port=10002 --nohttpinterface --replSet set1
6 
7 //10003
8 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10003/ -port=10003 --nohttpinterface --replSet set1
View Code

配置集群

bubuko.com,布布扣
 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":1
33 }
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" : 2413
51                         },
52                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
53                         "self" : true
54                 },
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" : 2413
65                         },
66                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
67                         "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"),
68                         "pingMs" : 0
69                 },
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" : 2413
80                         },
81                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
82                         "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"),
83                         "pingMs" : 0
84                 }
85         ],
86         "ok" : 1
87 }
View Code

配置第二组sets

bubuko.com,布布扣
1 //10011
2 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10011/ -port=10011 --nohttpinterface --replSet set2
3 
4 //10012
5 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10012/ -port=10012 --nohttpinterface --replSet set2
6 
7 //10013
8 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10013/ -port=10013 --nohttpinterface --replSet set2
View Code

配置集群

bubuko.com,布布扣
 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":1
33 }
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" : 2413
51                         },
52                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
53                         "self" : true
54                 },
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" : 2413
65                         },
66                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
67                         "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"),
68                         "pingMs" : 0
69                 },
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" : 2413
80                         },
81                         "optimeDate" : ISODate("2012-02-28T13:22:19Z"),
82                         "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"),
83                         "pingMs" : 0
84                 }
85         ],
86         "ok" : 1
87 }
View Code

启用config servier

bubuko.com,布布扣
1 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config1/ -port=20000 --nohttpinterface
2 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config2/ -port=20001 --nohttpinterface
3 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config3/ -port=20002 --nohttpinterface
View Code

启用Route server

bubuko.com,布布扣
1 $ ./mongos -configdb="192.168.35.106:20000,192.168.35.106:20001,192.168.35.106:20002" --fork -logpath=/home/data/logs/null
2 >#查看是否正常运行
3 >ps aux|grep mongos|grep -v grep
4 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
View Code

开始配置Sharding

bubuko.com,布布扣
 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数据库支持Sharding
13 >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();
View Code

 

Mongodb集群与分片 2,布布扣,bubuko.com

Mongodb集群与分片 2

标签:style   blog   http   color   使用   os   io   strong   

原文地址:http://www.cnblogs.com/chaoa/p/3914151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!