第二十三章 Replica Sets + Sharding
MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠、高可用还有些距离,所以有了” Replica Sets + Sharding”的解决方案:
l Shard:
使用 Replica Sets,确保每个数据节点都具有备份、自动容错转移、自动恢复能力。
l Config:
使用3个配置服务器,确保元数据完整性
l Route:
使用3个路由进程,实现负载平衡,提高客户端接入性能
以下我们配置一个 Replica Sets +Sharding 的环境,架构图如下:
开放的端口如下:
主机 | IP | 服务及端口 |
Server A | 192.168.3.231 | mongod shard1_1:27017 mongod shard2_1:27018 mongod config1:20000 mongs1:30000 |
Server B | 192.168.3.232 | mongod shard1_2:27017 mongod shard2_2:27018 mongod config2:20000 mongs2:30000 |
Server C | 192.168.3.233 | mongod shard1_3:27017 mongod shard2_3:27018 mongod config3:20000 mongs3:30000 |
23.1创建数据目录
在Server A上:
[root@localhost bin]# mkdir -p /data/shard1_1 [root@localhost bin]# mkdir -p /data/shard2_1 [root@localhost bin]# mkdir -p /data/config |
在Server B上:
[root@localhost bin]# mkdir -p /data/shard1_2 [root@localhost bin]# mkdir -p /data/shard2_2 [root@localhost bin]# mkdir -p /data/config |
在Server C上:
[root@localhost bin]# mkdir -p /data/shard1_3 [root@localhost bin]# mkdir -p /data/shard2_3 [root@localhost bin]# mkdir -p /data/config |
23.2配置Replica Sets
23.2.1配置shard1所用到的ReplicaSets
在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 } |
23.2.2配置shard2所用到的ReplicaSets
在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]# |
在Server B上:
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork all output going to: /data/shard2_2/shard2_2.log forked process: 18923 [root@localhost bin]# |
在Server C上:
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork [root@localhost bin]# all output going to: /data/shard2_3/shard2_3.log forked process: 18824
[root@localhost bin]# |
用mongo连接其中一台机器的27018端口的mongod,初始化Replica Sets “shard2”,执行:
[root@localhost bin]# ./mongo --port 27018 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:27018/test > config = {_id: 'shard2', members: [ ... {_id: 0, host: '192.168.3.231:27018'}, ... {_id: 1, host: '192.168.3.232:27018'}, ... {_id: 2, host: '192.168.3.233:27018'}] ... } …… > rs.initiate(config) { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } |
23.3配置3台Config Server
在Server A、B、C上执行:
/Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork |
23.4配置3台Route Process
在Server A、B、C上执行:
/Apps/mongo/bin/mongos --configdb 192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork |
23.5配置Shard Cluster
连接到其中一台机器的端口30000的mongos进程,并切换到admin数据库做以下配置
[root@localhost bin]# ./mongo --port 30000 MongoDB shell version: 1.8.1 connecting to: 127.0.0.1:30000/test > use admin switched to db admin >db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:27017"}); { "shardAdded" : "shard1", "ok" : 1 } >db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:27018"}); { "shardAdded" : "shard2", "ok" : 1 } > |
激活数据库及集合的分片
db.runCommand({ enablesharding:"test" }) db.runCommand({ shardcollection: "test.users", key: { _id:1 }}) |
23.6验证Sharding正常工作
连接到其中一台机器的端口30000的mongos进程,并切换到test数据库,以便添加测试数据
use test for(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"}); db.users.stats() { "sharded" : true, "ns" : "test.users", "count" : 200000, "size" : 25600384, "avgObjSize" : 128, "storageSize" : 44509696, "nindexes" : 2, "nchunks" : 15, "shards" : { "shard0000" : { …… }, "shard0001" : { …… } }, "ok" : 1 } |
可以看到Sharding搭建成功了,跟我们期望的结果一致,至此我们就将Replica Sets与Sharding结合的架构也学习完毕了。
-------------------------------------------------------------------
《MongoDB管理与开发精要》、《Redis实战》作者
ChinaUnix.net专家 http://cdhongwan.blog.chinaunix.net
@CD红丸 http://weibo.com/u/2446082491