cat /etc/hosts
192.168.109.129 mongo2 member3.com member4.com
192.168.109.130 mongo3 member5.com member6.com
192.168.109.128 mongo1 member1.com member2.com
创建分片集群
mongod --bind_ip 0.0.0.0 --replSet shard1 --dbpath /data/shard1 --logpath /data/shard1/mongod.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 0
.1
rs.initiate({
_id:"shard1",
"members":[
{
"_id":0,
"host":"member1.com:27010"
},
{
"_id":1,
"host":"member3.com:27010"
},
{
"_id":2,
"host":"member5.com:27010"
}
]
});
创建配置集群
mongod --bind_ip 0.0.0.0 --replSet config --dbpath /data/config/ --logpath /data/config/mongod.log --port 27019 --fork --configsvr --wiredTigerCacheSizeG
B 0.1
rs.initiate({
_id:"config",
"members":[
{
"_id":0,
"host":"member1.com:27019"
},
{
"_id":1,
"host":"member3.com:27019"
},
{
"_id":2,
"host":"member5.com:27019"
}
]
});
启动mongos集群
mongos --bind_ip 0.0.0.0 --logpath /data/mongos/mongos.log --port 20000 --fork --configdb config/member1.com:27019,member3.com:27019,member5.com:27019
mongo localhost:20000
添加分片集群
sh.addShard("shard1/member1.com:27010,member3.com:27010,member5.com:27010");
创建分片表.
sh.enableSharding('foo')
sh.shardCollection('foo.bar',{"_id":"hashed"})
插入测试数据
use foo
for (var i = 0;i<10000;i++){
db.bar.insert({i:i})
}
扩容新的shard2
mongod --bind_ip 0.0.0.0 --replSet shard2 --dbpath /data/shard2 --logpath /data/shard2/mongod.log --port 27011 --fork --shardsvr --wiredTigerCacheSizeGB 0.1
rs.initiate({
_id:"shard2",
"members":[
{
"_id":0,
"host":"member2.com:27011"
},
{
"_id":1,
"host":"member4.com:27011"
},
{
"_id":2,
"host":"member6.com:27011"
}
]
});
加入新的shard2
sh.addShard("shard2/member2.com:27011,member4.com:27011,member6.com:27011");
sh.status()