简介
MongoDB的副本集(Replica Set)是一组维护相同数据集的mongod服务,副本集可提供冗余和高可用性,是所有生产部署的基础。
也就是说,副本集类似于有自动故障恢复功能的主从集群,通俗的讲就是用多台集群进行同一数据的异步同步,从而使多台机器拥有同一数据的多个副本,并且当主库挂掉时在不需要用户干预的情况下自动切换其他备份服务器做主库。而且还可以利用副本服务器做只读服务器,实现读写分离,提高负载。
- 冗余和数据可用性
复制提供冗余并提高数据可用性。通过在不同数据库服务器上提供多个数据副本,复制可提供一定级别的容错功能,以防止丢失单个数据库服务器。
在某些情况下,复制可以提供增加的读取性能,因为客户端可以将读取操作发送到不同的服务上,在不同数据中心维护数据副本可以增加分布式应用程序的数据位置和可用性。您还可以为专用目的维护其他副本,例如灾难恢复,报告或备份。 - MongoDB的复制
副本集是一组维护相同数据集的mongod实例。副本集包含多个数据承载节点和可选的一个仲裁节点。在承载数据的节点中,一个且仅一个成员被视为主节点,而其他节点被视为次要(从)节点。
主节点接收所有写操作。副本集只能有一个主节点;虽然在某些情况下,另一个mongod实例可能暂时认为自己也是主节点。主节点基类器操作日志中的数据集的所有更改,即oplog。
从节点复制主节点的oplog并将操作应用于其数据集,以使从节点的数据集反映主节点的数据集。如果主节点不再,则符号条件的从节点进行选举,选举出新的主节点。 - 主从复制和副本集区别
主从集群和副本集最大的区别就是副本集没有固定的主节点;整个集群会选举出一个主节点,当其挂掉后,又在剩余的从节点中选中其他节点为主节点,副本集总有一个活跃点(主、primary)和一个或多个备份节点(从、secondary)。
副本集的三个角色
副本集有两种类型三种角色
两种类型:
- 主节点(primary)类型:数据操作的主要连接点,可读写。
- 次要(从、辅助)节点(secondaries)类型:数据冗余备份节点,可以读或者选举。
三种角色:
主要成员(primary):主要接收所有写操作。就是主节点。
副本成员(Replicate):从节点通过复制操作以维护相同的数据集,即备份数据,不可写操作,但是可以读操作(但需要配置)。是默认的一种从节点类型。
仲裁者(arbiter):不保留任何数据的副本,只具有投票选举作用。当然也可以将仲裁服务器维护为副本集的一部分,即副本成员同时也可以是仲裁者,也是一种从节点类型。
副本集架构目标
副本集的创建
第一步:创建主节点
- 建立存放数据和日志的目录
# ----------------------------myrs
# 主节点
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27017/log
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27017/data/db
- 新建或修改配置文件:
[root@localhost mongodb]# cp single/mongod.conf replica_sets/myrs_27017/mongod.conf
[root@localhost mongodb]# vim replica_sets/myrs_27017/mongod.conf
- myrs_27017:
systemLog:
# MongoDB发送所有日志输出的目标指定为文件
destination: file
# Mongod或Mongos应向其发送所有诊断日志记录信息的日志文件的路径
path: "/opt/mongodb/replica_sets/myrs_27017/log/mongod.log"
# Mongos或Mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
# Mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
dbPath: "/opt/mongodb/replica_sets/myrs_27017/data/db"
journal:
# 启用或禁用持久性日志以确保数据文件保持有效和可恢复。
enabled: true
processManagement:
# 启用在后台运行mongos或mongod进程的守护进程模式
fork: true
# 指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
pidFilePath:"/opt/mongodb/replica_sets/myrs_27017/log/mongod.pid"
net:
# 服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
# bindIpAll: true
# 服务实例绑定的ip
bindIp: localhost,192.168.174.128
# 绑定的端口
port: 27017
replication:
# 副本集的名称
replSetName: myrs
- 启动节点服务
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongod -f ./replica_sets/myrs_27017/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14312
child process started successfully, parent exiting
第二步:创建副本节点
- 建立存放数据和日志的目录
# ----------------------------myrs
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27018/log
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27018/data/db
- 新建或修改配置文件:
[root@localhost mongodb]# cp /opt/mongodb/replica_sets/myrs_27017/mongod.conf /opt/mongodb/replica_sets/myrs_27018/mongod.conf
[root@localhost mongodb]# vim replica_sets/myrs_27018/mongod.conf
- myrs_27018:
systemLog:
# MongoDB发送所有日志输出的目标指定为文件
destination: file
# Mongod或Mongos应向其发送所有诊断日志记录信息的日志文件的路径
path: "/opt/mongodb/replica_sets/myrs_27018/log/mongod.log"
# Mongos或Mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
# Mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
dbPath: "/opt/mongodb/replica_sets/myrs_27018/data/db"
journal:
# 启用或禁用持久性日志以确保数据文件保持有效和可恢复。
enabled: true
processManagement:
# 启用在后台运行mongos或mongod进程的守护进程模式
fork: true
# 指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
pidFilePath:"/opt/mongodb/replica_sets/myrs_27018/log/mongod.pid"
net:
# 服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
# bindIpAll: true
# 服务实例绑定的ip
bindIp: localhost,192.168.174.128
# 绑定的端口
port: 27018
replication:
# 副本集的名称
replSetName: myrs
- 启动节服务
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongod -f ./replica_sets/myrs_27018/mongod.conf about to fork child process, waiting until server is ready for connections.
forked process: 14554
child process started successfully, parent exiting
[root@localhost mongodb]#
第三步:创建仲裁节点
- 建立存放数据和日志的目录
# ----------------------------myrs
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27019/log
[root@localhost single]# mkdir -p /opt/mongodb/replica_sets/myrs_27019/data/db
- 仲裁节点
新建或修改配置文件:
[root@localhost mongodb]# cp /opt/mongodb/replica_sets/myrs_27017/mongod.conf /opt/mongodb/replica_sets/myrs_27019/mongod.conf
[root@localhost mongodb]# vim replica_sets/myrs_27019/mongod.conf
- myrs_27019:
systemLog:
# MongoDB发送所有日志输出的目标指定为文件
destination: file
# Mongod或Mongos应向其发送所有诊断日志记录信息的日志文件的路径
path: "/opt/mongodb/replica_sets/myrs_27019/log/mongod.log"
# Mongos或Mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾
logAppend: true
storage:
# Mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
dbPath: "/opt/mongodb/replica_sets/myrs_27019/data/db"
journal:
# 启用或禁用持久性日志以确保数据文件保持有效和可恢复。
enabled: true
processManagement:
# 启用在后台运行mongos或mongod进程的守护进程模式
fork: true
# 指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
pidFilePath:"/opt/mongodb/replica_sets/myrs_27019/log/mongod.pid"
net:
# 服务实例绑定所有IP,有副作用,副本集初始化的时候,节点名字会自动设置为本地域名,而不是ip
# bindIpAll: true
# 服务实例绑定的ip
bindIp: localhost,192.168.174.128
# 绑定的端口
port: 27019
replication:
# 副本集的名称
replSetName: myrs
- 启动仲裁节点服务
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongod -f ./replica_sets/myrs_27019/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14554
child process started successfully, parent exiting
[root@localhost mongodb]#
第四步:初始化配置副本集合主节点
使用客户端命令连接任意一个节点,但这里尽量要连接主节点(27017节点):
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongo --host=localhost --port=27017
MongoDB shell version v4.4.4
connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("217e8b5b-d668-4b6a-984d-26de60b73c60") }
MongoDB server version: 4.4.4
---
结果,连接上之后,很多命令无法使用,比如 show dbs 等,必须初始化副本集才行。
准备初始化新的副本集:
语法:
rs.initiate(configuration)
选项:
Parameter | Type | Description |
---|---|---|
configuration | document | 可选的,为新副本集指定一个配置。如果没有指定配置,mongodb使用一个缺省的副本集配置。 |
- 示例:
使用默认的配置来初始化副本集:
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "192.168.174.128:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1619085858, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1619085858, 1)
}
myrs:SECONDARY>
myrs:PRIMARY>
提示:
- “ok”的值为1,说明创建成功。
- 命令行提示符发生变化,变成了一个从节点角色,次数默认不能读写,稍等片刻,回车,变成主节点。
第五步:查看副本集的配置内容
说明:
返回包含当前副本集配置的文档。
语法:
rs.conf(configuration)
rs.config()是该方法的别名。
configuration:可选,如果没有配置,则使用默认主节点的配置。
示例:
myrs:PRIMARY> rs.conf()
{
"_id" : "myrs",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.174.128: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("60814a2226e336b0cc13c0d3")
}
}
myrs:PRIMARY>
说明:
- “_id”:“myrs”:副本集的配置数据存储的主键值,默认就是副本集的名字。
- “members”:副本集成员数组,此时只有一个:“host” : “192.168.174.128:27017”,该成员不是仲裁节点: “arbiterOnly” : false,优先级(权重值):“priority” : 1。
- “settings”:副本集的参数配置。
提示:副本集配置的查看命令,本质是查询的是system.replset的表中的数据:
查看副本集状态
语法:
rs.status()
示例:
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2021-04-22T10:15:06.491Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 1,
"writeMajorityCount" : 1,
"votingMembersCount" : 1,
"writableVotingMembersCount" : 1,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1619086498, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-04-22T10:14:58.597Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1619086498, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-04-22T10:14:58.597Z"),
"appliedOpTime" : {
"ts" : Timestamp(1619086498, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1619086498, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-04-22T10:14:58.597Z"),
"lastDurableWallTime" : ISODate("2021-04-22T10:14:58.597Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1619086448, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-04-22T10:04:18.511Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1619085858, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2021-04-22T10:04:18.531Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-04-22T10:04:18.544Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.174.128:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 741,
"optime" : {
"ts" : Timestamp(1619086498, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-04-22T10:14:58Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1619085858, 2),
"electionDate" : ISODate("2021-04-22T10:04:18Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1619086498, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1619086498, 1)
}
myrs:PRIMARY>
第七步:添加副本从节点
在主节点添加从节点,将其他成员加入到副本集中
语法:
rs.add(host,arbiterOnly)
选项:
Parameter | Type | Description |
---|---|---|
host | string or document | 要添加到副本集的新成员。指定为字符串或者配置文档:1)如果是一个字符串,则需要指定新成员的主机名和可选的端口号;2)如果是一个文档,请指定在members数组中找到的副本集成员配置文档。你必须在成员配置文档中指定主机字段。有关文档配置字段的说明,详见下方文档:“主机成员的配置文档” |
arbiterOnly | boolean | 可选的。仅在值为字符串时适用。如果为true,则添加的主机是仲裁者。 |
示例:
将27018的副本节点添加到副本集中:
myrs:PRIMARY> rs.add("192.168.174.128:27018")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1619087317, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1619087317, 1)
}
说明:
- “ok”:1,说明添加成功。
第八步:添加仲裁从节点
添加一个仲裁节点到副本集
语法:
rs.addArb(host)
将27019的仲裁节点添加到副本集中:
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1619087628, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1619087614, 1)
}
myrs:PRIMARY>
说明:
- “ok”:1,说明添加成功。
查看副本的状态
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2021-04-22T10:36:15.227Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 2,
"votingMembersCount" : 4,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-04-22T10:36:08.707Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-04-22T10:36:08.707Z"),
"appliedOpTime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-04-22T10:36:08.707Z"),
"lastDurableWallTime" : ISODate("2021-04-22T10:36:08.707Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1619087718, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-04-22T10:04:18.511Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1619085858, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2021-04-22T10:04:18.531Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-04-22T10:04:18.544Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.174.128:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2010,
"optime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-04-22T10:36:08Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1619085858, 2),
"electionDate" : ISODate("2021-04-22T10:04:18Z"),
"configVersion" : 4,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.174.128:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 457,
"optime" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1619087768, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-04-22T10:36:08Z"),
"optimeDurableDate" : ISODate("2021-04-22T10:36:08Z"),
"lastHeartbeat" : ISODate("2021-04-22T10:36:14.514Z"),
"lastHeartbeatRecv" : ISODate("2021-04-22T10:36:13.487Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.174.128:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 4,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "192.168.172.128:27019",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"lastHeartbeat" : ISODate("2021-04-22T10:36:12.198Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Couldn't get a connection within the time limit of 1000ms",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1,
"configTerm" : -1
},
{
"_id" : 3,
"name" : "192.168.174.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 160,
"lastHeartbeat" : ISODate("2021-04-22T10:36:14.514Z"),
"lastHeartbeatRecv" : ISODate("2021-04-22T10:36:13.519Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 4,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1619087768, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1619087768, 1)
}
myrs:PRIMARY>
副本集的数据读写操作
目标:测试三个不同角色的节点的数据读写情况。
- 登录主节点27017,写入和读取数据
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongo --host=localhost --port=27017
myrs:PRIMARY> use articledb
switched to db articledb
myrs:PRIMARY> db
articledb
myrs:PRIMARY> db.comment.insert({"articleid":"100000","content":"今天天气真好,阳光明媚","userid":"1001","nickname":"rose","createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })
myrs:PRIMARY> db.comment.find()
{ "_id" : ObjectId("608154d20a85cb7a99a65f93"), "articleid" : "100000", "content" : "今天天气真好,阳光明媚", "userid" : "1001", "nickname" : "rose", "createdatetime" : ISODate("2021-04-22T10:49:54.233Z") }
myrs:PRIMARY>
- 登录从节点27018
[root@localhost ~]$ /usr/local/mongoDB/bin/mongo --host localhost --port 27018
myrs:SECONDARY> show dbs;
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("608149cceb6baf640e5d36eb"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1619088758, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1619088758, 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.slaveOk()
# 或者
rs.slaveOk(true)
提示:
该命令是db.getMongo().setSlaveOk()的简化命令。
myrs:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
myrs:SECONDARY> show dbs;
admin 0.000GB
articledb 0.000GB
config 0.000GB
local 0.000GB
myrs:SECONDARY> use articledb
switched to db articledb
myrs:SECONDARY> db.comment.find()
{ "_id" : ObjectId("608154d20a85cb7a99a65f93"), "articleid" : "100000", "content" : "今天天气真好,阳光明媚", "userid" : "1001", "nickname" : "rose", "createdatetime" : ISODate("2021-04-22T10:49:54.233Z") }
myrs:SECONDARY>
限制可以实现读写分离了,让主节点插入数据,让从来读取数据。
如果要取消作为奴隶节点的读权限:
myrs:SECONDARY> rs.slaveOk(false)
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
myrs:SECONDARY> db.comment.find()
Error: error: {
"topologyVersion" : {
"processId" : ObjectId("608149cceb6baf640e5d36eb"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1619089348, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1619089348, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
myrs:SECONDARY>
- 仲裁者节点,不存放任何业务数据的,可以登录查看
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongo --host localhost --port 27019
MongoDB shell version v4.4.4
connecting to: mongodb://localhost:27019/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a5daf004-9323-416d-b51e-8379d96e454f") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting:
2021-04-22T18:03:01.354+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-04-22T18:03:01.354+08:00: You are running this process as the root user, which is not recommended
2021-04-22T18:03:01.354+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-04-22T18:03:01.354+08:00: Soft rlimits too low
2021-04-22T18:03:01.354+08:00: currentValue: 1024
2021-04-22T18:03:01.354+08:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
myrs:ARBITER> rs.secondaryOk()
myrs:ARBITER> use local
switched to db local
myrs:ARBITER> show collections
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100347.html