一、MongoDB復制集概述
MongoDB復制集實現了冗余備份和故障轉移兩大功能,這樣能保證數據庫的高可用性。在生產環境,復制集至少包括三個節點,其中一個必須為主節點,一個從節點,一個仲裁節點。其中每一個節點都是mongod進程對應的實例,節點間通過心跳檢查對方的狀態。
primary節點:負責數據庫的讀寫操作。
secondary節點:備份primary節點上的數據,可以有多個。
arbiter節點:主節點故障時,參與復制集剩下節點中選舉一個新的primary節點。
二、搭建MongoDB復制集環境
1、環境准備:
1)主機環境
192.168.0.151 db01 ## primary節點
192.168.0.152 db02 ## secondary節點
192.168.0.153 db03 ## arbiter節點
2)創建搭建MongoDB用戶
groupadd mongodb
useradd -g mongodb mongodb
3)安裝mongodb軟件
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-3.6.0.tgz
mv mongodb-linux-x86_64-enterprise-rhel70-3.6.0 mongodb-3.6.0
2、創建復制集中每個節點存放數據目錄:
/home/mongodb/db_rs0/data/rs0
3、創建復制集中每個節點的日志目錄:
/home/mongodb/db_rs0/logs
4、創建復制集中每個節點啟動時所需的配置文件:
cat /home/mongodb/db_rs0/config_rs0/rs0.conf
dbpath = /home/mongodb/db_rs0/data/rs0
logpath = /home/mongodb/db_rs0/logs/rs0.log
logappend = true
journal = true
port = 40000
fork = true
maxConns = 5000
bind_ip = 0.0.0.0
replSet = rs0
5、啟動三個節點對應的mongodb實例
bin/mongod --config /home/mongodb/db_rs0/config_rs0/rs0.conf
6、db01下進入mongodb客戶端:
bin/mongo --port 40000
7、初始化復制集primary節點、添加second節點和arbiter節點:
1)初始化primary節點:
MongoDB Enterprise > rs.initiate({_id:'rs0',members:[{_id:1,host:'db01:40000'}]})
{
"ok" : 1,
"operationTime" : Timestamp(1513518326, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513518326, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
MongoDB Enterprise rs0:SECONDARY> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 1,
"host" : "db01:40000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5a3674f6be9a0ef57aaf73ac")
}
}
2)添加second節點和arbiter節點:
MongoDB Enterprise rs0:PRIMARY> rs.add("db02:40000")
{
"ok" : 1,
"operationTime" : Timestamp(1513518502, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513518502, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
MongoDB Enterprise rs0:PRIMARY> rs.addArb("db03:40000")
{
"ok" : 1,
"operationTime" : Timestamp(1513518525, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513518525, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
8、檢查各個節點local庫信息:
MongoDB Enterprise rs0:PRIMARY> use local
switched to db local
MongoDB Enterprise rs0:PRIMARY> show collections
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
MongoDB Enterprise rs0:SECONDARY> use local
switched to db local
MongoDB Enterprise rs0:SECONDARY>
MongoDB Enterprise rs0:SECONDARY>
MongoDB Enterprise rs0:SECONDARY> show collections
me
oplog.rs
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
MongoDB Enterprise rs0:ARBITER> use local
switched to db local
MongoDB Enterprise rs0:ARBITER> show collections
me
replset.minvalid
startup_log
system.replset
system.rollback.id
9、檢查復制集狀態:
MongoDB Enterprise rs0:PRIMARY> rs.status()
{
"set" : "rs0", //復制集名稱
"date" : ISODate("2017-12-17T13:49:23.671Z"), //當前實例所在服務器的時間
"myState" : 1, //當前節點成員在復制集中的位置,如1表示primary,2表示secondry。
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
}
},
"members" : [ //復制集所有成員信息
{
"_id" : 1, //成員編號
"name" : "db01:40000", //成員所在服務器名稱
"health" : 1, //成員在復制集中是否運行,1表示運行,0表示失敗
"state" : 1, //成員在復制集中的狀態,1表示主節點
"stateStr" : "PRIMARY", //成員在復制集中狀態名稱
"uptime" : 873, //成員在線時間,單位是秒
"optime" : { //本實例最近一次更改數據庫的時間以及每秒執行的操作數據庫的次數。
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T13:49:18Z"),
"electionTime" : Timestamp(1513518326, 2),
"electionDate" : ISODate("2017-12-17T13:45:26Z"),
"configVersion" : 3,
"self" : true //成員為當前命令所在的服務器
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 1, //成員在復制集中是否運行,1表示運行,0表示失敗
"state" : 2, //成員在復制集中的狀態,2表示second節點
"stateStr" : "SECONDARY",
"uptime" : 61,
"optime" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1513518558, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T13:49:18Z"),
"optimeDurableDate" : ISODate("2017-12-17T13:49:18Z"),
"lastHeartbeat" : ISODate("2017-12-17T13:49:23.505Z"), //當前實例到此遠端成員最近一次成功發送與接收心跳包的時間。
"lastHeartbeatRecv" : ISODate("2017-12-17T13:49:23.504Z"),
"pingMs" : NumberLong(0), //此遠端成員到本實例間一個路由包的來回時間
"syncingTo" : "db01:40000", //此成員從哪個實例同步數據
"configVersion" : 3
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7, //成員在復制集中的狀態,7表示arbiter節點
"stateStr" : "ARBITER",
"uptime" : 38,
"lastHeartbeat" : ISODate("2017-12-17T13:49:23.505Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T13:49:20.562Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513518558, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513518558, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
10、測試復制集
1)主節點插入數據:
MongoDB Enterprise rs0:PRIMARY> db
test
MongoDB Enterprise rs0:PRIMARY> for(var i =0; i <4; i ++){db.user.insert({userName:'gxt'+i,age:i})}
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise rs0:PRIMARY> show collections
user
MongoDB Enterprise rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5a3677b8041261917b9120a0"), "userName" : "gxt0", "age" : 0 }
{ "_id" : ObjectId("5a3677b8041261917b9120a1"), "userName" : "gxt1", "age" : 1 }
{ "_id" : ObjectId("5a3677b8041261917b9120a2"), "userName" : "gxt2", "age" : 2 }
{ "_id" : ObjectId("5a3677b8041261917b9120a3"), "userName" : "gxt3", "age" : 3 }
2)從節點檢查復制情況:
MongoDB Enterprise rs0:SECONDARY> db
test
MongoDB Enterprise rs0:SECONDARY> show collections
2017-12-17T21:57:37.136+0800 E QUERY [thread1] Error: listCollections failed: {
"operationTime" : Timestamp(1513519048, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false", //因為secondary是不允許讀寫的,如果非要解決,則執行:rs.slaveOk()
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1513519048, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:803:9
shellHelper@src/mongo/shell/utils.js:700:15
@(shellhelp2):1:1
MongoDB Enterprise rs0:SECONDARY> rs.slaveOk()
MongoDB Enterprise rs0:SECONDARY> db
test
MongoDB Enterprise rs0:SECONDARY> show collections
user
MongoDB Enterprise rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5a3677b8041261917b9120a0"), "userName" : "gxt0", "age" : 0 }
{ "_id" : ObjectId("5a3677b8041261917b9120a1"), "userName" : "gxt1", "age" : 1 }
{ "_id" : ObjectId("5a3677b8041261917b9120a2"), "userName" : "gxt2", "age" : 2 }
{ "_id" : ObjectId("5a3677b8041261917b9120a3"), "userName" : "gxt3", "age" : 3 }
通過db.user.find()查詢到和主復制集上一樣的數據,表示數據同步成功!
3)arbiter節點檢查數據情況:
MongoDB Enterprise rs0:ARBITER> db
test
MongoDB Enterprise rs0:ARBITER> show collections
2017-12-17T22:05:18.090+0800 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:803:9
shellHelper@src/mongo/shell/utils.js:700:15
@(shellhelp2):1:1
MongoDB Enterprise rs0:ARBITER> rs.slaveOk()
MongoDB Enterprise rs0:ARBITER> show collections
arbiter並沒有進行數據同步,因為仲裁節點只參與投票,不接收數據!
三、復制集工作原理
復制集通過local庫下的oplog.rs集合進行數據同步。
四、測試MongoDB復制集自動故障轉移功能
MongoDB復制集搭建完成后,相當於具備了備份的功能,上文中已經展示過,這里不再贅述,下面我們主要測試一下MongoDB的另一大功能,自動故障轉移的功能。
1、MongoDB的second節點宕機
通過kill second節點進行測試:
1)查看集群狀態,所有節點運行正常:
MongoDB Enterprise rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-12-17T14:43:37.210Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 1,
"name" : "db01:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4127,
"optime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T14:43:28Z"),
"electionTime" : Timestamp(1513518326, 2),
"electionDate" : ISODate("2017-12-17T13:45:26Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 1,
"state" : 2, //健康
"stateStr" : "SECONDARY",
"uptime" : 3314,
"optime" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1513521808, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T14:43:28Z"),
"optimeDurableDate" : ISODate("2017-12-17T14:43:28Z"),
"lastHeartbeat" : ISODate("2017-12-17T14:43:35.254Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:43:35.240Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "db01:40000",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 3291,
"lastHeartbeat" : ISODate("2017-12-17T14:43:35.254Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:43:36.323Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513521808, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513521808, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
2)kill second節點進程:
[mongodb@db02 mongodb-3.6.0]$ ps -ef|grep mongo
root 2313 2254 0 20:27 pts/0 00:00:00 su - mongodb
mongodb 2314 2313 0 20:27 pts/0 00:00:00 -bash
root 2625 2469 0 20:47 pts/1 00:00:00 su - mongodb
mongodb 2626 2625 0 20:47 pts/1 00:00:00 -bash
mongodb 3378 1 0 21:34 ? 00:00:24 bin/mongod --config /home/mongodb/db_rs0/config_rs0/rs0.conf
mongodb 4083 2314 0 22:44 pts/0 00:00:00 ps -ef
mongodb 4084 2314 0 22:44 pts/0 00:00:00 grep --color=auto mongo
[mongodb@db02 mongodb-3.6.0]$ kill -9 3378
[mongodb@db02 mongodb-3.6.0]$ ps -ef|grep mongo
root 2313 2254 0 20:27 pts/0 00:00:00 su - mongodb
mongodb 2314 2313 0 20:27 pts/0 00:00:00 -bash
root 2625 2469 0 20:47 pts/1 00:00:00 su - mongodb
mongodb 2626 2625 0 20:47 pts/1 00:00:00 -bash
mongodb 4085 2314 0 22:44 pts/0 00:00:00 ps -ef
mongodb 4086 2314 0 22:44 pts/0 00:00:00 grep --color=auto mongo
3)再次查看節點狀態,發現second節點已經宕機:
MongoDB Enterprise rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-12-17T14:44:33.514Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513521848, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513521848, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1513521868, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1513521868, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 1,
"name" : "db01:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4183,
"optime" : {
"ts" : Timestamp(1513521868, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T14:44:28Z"),
"electionTime" : Timestamp(1513518326, 2),
"electionDate" : ISODate("2017-12-17T13:45:26Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 0,
"state" : 8, //second節點宕機
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-12-17T14:44:33.301Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:44:17.267Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 3348,
"lastHeartbeat" : ISODate("2017-12-17T14:44:33.288Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:44:31.334Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513521868, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513521868, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4)主節點向測試表插入一條記錄,一切操作正常:
MongoDB Enterprise rs0:PRIMARY> db.scores.insert({stuid:1,subobject:"math",score:99})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise rs0:PRIMARY> db.scores.find()
{ "_id" : ObjectId("5a36810c041261917b9120a5"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3682fe041261917b9120a7"), "stuid" : 1, "subobject" : "math", "score" : 99 }
5)再次啟動從節點,檢查步驟4primary節點操作集合,發現數據正常同步到從節點:
MongoDB Enterprise rs0:SECONDARY> rs.slaveOk()
MongoDB Enterprise rs0:SECONDARY> show collections
scores
user
MongoDB Enterprise rs0:SECONDARY> db.scores.find()
{ "_id" : ObjectId("5a36810c041261917b9120a5"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3682fe041261917b9120a7"), "stuid" : 1, "subobject" : "math", "score" : 99 }
2、MongoDB的primary節點宕機
1)檢查復制集運行狀態
MongoDB Enterprise rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-12-17T14:56:52.953Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 1,
"name" : "db01:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4922,
"optime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T14:56:48Z"),
"electionTime" : Timestamp(1513518326, 2),
"electionDate" : ISODate("2017-12-17T13:45:26Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 649,
"optime" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1513522608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-12-17T14:56:48Z"),
"optimeDurableDate" : ISODate("2017-12-17T14:56:48Z"),
"lastHeartbeat" : ISODate("2017-12-17T14:56:51.820Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:56:51.844Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "db01:40000",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 4087,
"lastHeartbeat" : ISODate("2017-12-17T14:56:51.743Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:56:51.518Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513522608, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513522608, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
2)kill掉主節點
[mongodb@db01 mongodb-3.6.0]$ ps -ef|grep mongo
root 3082 3056 0 20:26 pts/0 00:00:00 su - mongodb
mongodb 3083 3082 0 20:26 pts/0 00:00:00 -bash
root 3266 3139 0 20:46 pts/1 00:00:00 su - mongodb
mongodb 3267 3266 0 20:46 pts/1 00:00:00 -bash
root 3316 3299 0 20:50 pts/2 00:00:00 su - mongodb
mongodb 3317 3316 0 20:50 pts/2 00:00:00 -bash
mongodb 3771 1 0 21:34 ? 00:00:30 bin/mongod --config /home/mongodb/db_rs0/config_rs0/rs0.conf
mongodb 3935 3083 0 22:57 pts/0 00:00:00 ps -ef
mongodb 3936 3083 0 22:57 pts/0 00:00:00 grep --color=auto mongo
[mongodb@db01 mongodb-3.6.0]$ kill -9 3771
[mongodb@db01 mongodb-3.6.0]$ ps -ef|grep mongo
root 3082 3056 0 20:26 pts/0 00:00:00 su - mongodb
mongodb 3083 3082 0 20:26 pts/0 00:00:00 -bash
root 3266 3139 0 20:46 pts/1 00:00:00 su - mongodb
mongodb 3267 3266 0 20:46 pts/1 00:00:00 -bash
root 3316 3299 0 20:50 pts/2 00:00:00 su - mongodb
mongodb 3317 3316 0 20:50 pts/2 00:00:00 -bash
mongodb 3937 3083 0 22:57 pts/0 00:00:00 ps -ef
mongodb 3938 3083 0 22:57 pts/0 00:00:00 grep --color=auto mongo
3)再次檢查集群運行狀態
MongoDB Enterprise rs0:SECONDARY>
MongoDB Enterprise rs0:PRIMARY> rs.status() //從提示我們可以看到secondary節點已經變成了主節點
{
"set" : "rs0",
"date" : ISODate("2017-12-17T14:58:24.922Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513522668, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513522668, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1513522703, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1513522703, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 1,
"name" : "db01:40000",
"health" : 0,
"state" : 8, //主節點宕機
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-12-17T14:58:24.620Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:57:51.856Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //原secondary節點已經變為主節點
"uptime" : 744,
"optime" : {
"ts" : Timestamp(1513522703, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-12-17T14:58:23Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1513522682, 1),
"electionDate" : ISODate("2017-12-17T14:58:02Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 743,
"lastHeartbeat" : ISODate("2017-12-17T14:58:24.601Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T14:58:21.549Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513522703, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513522703, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4)新主節點插入測試數據,一切操作正常
MongoDB Enterprise rs0:PRIMARY> db
test
MongoDB Enterprise rs0:PRIMARY> show collections
scores
user
MongoDB Enterprise rs0:PRIMARY> db.scores.insert({stuid:1,subobject:"math",score:99})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise rs0:PRIMARY> db.scores.find()
{ "_id" : ObjectId("5a36810c041261917b9120a5"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3682fe041261917b9120a7"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3686bb1e50f07a6a2d207d"), "stuid" : 1, "subobject" : "math", "score" : 99 }
5)重新啟動原主節點,查看集群狀態
MongoDB Enterprise rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-12-17T15:02:28.114Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 1,
"name" : "db01:40000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //原主節點已經變為從節點
"uptime" : 19,
"optime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-12-17T15:02:23Z"),
"optimeDurableDate" : ISODate("2017-12-17T15:02:23Z"),
"lastHeartbeat" : ISODate("2017-12-17T15:02:26.998Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T15:02:27.971Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "db02:40000",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "db02:40000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 988,
"optime" : {
"ts" : Timestamp(1513522943, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-12-17T15:02:23Z"),
"electionTime" : Timestamp(1513522682, 1),
"electionDate" : ISODate("2017-12-17T14:58:02Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 3,
"name" : "db03:40000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 986,
"lastHeartbeat" : ISODate("2017-12-17T15:02:26.754Z"),
"lastHeartbeatRecv" : ISODate("2017-12-17T15:02:26.603Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1513522943, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1513522943, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
6)查看步驟4插入測試數據信息
MongoDB Enterprise rs0:SECONDARY> db
test
MongoDB Enterprise rs0:SECONDARY> show collections
2017-12-17T23:03:33.650+0800 E QUERY [thread1] Error: listCollections failed: {
"operationTime" : Timestamp(1513523003, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1513523003, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:803:9
shellHelper@src/mongo/shell/utils.js:700:15
@(shellhelp2):1:1
MongoDB Enterprise rs0:SECONDARY> rs.slaveOk()
MongoDB Enterprise rs0:SECONDARY> show collections
scores
user
MongoDB Enterprise rs0:SECONDARY> db.scores.find()
{ "_id" : ObjectId("5a36810c041261917b9120a5"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3682fe041261917b9120a7"), "stuid" : 1, "subobject" : "math", "score" : 99 }
{ "_id" : ObjectId("5a3686bb1e50f07a6a2d207d"), "stuid" : 1, "subobject" : "math", "score" : 99 }
集群運轉正常,數據已經正常同步過來。
