您好,登錄后才能下訂單哦!
上篇文章已經詳細的介紹了replica set的搭建過程,這篇文章主要對故障的自動切換、節點的增、刪、改進行介紹
http://1413570.blog.51cto.com/1403570/1337619 mongodb 的replica set的搭建過程
模擬示列一:
res1:PRIMARY> rs.conf();
{
"_id" : "res1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "192.168.1.248:27017",
"priority" : 2
},
{
"_id" : 1,
"host" : "192.168.1.247:27018",
"priority" : 0
},
{
"_id" : 2,
"host" : "192.168.1.250:27019"
}
]
}
看出,primary 是host:192.168.1.248,因為priority 屬性大,其次是 host:192.168.1.250,當host 192.168.1.248宕機時,就有host 192.168.1.250 作為primary ,主庫
假設 host 192.168.1.248 停掉mongodb主進程
ps -ef | grep mongodb
kill 8665
盡量不要使用kill -9 這個可能會導致mongo數據文件的損壞
OK,現在其他兩臺server的日志已經提示
Fri Dec 6 16:36:10.522 [rsHealthPoll] couldn't connect to 192.168.1.248:27017: couldn't connect to server 192.168.1.248:27017
之后有host 192.168.20.250 來作為primary
Fri Dec 6 16:36:40.707 [conn248] end connection 192.168.1.250:46500 (1 connection now open)
Fri Dec 6 16:36:40.708 [initandlisten] connection accepted from 192.168.1.250:46592 #249 (2 connections now open)
Fri Dec 6 16:36:40.710 [conn249] authenticate db: local { authenticate: 1, nonce: "f70f5a8aea558178", user: "__system", key: "19fb73382ae940816c685b2561b0a76e" }
現在通過mongodb的shell ,登錄
[root@anenjoy ~]# /usr/local/mongodb/bin/mongo --port 27019
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:27019/test
res1:PRIMARY>
就會顯示primary
之后通過rs.ststus();
res1:PRIMARY> rs.status();
{
"set" : "res1",
"date" : ISODate("2013-12-06T08:44:01Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.1.248:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(1386118280, 1),
"optimeDate" : ISODate("2013-12-04T00:51:20Z"),
"lastHeartbeat" : ISODate("2013-12-06T08:44:00Z"),
"lastHeartbeatRecv" : ISODate("2013-12-06T08:41:32Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "192.168.1.247:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3790,
"optime" : Timestamp(1386118280, 1),
"optimeDate" : ISODate("2013-12-04T00:51:20Z"),
"lastHeartbeat" : ISODate("2013-12-06T08:44:00Z"),
"lastHeartbeatRecv" : ISODate("2013-12-06T08:44:01Z"),
"pingMs" : 0,
"syncingTo" : "192.168.1.250:27019"
},
{
"_id" : 2,
"name" : "192.168.1.250:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4958,
"optime" : Timestamp(1386118280, 1),
"optimeDate" : ISODate("2013-12-04T00:51:20Z"),
"self" : true
}
],
"ok" : 1
}
res1:PRIMARY>
可以看到name 192.168.1.248 這臺server的不正常,另外兩臺的LOG也是在不斷的輸出無法連接到host 192.168.1.248 27017 這個端口,
當你host 192.168.1.248 mongodb進程重新運行起來之后,就會自動切換為primary
Fri Dec 6 16:48:35.325 [conn246] SocketException handling request, closing client connection: 9001 socket exception [SEND_ERROR] server [192.168.1.247:27047]
Fri Dec 6 16:48:35.388 [rsHealthPoll] replSet member 192.168.1.248:27017 is now in state PRIMARY
[root@test02 bin]# /usr/local/mongodb/bin/mongo --port 27017
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:27017/test
res1:PRIMARY>
而如果你host 192.168.1.248宕機時,host 192.168.1.250 擔當primary,進行寫數據
db.appstore.save({'e_name':'xiaowang','e_id':1103,'class_id':2});
res1:PRIMARY> db.appstore.find();db.appstore.find();
{ "_id" : ObjectId("529e7c88d4d317e4bd3eece9"), "e_name" : "frank", "e_id" : 1101, "class_id" : 1 }
{ "_id" : ObjectId("52a18f3bd36b29b9c78be267"), "e_name" : "xiaowang", "e_id" : 1103, "class_id" : 2 }
之后當host 192.168.1.248 擔當primary時,新增加的數據也會進行同步的,類似mysql的master-slave 同步
示列二:replica set 節點的增、刪、改操作
現在呢,假設我primary host 192.168.1.248 宕機了,想把這個節點給刪掉
先ps -aux | grep mongodb ,然后kill掉進程
現在 host 192.168.20.250 已經被置為primary
[root@anenjoy ~]# /usr/local/mongodb/bin/mongo --port 27019
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:27019/test
res1:PRIMARY>
通過rs.conf()查看節點配置
res1:PRIMARY> rs.conf();
{
"_id" : "res1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "192.168.1.248:27017",
"priority" : 2
},
{
"_id" : 1,
"host" : "192.168.1.247:27018",
"priority" : 0
},
{
"_id" : 2,
"host" : "192.168.1.250:27019"
}
]
res1:PRIMARY> rs.remove('192.168.1.248:27017');
Fri Dec 6 16:59:01.480 DBClientCursor::init call() failed
Fri Dec 6 16:59:01.482 Error: error doing query: failed at src/mongo/shell/query.js:78
Fri Dec 6 16:59:01.482 trying reconnect to 127.0.0.1:27019
Fri Dec 6 16:59:01.482 reconnect 127.0.0.1:27019 ok
再次查看,ok 節點已經被刪除掉了
res1:PRIMARY> rs.conf();
{
"_id" : "res1",
"version" : 2,
"members" : [
{
"_id" : 1,
"host" : "192.168.1.247:27018",
"priority" : 0
},
{
"_id" : 2,
"host" : "192.168.1.250:27019"
}
]
}
LOG日志中也就不會有:[rsHealthPoll] couldn't connect to 192.168.1.248:27017: couldn't connect to server 192.168.1.248:27017 日志的輸出
增加節點:
通過oplog直接進行增加節點操作簡單且不需要人過多的參與,但oplog是capped collection,會循環使用的,所以如果只是簡單的使用oplog來進行增加節點,有可能導致數據的不一致,因為日志中存儲的信息有可能已經刷新過了。
可以通過使用數據庫快照(--fastsync)和oplog結合的方式來增加節點,一般的操作步驟是:
先取某一個復制集成員的物理文件作為初始化數據,然后剩余的部分用oplog日志來追加,從而最終達到數據一致性
最新準備的步驟都是一樣的:
建DB存儲的目錄,key文件、權限的600
第一步:配置存儲路徑,--dbpath的參數
均放在/data/mon_db下,目錄權限賦予mongodb用戶
mkdir -p /data/mon_db
chown -R mongodb:mongodb /data/mon_db/
創建日志文件,--logpath的參數,位置自己定義
就放在mkdir -p /usr/local/mongodb/log
touch /usr/local/mongodb/log/mongodb.log
chown -R mongodb:mongodb /usr/local/mongodb/
第二步:創建主從的key文件,用于標識集群的私鑰的完整路徑,如果各個實例的key file內容不一致,程序將不能正常用
[root@test02 ~]# mkdir -p /data/mon_db/key
[root@test02 ~]# echo "this is res key" > /data/mon_db/key/res1
Chmod +R 600 /data/mon_db/key/res1 權限賦予600,否則會提示error message
Wed Dec 4 06:22:36.413 permissions on /data/mon_db/key/res1 are too open
更改不同的名字就好了
假設說同步host 192.168.1.247的物理文件吧
Scp -r /data/mongodb/res2/ root@ip:/data/mon_db/res4
之后呢,可以在primary插入新數據(驗證使用)
啟動mongodb
/usr/local/mongodb/bin/mongod --port 27020 --replSet res1 --keyFile /data/mon_db/key/res4 --oplogSize 100 --dbpath=/data/mon_db/res4/ --logpath=/usr/local/mongodb/log/mongodb.log --logappend --fastsync --fork
之后
在primary上執行添加節點:
Rs.add(‘192.168.1.x:27020’)
之后在新添加的節點上,登錄到mongodb,獲取讀的權限,查看數據是不是同步成功
節點的更改
何為節點的更改,其實不外乎對節點host、port、priority進行更改,這邊文章簡單的描述下如何進行更改
目前我的replica set 有三個節點
/usr/local/mongodb/bin/mongo --port 27019
rs.status();
{
"set" : "res1",
"date" : ISODate("2013-12-06T11:56:42Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.1.247:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10661,
"optime" : Timestamp(1386330980, 1),
"optimeDate" : ISODate("2013-12-06T11:56:20Z"),
"lastHeartbeat" : ISODate("2013-12-06T11:56:42Z"),
"lastHeartbeatRecv" : ISODate("2013-12-06T11:56:40Z"),
"pingMs" : 0,
"syncingTo" : "192.168.1.250:27019"
},
{
"_id" : 2,
"name" : "192.168.1.250:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 16519,
"optime" : Timestamp(1386330980, 1),
"optimeDate" : ISODate("2013-12-06T11:56:20Z"),
"self" : true
},
{
"_id" : 3,
"name" : "192.168.1.248:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 22,
"optime" : Timestamp(1386330980, 1),
"optimeDate" : ISODate("2013-12-06T11:56:20Z"),
"lastHeartbeat" : ISODate("2013-12-06T11:56:42Z"),
"lastHeartbeatRecv" : ISODate("2013-12-06T11:56:41Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: 192.168.1.250:27019",
"syncingTo" : "192.168.1.250:27019"
}
],
"ok" : 1
}
我想更改節點直接的優先級,現在host 192.168.1.250 是primary ,priority為2 ,我想讓host:192.168.1.248 作為primary,只要它的priority 為3 大于2 即可
res1:PRIMARY> cfg=rs.conf();
{
"_id" : "res1",
"version" : 3,
"members" : [
{
"_id" : 1,
"host" : "192.168.1.247:27018",
"priority" : 0
},
{
"_id" : 2,
"host" : "192.168.1.250:27019"
},
{
"_id" : 3,
"host" : "192.168.1.248:27017"
}
]
}
res1:PRIMARY>cfg.members[2].priority=3;
res1:PRIMARY> rs.reconfig(cfg);rs.reconfig() 類似重新初始化
Fri Dec 6 20:00:29.788 DBClientCursor::init call() failed
Fri Dec 6 20:00:29.792 trying reconnect to 127.0.0.1:27019
Fri Dec 6 20:00:29.793 reconnect 127.0.0.1:27019 ok
reconnected to server after rs command (which is normal)
多敲兩次回車,就會發現之前是primary,就變成了secondary,而你的host 192.168.1.248 就變成了primary
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。