ITeye博客

搭建Mongodb集群:分片Sharding+副本集Replica Set

sblig · 2014-05-26 15:00 · mongodb · 编程语言 · Markdown

架构图如下: 客户请求: client 路由:Mongs1 Mongs2 Mongs3 40000 40100 402000 配置: config1 config2 config3 config:30000 30100 30200 副本集: rs1: 27017 27117 27217 rs2: 27018 27118 27218 rs3: 27019 27119 27219

添加windows服务形式启动: rs1: sc.exe create MongoDB0 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb0.cfg"" DisplayName= "MongoDB0" start= "auto" sc.exe create MongoDB1 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb1.cfg"" DisplayName= "MongoDB1" start= "auto" sc.exe create MongoDB2 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb2.cfg"" DisplayName= "MongoDB2" start= "auto" sc.exe create config1 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\config1.cfg"" DisplayName= "config1" start= "auto"

rs2: sc.exe create MongoDB3 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb3.cfg"" DisplayName= "MongoDB3" start= "auto" sc.exe create MongoDB4 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb4.cfg"" DisplayName= "MongoDB4" start= "auto" sc.exe create MongoDB5 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb5.cfg"" DisplayName= "MongoDB5" start= "auto" sc.exe create config2 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\config2.cfg"" DisplayName= "config2" start= "auto"

rs3: sc.exe create MongoDB6 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb6.cfg"" DisplayName= "MongoDB6" start= "auto" sc.exe create MongoDB7 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb7.cfg"" DisplayName= "MongoDB7" start= "auto" sc.exe create MongoDB8 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\mongodb8.cfg"" DisplayName= "MongoDB8" start= "auto" sc.exe create config3 binPath= ""D:\mongodb\bin\mongod.exe" --service --config="D:\mongodb\db\config3.cfg"" DisplayName= "config3" start= "auto"

net start MongoDB0 net start MongoDB1 net start MongoDB2 net start MongoDB3 net start MongoDB4 net start MongoDB5 net start MongoDB6 net start MongoDB7 net start MongoDB8 net start config1 net start config2 net start config3

评论(2)

  1. #2 楼 · sblig · 2014-05-26 15:25

    配置分片:

    mongo -port 27017 config = {_id:'rs1',members:};

    rs.initiate(config) rs.status()

    mongo -port 27018 config = {_id:'rs2',members:};

    rs.initiate(config) rs.status()

    mongo -port 27019 config = {_id:'rs3',members:};

    rs.initiate(config) rs.status()

  2. #1 楼 · sblig · 2014-05-26 15:24

    配置路由:

    mongs: 40000 40100 40200

    sc.exe create mongs1 binPath= ""D:\mongodb\bin\mongos.exe" --service --config="D:\mongodb\db\mongs1.cfg"" DisplayName= "mongs1" start= "auto" sc.exe create mongs2 binPath= ""D:\mongodb\bin\mongos.exe" --service --config="D:\mongodb\db\mongs2.cfg"" DisplayName= "mongs2" start= "auto" sc.exe create mongs3 binPath= ""D:\mongodb\bin\mongos.exe" --service --config="D:\mongodb\db\mongs3.cfg"" DisplayName= "mongs3" start= "auto"

    net start mongs1 net start mongs2 net start mongs3

    添加分片 1连接任意一台,其他无需这样操作: /usr/local/mongodb/bin/mongo --port 40000 use admin db.runCommand({addshard:'rs1/10.16.122.15:27017,10.16.122.15:27117,10.16.122.15:27217'}) db.runCommand({addshard:'rs2/10.16.122.15:27018,10.16.122.15:27118,10.16.122.15:27218'}) db.runCommand({addshard:'rs3/10.16.122.15:27019,10.16.122.15:27119,10.16.122.15:27219'})

    db.runCommand({ listshards:1 })

    通过执行以下命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard, 一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上, 但一个collection仍旧存放在同一个shard上,要使单个collection也分片, 还需单独对collection作些操作 db.runCommand({enablesharding:"test"});

    要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作 db.runCommand( { shardcollection : "test.c1",key : {id: 1},unique : true } )

    use test

    for(var i=1;i<=2000003;i++) db.c1.save({id:i,value1:"1234567890",value2:"1234567890",value3:"1234567890",value4:"1234567890"});

评论功能维护中,当前仅展示历史评论。