前面介绍了如何搭建replica set ,本文介绍如何向replica set 添加新节点和删除老节点。
向已有的replica set 添加新成员是很简单的,新加的成员可以是空的,当然也可以是从其他节点的数据拷贝,下面介绍一下如何添加新成员:
添加新成员时,必须在新的机器上先起来一个mongodb服务,必须指出要加入的replica set 集群的名字基本语法如下(其他是默认的),然后再在replica set的master 库上执行添加操作,
./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replSet myset
当然也可以使用下面的命令,将输出日志记录到/opt/mongodata/r1/rac3.27017.log 里面!
[mongodb@rac3 bin]$./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replSet myset -logpath=/opt/mongodata/r1/rac3.27017.log -logappend &
在主节点上执行添加操作
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
PRIMARY> rs.status();
{
"set" : "myset",
"date" : ISODate("2011-11-01T06:13:01Z"),
"myState" : 1,
"syncingTo" : "10.250.7.220:27018",
"members" : [
{
"_id" : 0,
"name" : "10.250.7.220:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 62668,
"optime" : {
"t" : 1320066230000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T13:03:50Z"),
"lastHeartbeat" : ISODate("2011-11-01T06:13:01Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "10.250.7.220:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 63887,
"optime" : {
"t" : 1320066230000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T13:03:50Z"),
"lastHeartbeat" : ISODate("2011-11-01T06:13:01Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "10.250.7.220:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1320066230000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T13:03:50Z"),
"self" : true
}
],
"ok" : 1
}
PRIMARY> rs.add("10.250.7.241:27017");
{ "ok" : 1 } --添加成功
添加之后,mongodb 会自动执行新加节点的恢复操作,所以第一次查询rs.status()时候,新加节点的信息为空,等待恢复完成之后再次查询就有了基本信息了。
PRIMARY> rs.status();
{
....省略....
{
"_id" : 2,
"name" : "10.250.7.220:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1320128045000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T06:14:05Z"),
"self" : true
},
{
"_id" : 3,
"name" : "10.250.7.241:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"t" : 0,
"i" : 0
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2011-11-01T06:14:07Z"),
"pingMs" : 0,
"errmsg" : "still initializing"
}
],
"ok" : 1
}
PRIMARY> rs.status();
{
"set" : "myset",
"date" : ISODate("2011-11-01T06:16:08Z"),
"myState" : 1,
"syncingTo" : "10.250.7.220:27018",
"members" : [
.....省略....
{
"_id" : 3,
"name" : "10.250.7.241:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 117,
"optime" : {
"t" : 1320128045000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T06:14:05Z"),
"lastHeartbeat" : ISODate("2011-11-01T06:16:07Z"),
"pingMs" : 0
}
],
"ok" : 1
}
PRIMARY>
NOTE:这里只是测试了使用空库来添加新节点的,当然也可以从其他节点拷贝数据文件过来,这样可以更快的和主库进行同步操作,从而节省恢复时间!
如何删除一个节点:
只需在主节点执行rs.remove()操作
PRIMARY> rs.remove("10.250.7.241:27017");
{ "ok" : 1 }
PRIMARY>
主节点将rac3 删除之后,如果退出再次进入rac3上的mongodb,会发现提示符变为 STARTUP,已经停止应用日志但是不可写入:
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27017
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27017/test
STARTUP> rs.status();
{
"set" : "myset",
"date" : ISODate("2011-11-01T06:40:23Z"),
"myState" : 0,
"syncingTo" : "10.250.7.220:27020",
"members" : [
{
"_id" : 3,
"name" : "10.250.7.241:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP", --启动状态,但是还是 myset 中的一个节点。
"optime" : {
"t" : 1320129036000,
"i" : 1
},
"optimeDate" : ISODate("2011-11-01T06:30:36Z"),
"self" : true
}
],
"ok" : 1
}
STARTUP>
STARTUP> show dbs;
local 1.203125GB
test 0.203125GB
STARTUP> use test;
switched to db test
STARTUP> db.yql.insert({val:"this is a message on rac3:27017 which is drop by rac4’s primary!"});
not master
STARTUP> exit
bye
想重新使用此节点必须关闭mongodb服务,
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27017
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27017/test
STARTUP> use admin
switched to db admin
STARTUP>
STARTUP> db.shutdownServer();
Tue Nov 1 14:45:44 DBClientCursor::init call() failed
Tue Nov 1 14:45:44 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27017
server should be down...
Tue Nov 1 14:45:44 trying reconnect to 127.0.0.1:27017
Tue Nov 1 14:45:44 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
Tue Nov 1 14:45:44 Error: error doing query: unknown shell/collection.js:150
> exit
bye
重新登录数据库:从replica set 脱离后,从主库应用的数据依然可用。
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27017
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27017/test
>
>
> use test
switched to db test
> db.yql.insert({val:"this is a message on rac3:27017 which is drop by rac4’s primary!"});
> db.yql.find();
{ "_id" : ObjectId("4eae9cb269faf1a7cde0811c"), "val" : "this is a message on 27020 primary !" } --从主库复制的数据。
{ "_id" : ObjectId("4eaf96ebc048c68f812e6de4"), "val" : "this is a message on rac3:27017 which is drop by rac4’s primary!" }