Nosql Mongodb之旅(31)—Replica Sets+Sharding

MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠、高可用还有些距离,所以有了"Replica Sets + Sharding"的解决方案。

    shard:

    使用Replica Sets,确保每个数据节点都具有备份,自动容错转移,自动回复能力。

    config:

    使用3个配置服务器,确保元数据的完整性。

    route:

    使用3个路由进程,实现负载均衡,提高客户端接入性能。

     配置Replica Sets + Sharding 架构图:

     Nosql Mongodb之旅(31)—Replica Sets+Sharding_第1张图片

    Nosql Mongodb之旅(31)—Replica Sets+Sharding_第2张图片

     配置Replica Sets + Sharding

    (1)配置shard1所用到的Replica Sets

    在server A上

[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017  
  2. --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork  
  3. [root@localhost bin]# all output going to: /data/shard1_1/shard1_1.log  
  4. forked process: 18923  
    在server B上
[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017  
  2. --dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork  
  3. forked process: 18859  
  4. [root@localhost bin]# all output going to: /data/shard1_2/shard1_2.log  
  5. [root@localhost bin]#  
    在Server C 上
[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017  
  2. --dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork  
  3. all output going to: /data/shard1_3/shard1_3.log  
  4. forked process: 18768  
  5. [root@localhost bin]#  
    用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:
[plain]  view plain copy
  1. [root@localhost bin]# ./mongo --port 27017  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: 127.0.0.1:27017/test  
  4. > config = {_id: 'shard1', members: [  
  5. ... {_id: 0, host: '192.168.3.231:27017'},  
  6. ... {_id: 1, host: '192.168.3.232:27017'},  
  7. ... {_id: 2, host: '192.168.3.233:27017'}]  
  8. ... }  
  9. ……  
  10. > rs.initiate(config)  
  11. {  
  12. "info" : "Config now saved locally. Should come online in about a minute.",  
  13. "ok" : 1  
  14. }  
    (2)配置shard2所用到的Replica Sets

       在server A上

[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018  
  2. --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork  
  3. all output going to: /data/shard2_1/shard2_1.log  
  4. [root@localhost bin]# forked process: 18993  
  5. [root@localhost bin]#  
       在server B上
[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018  
  2. --dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork  
  3. all output going to: /data/shard2_2/shard2_2.log  
  4. forked process: 18923  
  5. [root@localhost bin]#  
       在Server C上
[plain]  view plain copy
  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018  
  2. --dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork  
  3. [root@localhost bin]# all output going to: /data/shard2_3/shard2_3.log  
  4. forked process: 18824  
  5. [root@localhost bin]#  

       用mongo 连接其中一台机器的27018 端口的mongod,初始化Replica Sets “shard2”,执行:

[plain]  view plain copy
  1. [root@localhost bin]# ./mongo --port 27018  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: 127.0.0.1:27018/test  
  4. > config = {_id: 'shard2', members: [  
  5. ... {_id: 0, host: '192.168.3.231:27018'},  
  6. ... {_id: 1, host: '192.168.3.232:27018'},  
  7. ... {_id: 2, host: '192.168.3.233:27018'}]  
  8. ... }  
  9. ……  
  10. > rs.initiate(config)  
  11. {  
  12. "info" : "Config now saved locally. Should come online in about a minute.",  
  13. "ok" : 1  
  14.   
  15. db.runCommand({ enablesharding:"test" })  
  16. db.runCommand({ shardcollection: "test.users", key: { _id:1 }})  
  17.   
  18. }  
  (3)配置3 台Config Server
    在Server A、B、C上执行:
[plain]  view plain copy
  1. /Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath  
  2. /data/config/config.log --logappend --fork  
   (4)配置3台Route Process
    在Server A、B、C上执行: 
[plain]  view plain copy
  1. /Apps/mongo/bin/mongos --configdb  
  2. 192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1  
  3. --logpath /data/mongos.log --logappend --fork  
    (5)配置Shard Cluster
     连接到其中一台机器的端口30000 的mongos 进程,并切换到admin 数据库做以下配置
[plain]  view plain copy
  1. [root@localhost bin]# ./mongo --port 30000  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: 127.0.0.1:30000/test  
  4. > use admin  
  5. switched to db admin  
  6. >db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:  
  7. 27017"});  
  8. { "shardAdded" : "shard1", "ok" : 1 }  
  9. >db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:  
  10. 27018"});  
  11. { "shardAdded" : "shard2", "ok" : 1 }  
  12. >  
    激活数据库及集合的分片 
[plain]  view plain copy
  1. db.runCommand({ enablesharding:"test" })  
  2. db.runCommand({ shardcollection: "test.users", key: { _id:1 }})  
    (6)验证Sharding正常工作
    连接到其中一台机器的端口30000 的mongos 进程,并切换到test 数据库,以便添加测试数据 
[plain]  view plain copy
  1. use test  
  2. for(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});  
  3. db.users.stats()  
  4. {  
  5. "sharded" : true,  
  6. "ns" : "test.users",  
  7. "count" : 200000,  
  8. "size" : 25600384,  
  9. "avgObjSize" : 128,  
  10. "storageSize" : 44509696,  
  11. "nindexes" : 2,  
  12. "nchunks" : 15,  
  13. "shards" : {  
  14. "shard0000" : {  
  15. ……  
  16. },  
  17. "shard0001" : {  
  18. ……  
  19. }  
  20. },  
  21. "ok" : 1  
  22. }  
    可以看到Sharding搭建成功了,跟我们期望的结果一致,至此我们就将Replica Sets与Sharding结合的架构也学习完毕了!

你可能感兴趣的:(mongodb,数据库,NoSQL,nosql数据库)