分片结构端口分布如下:
Shard Server 1:27020
Shard Server 2:27021
Shard Server 3:27022
Shard Server 4:27023
Config Server :27100
Route Process:40000
1.正确安装MongoDb
http://blog.youkuaiyun.com/u013298318/article/details/52248478
2.启动Shard Server
[root@100 /]# mkdir -p /www/mongoDB/shard/s0
[root@100 /]# mkdir -p /www/mongoDB/shard/s1
[root@100 /]# mkdir -p /www/mongoDB/shard/s2
[root@100 /]# mkdir -p /www/mongoDB/shard/s3
[root@100 /]# mkdir -p /www/mongoDB/shard/log
[root@100 /]# /usr/local/mongoDB/bin/mongod —shardsvr --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork
[root@100 /]# /usr/local/mongoDB/bin/mongod —shardsvr --port 27021 --dbpath=/www/mongoDB/shard/s1 --logpath=/www/mongoDB/shard/log/s1.log --logappend --fork
[root@100 /]# /usr/local/mongoDB/bin/mongod —shardsvr --port 27022 --dbpath=/www/mongoDB/shard/s2 --logpath=/www/mongoDB/shard/log/s2.log --logappend --fork
[root@100 /]# /usr/local/mongoDB/bin/mongod —shardsvr --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork
3.启动Config Server
[root@100 /]# mkdir -p /www/mongoDB/shard/config
[root@100 /]# /usr/local/mongoDB/bin/mongod --configsvr --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork
4.启动Route Process
/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500
5.配置Sharding
使用MongoDB Shell登录到mongos,添加Shard节点
[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000
MongoDB shell version: 2.0.7
connecting to: 127.0.0.1:40000/admin
mongos> db.runCommand({ addshard:"localhost:27020" })
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({ addshard:"localhost:27021" })
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({ addshard:"localhost:27022" })
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos> db.runCommand({ addshard:"localhost:27023" })
{ "shardAdded" : "shard0003", "ok" : 1 }
mongos> db.runCommand({ enablesharding:"shardtest" }) #设置分片存储的数据库
{ "ok" : 1 }
mongos> db.runCommand({ shardcollection: "shardtest.log", key: { id:1,time:1}})
{ "collectionsharded" : "shardtest.log", "ok" : 1 }
for(i=0;i<10000;i++){db.shardtest.userid.insert({"user_id":i})}
7.查看分片状态
use config
db.shards.find()
use shardtest
db.userid.stats()
db.printShardStatus()