当前位置:数据库 > mongodb >>

MongoDB学习整理之Sharding

   环境准备:

            

    配置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()
                    

   
        维护Sharding信息
                1、列出所有的Shard Server
                        >db.runCommand({listshards:1})
                2、查看Sharding信息
                        >printShardingStatus()
                3、判断是否是Sharding
                        >db.runCommand({isdbgrid:1})
                4、对现有的表执行Sharding
                        >db.runCommand({shardcollection:"test.users",key:{_id:1}})
       
        新增Shard Server
                1、创建新增Shard Server数据目录
                        mkdir -p /app/mongo/mongodb/data/shard/s2
                2、启动Shard Server
                        /app/mongo/mongodb/bin/mongod --shardsvr --port 20002 --dbpath=/app/mongo/mongodb/data/shard/s2 --fork --logpath=/app/mongo/mongodb/data/shard/log/s2.log  --directoryperdb
                3、添加Shard Server到Sharding环
                        >db.runCommand({addshard:"localhost:20002"})

        移除Shard Server
                >use admin
                >db.runCommand({"removeshard":"localhost:20002"})
作者:xiaoping8411

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,