演示部署3个节点的Replica sets,每个节点部署在不同的服务器上。
ServerA,IP:192.168.2.251
ServerB,IP:192.168.2.252
ServerC,IP:192.168.2.253
[root@localhost ~]# mkdir -p /data/data/organization
[root@localhost ~]# mkdir -p /data/log
[root@localhost ~]# mkdir -p /data/key
[root@localhost ~]# echo "this is org replica set super secret key” > /data/key/organization.key
[root@localhost ~]# chmod 600 /data/key/organization.key
[root@localhost ~]# mongod --replSet org_rs --keyFile /data/key/organization.key --fork --port 27017 --dbpath /data/data/organization --logpath /data/log/organization.log --logappend
各参数说明:
replSet:指明明replica set的名字,本例取值org_rs。
keyFile:指明replica set的key路径,能保证各节点间的连通性。
fork:将命令放在后台运行。
port:指定监听端口。
dbpath:数据文件存储路径。
logpath:系统日志文件存放位置。
logappend:明确指定日志的写入模式是追加的。
[root@localhost data]# mongo --port 27017
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27017/test
> config_rs = {_id:'org_rs',members:[{_id:0,host:'192.168.2.251:27017'}]}
{
"_id" : "org_rs",
"members" : [
{
"_id" : 0,
"host" : "192.168.2.251:27017"
}
]
}
> rs.initiate(config_rs);
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
添加其他两个节点
org_rs:PRIMARY> rs.add('192.168.2.252:27017');
{ "ok" : 1 }
org_rs:PRIMARY> rs.add('192.168.2.253:27017');
{ "ok" : 1 }
查看replica set的状态
org_rs:PRIMARY> rs.status();
{
"set" : "org_rs",
"date" : ISODate("2013-06-28T15:26:46Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.2.251:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1335,
"optime" : {
"t" : 1372433115,
"i" : 1
},
"optimeDate" : ISODate("2013-06-28T15:25:15Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.2.252:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 100,
"optime" : {
"t" : 1372433115,
"i" : 1
},
"optimeDate" : ISODate("2013-06-28T15:25:15Z"),
"lastHeartbeat" : ISODate("2013-06-28T15:26:46Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"syncingTo" : "192.168.2.251:27017"
},
{
"_id" : 2,
"name" : "192.168.2.253:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 91,
"optime" : {
"t" : 1372433115,
"i" : 1
},
"optimeDate" : ISODate("2013-06-28T15:25:15Z"),
"lastHeartbeat" : ISODate("2013-06-28T15:26:45Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"syncingTo" : "192.168.2.251:27017"
}
],
"ok" : 1
}
至此各节点配置成功。
获取配置信息
org_rs:PRIMARY> cfg = rs.conf();
{
"_id" : "org_rs",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "192.168.2.251:27017"
},
{
"_id" : 1,
"host" : "192.168.2.252:27017"
},
{
"_id" : 2,
"host" : "192.168.2.253:27017"
}
]
}
设置0号节点的priority为3
org_rs:PRIMARY> cfg.members[0].priority = 3
3
设置1号节点的priority为
org_rs:PRIMARY> cfg.members[0].priority = 2
2
设置2号节点的priority为0
org_rs:PRIMARY> cfg.members[0].priority = 0
0
更新replica set的配置
org_rs:PRIMARY> rs.reconfig(cfg);
Replica set中只允许Primary拥有写操作,故所有写操作都是在Primary执行的。Primary还拥有读的权限。
Secondary默认是不能进行查询操作的,必须得设置。如在ServerB上执行如下命令
[root@localhost ~]# mongo -port 27017
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27017/test
org_rs:SECONDARY> show dbs
admin (empty)
local 2.0771484375GB
test 0.203125GB
org_rs:SECONDARY> show collections
Sat Jun 29 02:35:15.208 JavaScript execution failed: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:L128
org_rs:SECONDARY>
show collections 报"not master and slaveOk=false”错误说明这个库暂时不能执行查询操作。
为满足我的配置要求,我需将其设为可查询的,以分担主库的压力,执行如下命令即可
org_rs:SECONDARY> db.getMongo().setSlaveOk()
现在再执行show collections看看。
在Primary(ServerA)上插入一条数据。
org_rs:PRIMARY> db.c1.insert({age:30})
org_rs:PRIMARY> db.c1.find()
{ "_id" : ObjectId("51cdbf00a540cd543e5cb316"), "age" : 30 }
在Secondary(ServerB)上进行查询
org_rs:SECONDARY> db.c1.find()
{ "_id" : ObjectId("51cdbf00a540cd543e5cb316"), "age" : 30 }
OK,部署工作已经完成。
另外,可以在Primary执行db.printSlaveReplicationInfo()命令,查看replica set的同步状态。
org_rs:PRIMARY> db.printSlaveReplicationInfo()
source: 192.168.2.252:27017
syncedTo: Sat Jun 29 2013 00:51:12 GMT+0800 (CST)
= 7055 secs ago (1.96hrs)
source: 192.168.2.253:27017
syncedTo: Sat Jun 29 2013 00:51:12 GMT+0800 (CST)
= 7055 secs ago (1.96hrs)
org_rs:PRIMARY>
source是secondary的ip及端口,syncedTo是同步的情况和最后一次的同步时间。
本人是新手,还望各位多多指教。