前面介紹了如何搭建replica set ,本文介紹如何向replica set 添加新節點和删除老節點。
向已有的replica set 添加新成員是很簡單的,<b>新加的成員可以是空的</b>,當然也<b>可以是從其他節點的資料拷貝</b>,下面介紹一下如何添加新成員:
<b>添加新成員時,必須在新的機器上先起來一個mongodb服務,必須指出要加入的replica set 叢集的名字基本文法如下(其他是預設的),然後再在replica set的master 庫上執行添加操作,</b>
./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replset myset
當然也可以使用下面的指令,将輸出日志記錄到/opt/mongodata/r1/rac3.27017.log 裡面!
[mongodb@rac3 bin]$<b>./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replset myset -logpath=/opt/mongodata/r1/rac3.27017.log -logappend &</b>
<b>在主節點上執行添加操作</b>
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
mongodb shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
primary><b> rs.status();</b>
{
"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",
"uptime" : 63887,
"_id" : 2,
"name" : "10.250.7.220:27020",
"state" : 1,
"statestr" : "primary",
"self" : true
}
],
"ok" : 1
}
primary> <b>rs.add("10.250.7.241:27017");</b>
{ "ok" : 1 } --添加成功
添加之後,mongodb 會自動執行新加節點的恢複操作,是以第一次查詢rs.status()時候,新加節點的資訊為空,等待恢複完成之後再次查詢就有了基本資訊了。
primary> rs.status();
....省略....
"t" : 1320128045000,
"optimedate" : isodate("2011-11-01t06:14:05z"),
"_id" : 3,
"name" : "10.250.7.241:27017",
"health" : 0,
"state" : 8,
"statestr" : "(not reachable/healthy)",
"uptime" : 0,
"t" : 0,
"i" : 0
"optimedate" : isodate("1970-01-01t00:00:00z"),
"lastheartbeat" : isodate("2011-11-01t06:14:07z"),
"pingms" : 0,
"errmsg" : "still initializing"
"date" : isodate("2011-11-01t06:16:08z"),
.....省略....
"uptime" : 117,
"lastheartbeat" : isodate("2011-11-01t06:16:07z"),
primary>
<b>note:</b>這裡隻是測試了使用空庫來添加新節點的,當然也可以從其他節點拷貝<b>資料檔案</b>過來,這樣可以更快的和主庫進行同步操作,進而節省恢複時間!
<b>如何</b><b>删除一個節點:</b>
<b>隻需在主節點執行rs.remove()操作</b>
primary> <b>rs.remove("10.250.7.241:27017");</b>
{ "ok" : 1 }
<b>主節點将rac3 删除之後,如果退出再次進入rac3上的mongodb,會發現提示符變為 startup,已經停止應用日志但是不可寫入:</b>
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27017
connecting to: 127.0.0.1:27017/test
startup> rs.status();
"date" : isodate("2011-11-01t06:40:23z"),
"mystate" : 0,
"syncingto" : "10.250.7.220:27020",
"state" : 0,
<b>"statestr" : "startup", --啟動狀态,但是還是 myset 中的一個節點。</b>
"t" : 1320129036000,
"optimedate" : isodate("2011-11-01t06:30:36z"),
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!"});
<b>not master</b>
startup> exit
bye
<b>想重新使用此節點必須關閉mongodb服務,</b>
startup> use admin
switched to db admin
startup> <b>db.shutdownserver();</b>
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
<b>重新登入資料庫:從replica set 脫離後,從主庫應用的資料依然可用。</b>
>
> use test
> <b>db.yql.insert({val:"this is a message on rac3:27017 which is drop by rac4’s primary!"});</b>
> 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!" }