MongoDB整理笔记のReplica Sets + Sharding
MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠、高可用还有些距离,所以有了"Replica Sets + Sharding"的解决方案。
shard:
使用Replica Sets,确保每个数据节点都具有备份,自动容错转移,自动回复能力。
config:
使用3个配置服务器,确保元数据的完整性。
route:
使用3个路由进程,实现负载均衡,提高客户端接入性能。
配置Replica Sets + Sharding 架构图:
配置Replica Sets + Sharding
(1)配置shard1所用到的Replica Sets
在server A上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork [root@localhost bin]# all output going to: /data/shard1_1/shard1_1.log forked process: 18923
在server B上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork forked process: 18859 [root@localhost bin]# all output going to: /data/shard1_2/shard1_2.log [root@localhost bin]#
在Server C 上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork all output going to: /data/shard1_3/shard1_3.log forked process: 18768 [root@localhost bin]#
用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:
[root@localhost bin]# ./mongo --port 27017 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:27017/test > config = {_id: 'shard1', members: [ ... {_id: 0, host: '192.168.3.231:27017'}, ... {_id: 1, host: '192.168.3.232:27017'}, ... {_id: 2, host: '192.168.3.233:27017'}] ... } …… > rs.initiate(config) { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
(2)配置shard2所用到的Replica Sets
在server A上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork all output going to: /data/shard2_1/shard2_1.log [root@localhost bin]# forked process: 18993 [root@localhost bin]#