序号 角色 ip地址 备注
1 服务器 192.168.8.81 (config server)
2 路由 192.168.8.82 (mongos)
3 分片节点 192.168.8.83 (shard)
4 分片节点 192.168.8.84 (shard)
fork = true
pidfilepath = /var/run/mongodb/mongod.pid
logpath = /var/log/mongodb/mongod.log
unixSocketPrefix = /var/run/mongodb
dbpath = /var/lib/mongodb
port=27017
configsvr=true
b. 启动服务:
systemctl start mongod
fork = true
pidfilepath = /var/run/mongodb/mongod.pid
logpath = /var/log/mongodb/mongod.log
unixSocketPrefix = /var/run/mongodb
#dbpath = /var/lib/mongodb (注释掉数据库)
port=27017
configdb=192.168.8.81:27017
b. 删除数据库中的文件:
rm -rf /var/lib/mongodb/*
c. 启动服务:
mongos -f /etc/mongod.conf
fork = true
pidfilepath = /var/run/mongodb/mongod.pid
logpath = /var/log/mongodb/mongod.log
unixSocketPrefix = /var/run/mongodb
dbpath = /var/lib/mongodb
b. 启动服务:
systemctl start mongod
mongo
a-2. 添加分片机:
sh.addShard("192.168.8.83:27017")
sh.addShard("192.168.8.84:27017")
a-3. 查看分片机状态:
sh.status()
a-4. 添加分片数据库:
sh.enableSharding("testdb")
a-5. 添加索引:
sh.shardCollection("testdb.user",{name:1,age:1})
a-6. 添加测试数据:
for (i = 1;i < 100000; i++)
{
db.user.insert({name:‘user‘+i,age:(i%150),addr:‘#‘+i}
}
b. 分片机(192.168.8.83/84):
b-1. 进入终端:
mongo
b-2. 查看经过分片处理的数据:
use testdb
db.user.find()
原文地址:http://blog.51cto.com/12173069/2093623