日期:2014-05-16  浏览次数:21139 次

MongoDB学习整理之Sharding

     环境准备:

            

   

    启动Shard服务器:
        /app/mongo/mongodb/bin/mongod --shardsvr --port 20000 --dbpath=/app/mongo/mongodb/data/shard/s0
                                        --fork --logpath=/app/mongo/mongodb/data/shard/log/s0.log --directoryperdb 
        /app/mongo/mongodb/bin/mongod --shardsvr --port 20001 --dbpath=/app/mongo/mongodb/data/shard/s1
                                --fork --logpath=/app/mongo/mongodb/data/shard/log/s1.log  --directoryperdb

    启动Config服务器:
        /app/mongo/mongodb/bin/mongod --configsvr --port 30000 --dbpath=/app/mongo/mongodb/data/shard/config
                                         --fork --logpath=/app/mongo/mongodb/data/shard/log/config.log  --directoryperdb

    启动Route process
        /app/mongo/mongodb/bin/mongos --port 40000 --configdb localhost:30000
                        --fork --logpath=/app/mongo/mongodb/data/shard/log/route.log --chunkSize 1

 

    配置Sharding
        登录路由控制器上添加Shard节点,进行配置Sharding
                /app/mongo/mongodb/bin/mongo admin --port 40000           连接路由
                >db.runCommand({addshard:"localhost:20000"})               添加本地端口20000的Shard Server实例
                >db.runCommand({addshard:"localhost:20001"})
                >db.runCommand({enablesharding:"test"})                    设置在test数据库上执行分片
                >db.runCommand({shardcollection:"test.users",key:{_id:1}}) 设置在test数据库的users表上分片,同时指明id为shard key
        验证Sharding
                >use test
                >for(var i=1;i<500000;i++){db.users.insert({age:i,name:"liangzhangping",addr:"beijing",country:"china"})}
                >db.users.stats()