MongoDB Replica Sets

简介


​MongoDB​​​中的副本集​​(Replica Set)​​​是一组维护相同数据集的​​mongod​​服务。副本集可提供冗余和高可用性,是所有生产部署的基础。

也可以说,副本集类似于有自动故障修复功能的主从集群。通俗的讲就使用多台机器进行同一数据的异步同步,从而使多台机器拥有同一数据的多个副本,并且当主库宕机时再不需要用户干预的情况下自动切换其他备份服务器做主库。而且还可以利用副本服务器做只读服务器,实现读写分离,提高负载。

1 冗余和数据可用性

复制提供冗余并提高数据可用性。通过在不同数据库服务器上提供多个数据副本,复制可提供一定级别的容错功能,以防止丢失单个数据库服务器。

在某些情况下,复制可以提供增加的读取性能,因为客户端可以将读取操作发送到不同的服务上,在不同数据中心维护数据副本可以增加分布式应用程序的数据位置和可用性。

2 MongoDB中的复制

副本集是一组维护相同数据集的​​mongod​​实例。副本集包含多个数据承载节点和可选的一个仲裁节点。在承载数据的节点中,一个且仅一个成员视为主节点,而其他节点被视为要(从)节点。

主节点接收所有的操作。副本集只能有一个主要能够确认具有{w:"most"}写入关注的写入;虽然在某些情况下,另一个​​mongod​​​实例可能暂时认为自己也是主要的。主要记录其操作日志中的数据集的所有更改,即​​oplog​​。

3 主从复制和副本集区别

主从集群和副本集最大的区别就是副本集没有固定的“主节点”;整个集群会选出一个“主节点”,当其挂掉后,又在剩下的从节点中选中其他节点为“主节点”,副本集总有一个活跃点(主、primary)和一个或多个备份节点(从、secondary)



副本集的三个角色


副本集有两种类型三种角色

两种类型:

  • 主节点(Primary)类型:数据操作的主要连接带你,可读写。
  • 次要(辅助、从)节点(Secondaries)类型:数据冗余备份节点,可以读或选举。

三种角色:

主要成员(Primary):主要接收所有写操作。就是主节点。

副本成员(Replicate):从节点通过复制操作以维护相同的数据集,即备份数据,不可泄操作,但可以读操作(但需要配置)。是默认的一种从节点类型。

仲裁者(Arbiter):不保留任何数据的副本,只具有投票选举作用。当然也可以将仲裁服务器维护为副本集的一部分,即副本成员同事也可以是仲裁者。也是一种从节点类型。

MongoDB--副本集_副本集



搭建副本集集群

一主一副本一仲裁

MongoDB--副本集_MongoDB_02

这里在一台机器上面进行搭建,通过多端口方式实现


创建主节点

1.创建数据存储和日志存放目录

# mkdir -p /data/replica_sets/myrs_27017/log
# mkdir -p /data/replica_sets/myrs_27017/data/db

2.新建或修改配置文件

# vim /data/replica_sets/myrs_27017/mongod.conf 
systemLog:
#日志输出的目标指定为文件
destination: file
#日志存放目录
path: "/data/replica_sets/myrs_27017/log/mongod.log"
#当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
#mongod实例存储其数据的目录
dbPath: "/data/replica_sets/myrs_27017/data/db"
journal:
#启用或禁用持久性日志已确保数据文件保持有效和可恢复
enabled: true
processManagement:
#启用在后台运行mongos或mongod进程的守护进程模式
fork: true
#指定用于保存mongos或mongod进程的进程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27017/log/mongod.pid"
net:
#服务实例绑定IP
bindIp: 127.0.0.1,10.10.10.11
#服务实例绑定端口
port: 27017
replication:
#副本集的名称
replSetName: myrs

3.启动节点服务

# mongod -f /data/replica_sets/myrs_27017/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 23945
child process started successfully, parent exiting


创建副本节点

1.创建数据存储和日志存放目录

# mkdir -p /data/replica_sets/myrs_27018/log
# mkdir -p /data/replica_sets/myrs_27018/data/db

2.新建或修改配置文件

# vim /data/replica_sets/myrs_27018/mongod.conf 
systemLog:
#日志输出的目标指定为文件
destination: file
#日志存放目录
path: "/data/replica_sets/myrs_27018/log/mongod.log"
#当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
#mongod实例存储其数据的目录
dbPath: "/data/replica_sets/myrs_27018/data/db"
journal:
#启用或禁用持久性日志已确保数据文件保持有效和可恢复
enabled: true
processManagement:
#启用在后台运行mongos或mongod进程的守护进程模式
fork: true
#指定用于保存mongos或mongod进程的进程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27018/log/mongod.pid"
net:
#服务实例绑定IP
bindIp: 127.0.0.1,10.10.10.11
#服务实例绑定端口
port: 27018
replication:
#副本集的名称
replSetName: myrs

3.启动节点服务

# mongod -f /data/replica_sets/myrs_27018/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 24174
child process started successfully, parent exiting


创建仲裁节点

1.创建数据存储和日志存放目录

# mkdir -p /data/replica_sets/myrs_27019/log
# mkdir -p /data/replica_sets/myrs_27019/data/db

2.新建或修改配置文件

# vim /data/replica_sets/myrs_27019/mongod.conf
systemLog:
#日志输出的目标指定为文件
destination: file
#日志存放目录
path: "/data/replica_sets/myrs_27019/log/mongod.log"
#当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
#mongod实例存储其数据的目录
dbPath: "/data/replica_sets/myrs_27019/data/db"
journal:
#启用或禁用持久性日志已确保数据文件保持有效和可恢复
enabled: true
processManagement:
#启用在后台运行mongos或mongod进程的守护进程模式
fork: true
#指定用于保存mongos或mongod进程的进程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27019/log/mongod.pid"
net:
#服务实例绑定IP
bindIp: 127.0.0.1,10.10.10.11
#服务实例绑定端口
port: 27019
replication:
#副本集的名称
replSetName: myrs

3.启动节点服务

# mongod -f /data/replica_sets/myrs_27019/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 24441
child process started successfully, parent exiting


初始化配置副本集

1.使用客户端命令连接任意一个节点,初始化主节点(这里选择27017)

# mongo --host=10.10.10.11 --port=27017

备注:连接上之后,很多命令无法使用,比如​​show dbs​​等,必须初始化副本集才行

2.初始化新的副本集(命令:​​rs.initiate()​​)

> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "10.10.10.11:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604913568, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604913568, 1)
}
myrs:SECONDARY>
myrs:PRIMARY>

备注:

1)​​"ok"​​的值为1,说明创建成功。

2)命令行提示符发生变化,变成了一个从节点角色,此时默认不能读写。稍等片刻,回车,变成主节点。


查看副本集的配置


返回包含当前副本集配置的文档。

语法:​​rs.conf(configuration)​

备注:

​rs.config()​​是该方法的别名。

configuration:可选,如果没有配置,则使用默认主节点配置。


示例:在27017上执行副本集中当前节点的默认节点配置

myrs:PRIMARY> rs.conf()
{
"_id" : "myrs",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.10.11:27017",
"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("5fa909a07e34d2b6fa2e54a1")
}
}

说明:

1)​​"_id" : "myrs"​​:副本集的配置数据存储的主键值,默认就是副本集的名字

2)​​"members"​​​:副本集成员数组,此时只有一个:​​"host" : "10.10.10.11:27017"​​​,该成员不是仲裁节点:​​"arbiterOnly" : false​​​,优先级(权重值):​​"priority" : 1​​,

3)​​"settings"​​:副本集的参数配置

提示:副本集配置的查看命令,本质是查询的是​​system.replset​​的表中的数据:

myrs:PRIMARY> use local
switched to db local
myrs:PRIMARY> show collections
oplog.rs
replset.election
replset.initialSyncId
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
myrs:PRIMARY> db.system.replset.find()
{ "_id" : "myrs", "version" : 1, "term" : 1, "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "members" : [ { "_id" : 0, "host" : "10.10.10.11:27017", "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("5fa909a07e34d2b6fa2e54a1") } }


查看副本集的状态


说明:返回包含状态信息的文档,次输出结果从副本集的其他成员发送的心跳包中获得的数据反映副本集的当前状态

语法:​​rs.status()​


在27017上查看副本集状态

myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T01:15:18.661Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 1,
"writeMajorityCount" : 1,
"votingMembersCount" : 1,
"writableVotingMembersCount" : 1,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"lastDurableWallTime" : ISODate("2020-11-10T01:15:15.402Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604970895, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 57734,
"optime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T01:15:15Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604970915, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604970915, 1)
}

说明:

1)​​"set" : "myrs"​​:副本集的名字

2)​​"myState" : 1​​:说明状态正常

3)​​"members"​​​:副本集成员数组,此时只有一个:​​"name" : "10.10.10.11:27017"​​​,该成员的角色是​​"stateStr" : "PRIMARY"​​​,该节点是健康的:​​"health" : 1​​。


添加副本从节点


在主节点添加从节点,将其他成员加入到副本集


语法:

rs.add(host, arbiterOnly)

Parameter

Type

Description

​host​

string or document

要添加到副本集的新成员。指定为字符串或配置文档:1)如果是一个字符串,则需要指定新成员的主机名和可选的端口;2)如果是一个文档,需要指定在members数组中找到的副本集成员配置文档。必须在成员配置文档中指定主机字段。详见下方文档:“主机成员的配置文档”

​arbiterOnly​

boolean

可选的。仅在值为字符串时使用。如果为true,则添加的主机是仲裁者。

主机成员的配置文档:

{ 
_id: <int>,
host: <string>, // required
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
slaveDelay: <int>,
votes: <number>
}

将27018的副本节点添加到副本集中:

myrs:PRIMARY> rs.add("10.10.10.11:27018")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604975516, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604975516, 1)
}

说明:

1)​​"ok" : 1​​:说明添加成功。

查看副本集状态

myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T02:33:54.698Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 2,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"lastDurableWallTime" : ISODate("2020-11-10T02:33:45.868Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604975575, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 62450,
"optime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T02:33:45Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 2,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 117,
"optime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T02:33:45Z"),
"optimeDurableDate" : ISODate("2020-11-10T02:33:45Z"),
"lastHeartbeat" : ISODate("2020-11-10T02:33:52.778Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T02:33:53.089Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "10.10.10.11:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604975625, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604975625, 1)
}

说明:

1)​​"name" : "10.10.10.11:27018"​​​是第二个节点的名字,其角色是​​"stateStr" : "SECONDARY"​


添加仲裁节点


在主节点添加一个仲裁节点到副本集


语法:

rs.addArb(host)

将27019节点添加到副本集中作为仲裁节点:

myrs:PRIMARY> rs.addArb("10.10.10.11:27019")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604989664, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604989664, 1)
}

说明:

1)​​"ok" : 1​​:说明添加成功

查看副本集状态:

myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T06:28:31.102Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"lastDurableWallTime" : ISODate("2020-11-10T06:28:27.292Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604989675, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 76527,
"optime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T06:28:27Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 3,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14194,
"optime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T06:28:27Z"),
"optimeDurableDate" : ISODate("2020-11-10T06:28:27Z"),
"lastHeartbeat" : ISODate("2020-11-10T06:28:30.884Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T06:28:30.897Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "10.10.10.11:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "10.10.10.11:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 46,
"lastHeartbeat" : ISODate("2020-11-10T06:28:30.900Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T06:28:30.917Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604989707, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604989707, 1)
}

说明:

1)​​"name" : "10.10.10.11:27019"​​​是第二个节点的名字,其角色是​​"stateStr" : "ARBITER"​


副本集的数据读写操作


测试三个不同角色的节点的数据读写情况。


1、测试主节点27017,写入和读数据:

# mongo --host=10.10.10.11 --port=27017
myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db
collectest
myrs:PRIMARY> db.collectest.insert({"name":"张三","sex":"男","age":22,"userid":1001,"createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })
myrs:PRIMARY>
myrs:PRIMARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "张三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
myrs:PRIMARY>

说明:通过测试可以发现主节点是可以进行数据的读和写操作的。

2、测试从节点

# mongo --host=10.10.10.11 --port=27018
myrs:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa909804c7cb6020458ce77"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1605085202, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1605085202, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

测试发现,不能读取集合的数据。当前从节点只是一个备份,不是奴隶节点,无法读取数据,写当然更不行。因为默认情况下,从节点是没有读写权限的,可以增加读的权限,但是需要进行设置。

设置读操作权限:

rs.secondaryOk()
#或者
rs.secondaryOk(true)

示例,在27018上面设置从节点具备读权限:

myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> show dbs
admin 0.000GB
collectest 0.000GB
config 0.000GB
local 0.000GB
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> show collections
collectest
myrs:SECONDARY>
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "张三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }

这里便实现了读写分离,让主节点插入数据,让从来读取数据。

如果要取消从节点的读权限:

myrs:SECONDARY> rs.secondaryOk(false)
myrs:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa909804c7cb6020458ce77"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1605086322, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1605086322, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

3、测试仲裁节点,仲裁节点不会放任何业务数据,可以登陆查看

# mongo --host=10.10.10.11 --port=27019
myrs:ARBITER> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa90b59bea51b269ba3eb82"),
"counter" : NumberLong(3)
},
"ok" : 0,
"errmsg" : "node is not in primary or recovering state",
"code" : 13436,
"codeName" : "NotMasterOrSecondary"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

测试发现,仲裁节点不能进行数据的查看。


主节点的选举原则


​MongoDB​​在副本集中,会自动进行主节点的选举,主节点选举的触发条件:

1)主节点故障

2)主节点网络不可达(默认心跳信息为10秒)

3)人工干预(​​rs.stepDown(600)​​)

一旦触发选举,就要根据一定规则来选主节点。

选举规则是根据票数来决定谁获胜:

  • 票数最高,且获得了“大多数”成员的投票支持的节点获胜
  • 若票数相同,且都获得了“大多数”成员的投票支持的,数据新的节点获胜。数据的新旧是通过操作日志​​oplog​​来对比的。

在获得票数的时候,优先级(​​priority​​​)参数影响重大。可以通过设置优先级(​​priority​​​)来设置额外票数。优先级即权重,取值为​​0-1000​​​,相当于可额外增加0-1000的票数,优先级的值越大,就越有可能获得多数成员的投票(​​votes​​)数。指定较高的值可使成员更有资格成为主要成员,更低的值可使成员更不符合条件。


默认情况下,优先级都为1

myrs:SECONDARY> rs.conf()
{
"_id" : "myrs",
"version" : 3,
"term" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.10.11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.10.10.11:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.10.10.11:27019",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"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("5fa909a07e34d2b6fa2e54a1")
}
}

可以通过上面的配置信息看出,主节点和副本节点的优先级各位1,即,默认可以认为都已经有了一票。单选举节点,优先级是0,(注意:官方说了,选举节点的优先级必须是0,不能是别的值。即不具备选举权,单具有投票权)


故障测试

副本节点故障测试

关闭27018副本节点:

# ps -ef |grep mongo
root 23945 1 0 Nov10 ? 00:22:10 mongod -f /data/replica_sets/myrs_27017/mongod.conf
root 24174 1 0 Nov10 ? 00:22:33 mongod -f /data/replica_sets/myrs_27018/mongod.conf
root 24441 1 0 Nov10 ? 00:15:15 mongod -f /data/replica_sets/myrs_27019/mongod.conf
# kill -2 24174

进入主节点27017写入数据

# mongo --host=10.10.10.11 --port=27017
---
myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db.collectest.insert({"name":"小白", "sex":"女", "age":20, "userid":1002, "createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })

启动27018副本节点,并验证数据

# mongod -f /data/replica_sets/myrs_27018/mongod.conf
# mongo --host=10.10.10.11 --port=27018
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> show dbs
admin 0.000GB
collectest 0.000GB
config 0.000GB
local 0.001GB
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "张三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
{ "_id" : ObjectId("5facec10cca53c48154d261c"), "name" : "小白", "sex" : "女", "age" : 20, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:02:24.915Z") }

通过上面的测试验证,可以发现,当副本节点故障时,主节点读写不受影响,并且在副本节点恢复后,主节点写入的数据,会自动同步给从节点。


主节点故障测试

关闭主节点27017

# ps -ef |grep mongo
root 23945 1 0 Nov10 ? 00:22:34 mongod -f /data/replica_sets/myrs_27017/mongod.conf
root 24441 1 0 Nov10 ? 00:15:27 mongod -f /data/replica_sets/myrs_27019/mongod.conf
root 50521 1 1 16:04 ? 00:00:16 mongod -f /data/replica_sets/myrs_27018/mongod.conf
# kill -2 23945

登陆副本节点查看集群状态

# mongo --host=10.10.10.11 --port=27018
myrs:PRIMARY>
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-12T08:30:11.791Z"),
"myState" : 1,
"term" : NumberLong(7),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"lastCommittedWallTime" : ISODate("2020-11-12T08:28:31.364Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"readConcernMajorityWallTime" : ISODate("2020-11-12T08:28:31.364Z"),
"appliedOpTime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"durableOpTime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"lastAppliedWallTime" : ISODate("2020-11-12T08:30:02.141Z"),
"lastDurableWallTime" : ISODate("2020-11-12T08:30:02.141Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1605169711, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-12T08:28:42.106Z"),
"electionTerm" : NumberLong(7),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-11-12T08:28:42.125Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"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("2020-11-12T08:30:10.207Z"),
"lastHeartbeatRecv" : ISODate("2020-11-12T08:28:31.380Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 10.10.10.11:27017 :: caused by :: Connection refused",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 5
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1537,
"optime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2020-11-12T08:30:02Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1605169722, 1),
"electionDate" : ISODate("2020-11-12T08:28:42Z"),
"configVersion" : 3,
"configTerm" : 7,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "10.10.10.11:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1534,
"lastHeartbeat" : ISODate("2020-11-12T08:30:10.162Z"),
"lastHeartbeatRecv" : ISODate("2020-11-12T08:30:10.165Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 7
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1605169802, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1605169802, 1)
}

通过上面查看之前的27018副本节点已经变为了主节点,接下来在副本节点中插入数据

myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db.collectest.insert({"name":"小嘿", "sex":"女", "age":21, "userid":1002, "createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })

此时,重新启动27017节点,并查看状态

# mongod -f /data/replica_sets/myrs_27017/mongod.conf
# mongo --host=10.10.10.11 --port=27017
myrs:SECONDARY>
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "张三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
{ "_id" : ObjectId("5facec10cca53c48154d261c"), "name" : "小白", "sex" : "女", "age" : 20, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:02:24.915Z") }
{ "_id" : ObjectId("5facf32dfb5fe16aaf699d7d"), "name" : "小嘿", "sex" : "女", "age" : 21, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:32:45.919Z") }

通过上面测试发现,此时27017节点已经变成了副本节点,并且配置查看权限后,可以对数据进行查看操作。

通过上面的测试,主从自动切换,故而试下了高可用。


仲裁节点和主节点故障

先关掉仲裁节点27019

关掉现在的主节点27018

登陆27017后,发现,27017仍然是从节点,副本集中没有主节点了,导致此时,副本集只是读状态,无法写入。

为啥不选举了?因为27017的票数,没有获得大多数,即没有大于等于2,它只有默认的一票(优先级是1)

如果要触发选举,随便加入一个成员即可。

  • 如果只加入27019仲裁节点成员,则主节点一定是27017,因为没得选了,仲裁节点不参与选举,但参与投票
  • 如果只加入27018节点,会发起选举。因为27017和27018都是两票,则按照谁数据新,谁当主节点。

人生是条无名的河,是浅是深都要过; 人生是杯无色的茶,是苦是甜都要喝; 人生是首无畏的歌,是高是低都要唱。