# cd /mongodb/mongodb-linux-x86_64-2.4.8/bin
# ./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard13 –oplogSize 100 –logpath/mongodb/data/shard13.log –logappend –fork
3.初始化replica set
mongo1:
# ./mongo
> config = {_id: ’shard1′, members: [
{_id: 0, host: '10.0.1.3:27017'},
{_id: 1, host: '10.0.1.4:27017'},
{_id: 2, host: '10.0.1.5:27017'}]
}
> rs.initiate(config);
4.配置3台config server
mongo1:
# mkdir -p /mongodb/data/config
# ./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork ######备注config server也需要dbpath#######
mongo2:
# mkdir -p /mongodb/data/config
# ./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork mongo3:
# mkdir -p /mongodb/data/config
# ./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork 5.配置mongos server
mongo1:
# ./mongos –configdb 10.0.1.3:20000,10.0.1.4:20000,10.0.1.5:20000 –port 30000 –chunkSize 5 –logpath /mongodb/data/mongos.log –logappend –fork
######备注mongs不需要dbpath#####
mongo2:
# ./mongos –configdb 10.0.1.3:20000,10.0.1.4:20000,10.0.1.5:20000 –port 30000 –chunkSize 5 –logpath /mongodb/data/mongos.log –logappend –fork
mongo3:
# ./mongos –configdb 10.0.1.3:20000,10.0.1.4:20000,10.0.1.5:20000 –port 30000 –chunkSize 5 –logpath /mongodb/data/mongos.log –logappend –fork
6.配置分片集群
mongo1:
# ./mongo 10.0.1.3:30000/admin
>db
admin
加入shards
>db.runCommand( { addshard : “shard1/10.0.1.3:27017,10.0.1.4:27017,10.0.1.5:27017″,name:”s1″,maxsize:20480} );
{ "shardAdded" : "s1", "ok" : 1 }
列出shards
>db.runCommand( { listshards : 1 } )
激活数据库分片
> db.runCommand( { enablesharding : “” } );
注:dbname不是现在已存在的数据库;
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作
Collecton分片
要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:
> db.runCommand( { shardcollection : “”,key : });
注:
a. 分片的collection系统会自动创建一个索引(也可用户提前创建好)
b. 分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许
One note: a sharded collection can have only one unique index, which must exist on the shard key. No other unique indexes can exist on the collection.