环境准备:
配置Replica Sets
1、配置shard1所用到Replica Set1
Server A:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /app/mongo/data/shard1_1 --logpath /app/mongo/data/shard1_1/shard1_1.log --logappend --fork
Server B:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /app/mongo/data/shard1_2 --logpath /app/mongo/data/shard1_2/shard1_2.log --logappend --fork
Server C:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /app/mongo/data/shard1_3 --logpath /app/mongo/data/shard1_3/shard1_3.log --logappend --fork
用mongo连接其中一台机器的27017端口的mongod,初始化Replica Set 1,如下面代码所示:
[mongodb@Hadoop2 bin]$ /app/mongo/mongodb/bin/mongo --port 27017
MongoDB shell version: 2.0.5
connecting to: 127.0.0.1:27017/test
> config ={_id:'shard1',members:[{_id:0,host:'132.42.33.212:27017'},{_id:1,host:'132.42.33.213:27017'},{_id:2,host:'132.42.33.214:27017'}]}
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "132.42.33.212:27017"
},
{
"_id" : 1,
"host" : "132.42.33.213:27017"
},
{
"_id" : 2,
"host" : "132.42.33.214:27017"
}
]
}
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
2、配置shard2所用到的Replica Set2
Server A:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /app/mongo/data/shard2_1 --logpath /app/mongo/data/shard2_1/shard2_1.log --logappend --fork
Server B:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /app/mongo/data/shard2_2 --logpath /app/mongo/data/shard2_2/shard2_2.log --logappend --fork
Server C:
/app/mongo/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /app/mongo/data/shard2_3 --logpath /app/mongo/data/shard2_3/shard2_3.log --logappend --fork
用mongo连接其中一台机器的27017端口的mongod,初始化Replica Set 1,如下面代码所示:
[mongodb@Hadoop2 bin]$ /app/mongo/mongodb/bin/mongo --port 27018
MongoDB shell version: 2.0.5
connecting to: 127.0.0.1:27018/test
> config ={_id:'shard2',members:[
... {_id:0,host:'132.42.33.212:27018'},
... {_id:1,host:'132.42.33.213:27018'},
... {_id:2,host:'132.42.33.214:27018'}]
... }
{
"_id" : "shard2",
"members" : [
{
"_id" : 0,
"host" : "132.42.33.212:27018"
},
{
"_id" : 1,
"host" : "132.42.33.213:27018"
},
{
"_id" : 2,
"host" : "132.42.33.214:27018"
}
]
}
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
3、配置3台config server
/app/mongo/mongodb/bin/mongod --configsvr --dbpath /app/mongo/data/config --port 20000 --logpath /app/mongo/data/config/config.log --logappend --fork
4、配置3台Route Process
/app/mongo/mongodb/bin/mongos --configdb 132.42.33.212:20000,132.42.33.213:20000,132.42.33.214:20000 --port 30000 --chunkSize 1 --logpath /app/mongo/data/mongos.log --logappend --fork
5、配置Shard Cluster
用mongo连接其中一台机器的30000端口的mongod,并切换为admin数据库,进行配置Sharding环境,如下面的代码所示:
[mongodb@Hadoop4 mongo]$ /app/mongo/mongodb/bin/mongo --port 30000
>use admin
>db.runCommand({addshard:"shard1/132.42.33.212:27017,shard1/132.42.33.213:27017,shard1/132.42.33.214:27017"})
>db.runCommand({addshard:"shard2/132.42.33.212:27018,shard1/132.42.33.213:27018,shard1/132.42.33.214:27018"})
>db.runCommand({enablesharding:"test"})
>db.runCommand({shardcollection:"test.users",key:{_id:1}})
6、验证Sharding
连接其中一台端口30000对应的mongos进程,并切换到test数据库
use test
for(var i=1;i<=200000;i++) {db.users.insert({id:i,addr_1:"beijing",addr_2:"shanghai"})}
db.users.stats()