},
"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("5cff76e5e57e23a5bc7054e2")
}
}
四、验证Replica Set
在Primary中插入数据
rs0:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
rs0:PRIMARY> db.users.insertOne(
... {
... name:"sue",
... age: 26,
... status:"pending"
... })
{
"acknowledged" : true,
"insertedId" : ObjectId("5cff79e8993e70290a081d04")
}
rs0:PRIMARY> db.users.find()
{ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }
在Secondary中,默认是不允许读
rs0:SECONDARY> db.users.find()
Error: error: {
"operationTime" : Timestamp(1560247181, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1560247181, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
五、故障模拟
停止Primary上的mongo进程,在Secondary上登录mongo查看
mongo --host 10.10.18.11
rs0:PRIMARY> db.users.find()
{ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }
发现原Secondary变成Primary,并且可以进行查询。
开启原Primary服务器上的mongo进程,该服务器变成了Secondary。
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx