MongoDB实现分片存储(Shell)

MongoDB分片存储结构如下图所示:
Mongo分片存储

#启动配置服务
mongod --dbpath=D:\Database\MongoDB3\slice\config --port 27018
#启动mongos
mongos --port 27017 --configdb=localhost:27018
#启动mongo分片服务
mongod --dbpath=D:\Database\MongoDB3\slice\slice1 --port 27019
mongod --dbpath=D:\Database\MongoDB3\slice\slice2 --port 27020
mongo localhost:27017
#增加节点
sh.addShard("localhost:27019")
sh.addShard("localhost:27020")
#开启分片
sh.enableSharding("test")
#配置collection
sh.shardCollection("test.patient",{"patid":1},true)
#查看状态
sh.status()
#批量插入数据
#单独连一个mongo查一下试试:)
for(var i=0;i<10000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age,address:{"city":"shanghai","street":"huaihai road"}});
}

Comments are closed.