上一篇 http://www.2cto.com/database/201201/116908.html
md01 10.0.0.11
md02 10.0.0.12
md03 10.0.0.14
 
2、启动三台机器的mongod实例
根据Replica Set、Sharding策略部署mongod。将两个sharding组部署到三台服务器上,每个sharding组有三个replica set成员。
#Server1:
 
mkdir -p /elain/data/mongodb/shard11
mkdir -p /elain/data/mongodb/shard21
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /elain/data/mongodb/shard11 --oplogSize 100 --logpath /elain/data/mongodb/shard11.log --logappend --fork --rest
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /elain/data/mongodb/shard21 --oplogSize 100 --logpath /elain/data/mongodb/shard21.log --logappend --fork --rest
#Server2:
 
mkdir -p /elain/data/mongodb/shard12/
mkdir -p /elain/data/mongodb/shard22/
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /elain/data/mongodb/shard12 --oplogSize 100 --logpath /elain/data/mongodb/shard12.log --logappend --fork --rest
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /elain/data/mongodb/shard22 --oplogSize 100 --logpath /elain/data/mongodb/shard22.log --logappend --fork --rest
#Server3:
 
mkdir -p /elain/data/mongodb/shard13/
mkdir -p /elain/data/mongodb/shard23/
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /elain/data/mongodb/shard13 --oplogSize 100 --logpath /elain/data/mongodb/shard13.log --logappend --fork --rest
/elain/apps/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /elain/data/mongodb/shard23 --oplogSize 100 --logpath /elain/data/mongodb/shard23.log --logappend --fork --rest
 
3、初始化Replica Set
通过命令行初始化两组Replica Set,通过mongo连接到一个mongod
 
/elain/apps/mongodb/bin/mongo 10.0.0.11:27017config = {_id: ''''''''shard1'''''''', members: [
{_id: 0, host: ''''''''10.0.0.11:27017''''''''},
{_id: 1, host: ''''''''10.0.0.12:27017''''''''},
{_id: 2, host: ''''''''10.0.0.14:27017''''''''}]};
 
rs.initiate(config);
 
/elain/apps/mongodb/bin/mongo 10.0.0.11:27018
 
config = {_id: ''''''''shard2'''''''', members: [
{_id: 0, host: ''''''''10.0.0.11:27018''''''''},
{_id: 1, host: ''''''''10.0.0.12:27018''''''''},
{_id: 2, host: ''''''''10.0.0.14:27018''''''''}]};
 
rs.initiate(config);
4、启动并配置三台Config Server
 
#Server1、2、3:
 
mkdir -p /elain/data/mongodb/config/
/elain/apps/mongodb/bin/mongod --configsvr --dbpath /elain/data/mongodb/config/ --port 20000 --logpath /elain/data/mongodb/config1.log --logappend --fork
5、部署并配置三台Routing Server
 
指定所有的config sever地址参数,chunkSize是分割数据时每块(Chunk)的单位大小
 
#Server1、2、3:
 
/elain/apps/mongodb/bin/mongos --configdb 10.0.0.11:20000,10.0.0.12:20000,10.0.0.14:20000 --port 30000 --chunkSize 100 --logpath /elain/data/mongodb/mongos.log --logappend --fork
6、命令行添加分片
连接到mongs服务器,并切换到admin
 
/elain/apps/mongodb/bin/mongo 10.0.0.11:30000/admin
 
db.runCommand( {


addshard : "shard1/10.0.0.11:27017,10.0.0.12:27017,10.0.0.14:27017",
name:"shard1",
maxsize:2048,
allowLocal:true } );
 
db.runCommand( {

addshard : "shard2/10.0.0.11:27018,10.0.0.12:27018,10.0.0.14:27018",
name:"shard2",
maxsize:2048,
allowLocal:true } );
 
db.runCommand( { listshards : 1 } );
如果列出(sharding)了以上二个你加的shards,表示shards已经配置成功
 
#激活分片
 
db.runCommand( { enablesharding : "elain" } );
要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:
 
db.runCommand( { shardcollection : “”,key : db.chujq.t1.stats();
{

"sharded" : true,
"flags" : 1,
"ns" : "elain.test",
"count" : 4058977,
"numExtents" : 34,
"size" : 675039816,
"storageSize" : 818757632,
"totalIndexSize" : 131854352,
"indexSizes" : {

"_id_" : 131854352
},
"avgObjSize" : 166.30786919955446,
"nindexes" : 1,
"nchunks" : 14,
"shards" : {

"shard1" : {

"ns" : "elain.test",
"count" : 1860365,
"size" : 309376352,
"avgObjSize" : 166.29873815084673,
"storageSize" : 408920064,
"numExtents" : 16,
"nindexes" : 1,
"lastExtentSize" : 77955072,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 60371584,
"indexSizes" : {

"_id_" : 60371584
},
"ok" : 1
},
"shard2" : {

"ns" : "elain.test",
"count" : 2198612,
"size" : 365663464,
"avgObjSize" : 166.31559547569103,
"storageSize" : 409837568,
"numExtents" : 18,
"nindexes" : 1,
"lastExtentSize" : 74846208,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 71482768,
"indexSizes" : {

"_id_" : 71482768
},
"ok" : 1
}
},
"ok" : 1
}
删除片操作
 
mongos> db.runCommand({"removeshard" : "10.0.0.11:27018"});
{

"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard2",
"ok" : 1
}
再执行,可看到removeshard的挪动进度
 
mongos> db.runCommand({"removeshard" : "10.0.0.11:27018"});
{

"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {

"chunks" : NumberLong(3),
"dbs" : NumberLong(0)
},
"ok" : 1
}
例:
 
/elain/apps/mongodb/bin/mongod --config /elain/data/mongodb/shard1/shard1.properties --rest