mongodb分片(sharding)搭建应用及管理
Posted lhdz_bj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb分片(sharding)搭建应用及管理相关的知识,希望对你有一定的参考价值。
1.启动shard server
# mkdir -p /data/shard/s0
# mkdir -p /data/shard/s1
# mkdir -p /data/shard/log
# cd /usr/local/mongodb/bin
# ./mongod --shardsvr --port 20000 --dbpath /data/shard/s0 --fork --logpath /data/shard/log/s0.log --directoryperdb
# ./mongod --shardsvr --port 20001 --dbpath /data/shard/s1 --fork --logpath /data/shard/log/s1.log --directoryperdb
2.启动config server
# mkdir -p /data/shard/config
# cd /usr/local/mongodb/bin
# ./mongod --configsvr --port 30000 --dbpath /data/shard/config --fork --logpath /data/shard/log/config.log --directoryperdb
3.启动route process
# /usr/local/mongodb/bin/mongos --prot 40000 --configdb localhost:30000 --fork --logpath /data/shard/log/route.log --chunkSize 1
4.配置sharding
# /usr/local/mongodb/bin/mongo admin --port 40000
> db.runCommand({addshard:"localhost:20000"})
> db.runCommand({addshard:"localhost:20001"})
> db.runCommand({enablesharding:"test"})
> db.runCommand({shardcollection:"test.users",key:{_id:1}})
5.验证sharding
# /usr/local/mongodb/bin/mongo admin --port 40000
> use test
> for(var i=1;i<=500000;i++) db.users.insert({age:i,name:"smith",addr:"newyork",country:"america"})
> db.users.stats()
> exit
# ll /data/shard/s0/test
# ll /data/shard/s1/test
6.维护sharding
1)列出所有shard server
# /usr/local/mongodb/bin/mongo admin --port 40000
> db.runCommand({listshards:1})
2)查看sharding信息
# /usr/local/mongodb/bin/mongo admin --port 40000
> printShardingStatus()
3)查看特定实例是否为shard server
# /usr/local/mongodb/bin/mongo admin --port 20001
> db.runCommand({isdbgrid:1})
4)对现有表进行sharding
# /usr/local/mongodb/bin/mongo admin --port 40000
> use test
> db.users_2.stats()
> use admin
> db.runCommand({shardcollection:"test.users_2",key{_id:1}})
> use test
> db.users_2.stats()
5)新增shard server
# mkdir /data/shard/s2
# /usr/local/mongodb/bin/mongod --shardsvt --port 20002 --dbpath /data/shard/s2 --fork --logpath /data/shard/log/s2.log --directoryperdb
# /usr/local/mongodb/bin/mongo admin --port 40000
> db.runCommand({addshard:"localhost:20002"})
> use test
> db.users_2.stats()
6)移除shard server
# /usr/local/mongodb/bin/mongo admin --port 40000
>use admin
> db.runCommand({removeshard:"localhost:20002"})
--可反复运行该命令,直到shard server服务器移除,期间,state值:started-->ongoing,直到最后errmsg:db assertion failure,表示该shard server移除成功.
> use test
> db.users_2.stats()
以上是关于mongodb分片(sharding)搭建应用及管理的主要内容,如果未能解决你的问题,请参考以下文章
搭建 MongoDB分片(sharding) / 分区 / 集群环境
搭建 MongoDB分片(sharding) / 分区 / 集群环境
笔记14:MongoDB分片集群Shard Cluster搭建