MongoDB学习整理之Replica Sets+Sharding
MongoDB学习整理之Replica Sets+Sharding
环境准备:
配置Replica Sets
1、配置shard1所用到Replica Set1
Server A: www.zzzyk.com
/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'}]}
{ www.zzzyk.com
"_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 www.zzzyk.com
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'},
&n