美文网首页mongodb
Anolis8部署mongodb复制集RS(Replicatio

Anolis8部署mongodb复制集RS(Replicatio

作者: 前浪浪奔浪流 | 来源:发表于2023-01-16 17:31 被阅读0次

Anolis8部署mongodb复制集RS(ReplicationSet),通过多实例实现,即3台服务器部署3个实例。

MongoDB中的复制集(也被称为副本)是一组维护相同数据集的mongod进程。副本集提供冗余性及和高可用,是所有生产部署的基础。简单来说,复制集有多台MongoDB组成的一个集群,集群中有一个主节点(Primary)和N个副本节点(Secondary)等,它们有相同的数据库,假如主MongoDB服务器或者MongoDB实例Down机之后,其它的副本服务器可以继续提供服务,实现数据的高可用及可靠性。

1、 mongodb复制集架构

1.1 角色介绍:

术语 类型 描述
Primary 主节点 负责整个集群的读写操作,包含了所有改变操作的日志
Secondary 备节点 同步主服务器所有的数据,负责集群的读取请求,主服务器宕机可以称为主节点
Arbiter 仲裁者 在主节点宕机后只进行投票,不参与选举,不同步主节点数据

1.2 mongodb的复制集架构主要分为两种,普通复制集和选主复制集。

1、普通复制集架构:


image.png

主节点故障后,复制集变化过程


image.png

2、选主复制集架构(仲裁节点架构)


image.png

主节点故障后,复制集变化过程


image.png

1.3 基本原理

复制集基本架构为1主2从或1主1从1Arbiter 。而且mongodb复制集自带互相监控投票机制,其内部算法为【MongoD(Raft)】和【Paxos(mysql MGR )】算法基本相同。
如果发生主库宕机,复制集内部会根据Raft算法进行投票选举,选择一个新的主库替代原有主库对外提供服务。同时复制集会自动通知客户端程序,主库已经发生切换了,应用就会连接到新的主库。

2、 复制集架构规划

可以使用3个节点或多实例实现。本案例通过多实例实现。

2.1 多实例规划

主节点:192.168.100.161:27017
从节点:192.168.100.162:27017
投票节点:192.168.100.163:27017

配置文件内容:

主节点:192.168.100.161:27017
systemLog:
  destination: file
  path: "/data/mongodb/log/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /data/mongodb/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 192.168.100.161,127.0.0.1
replication:
  oplogSizeMB: 2048
  replSetName: myReplSet

从节点:192.168.100.162:27017
systemLog:
  destination: file
  path: "/data/mongodb/log/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /data/mongodb/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 192.168.100.162,127.0.0.1
replication:
  oplogSizeMB: 2048
  replSetName: myReplSet

投票节点:192.168.100.163:27017
systemLog:
  destination: file
  path: "/data/mongodb/log/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /data/mongodb/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 192.168.100.163,127.0.0.1
replication:
  oplogSizeMB: 2048
  replSetName: myReplSet

保存退出后启动mongod服务

  • 如果报错:
    MongoDB Requested option conflicts with current storage engine option for directoryPerDB

是因为之前启动时没设置directoryperdb ,它默认是false.
详细解释https://blog.51cto.com/alwaysyunwei/1596238
解决办法如果之前的数据可以放弃直接删除dbPath目录下的所有数据,然后重启就正常了。
如果不能放弃建议做数据迁移。

2.2启动mongod,并查看进程和端口

systemctl start mongod

systemctl status mongod.service -l

ps -ef|grep mongodb

netstat  -lntp|grep 270

3、 配置复制集

3.1 配置普通复制集【1主2从】

1、登陆到mongodb数据库
[mongod@mysql-master data]$ mongo --port 27017 admin

2、配置mongodb复制集
config = {_id: 'myReplSet', members: [
                          {_id: 0, host: '192.168.100.161:27017'},
                          {_id: 1, host: '192.168.100.162:27017'},
                          {_id: 2, host: '192.168.100.163:27017'}]
          } 
               
rs.initiate(config) 

注:
admin为验证库。
集群过程中会出现两种角色PRIMARY和SECONDARY,为配置成功。
myReplSet:SECONDARY> 
myReplSet:PRIMARY> 

3、查看复制集集群的状态
myReplSet:PRIMARY> rs.status()

3.2、配置选择复制集【1主1从1个arbiter】

[root@mysql8-1 data]# mongo --port 27017 admin
>
config = {_id: 'myReplSet', members: [
                          {_id: 0, host: '192.168.100.161:27017'},
                          {_id: 1, host: '192.168.100.162:27017'},
                          {_id: 2, host: '192.168.100.163:27017',"arbiterOnly":true}]
          }   
       
rs.initiate(config) 

注:myReplSet要和配置文件中的复制集模块一致。
       arbiterOnly设置为true.

执行结果

{_id: 'myReplSet', members: [
             {_id: 0, host: '192.168.100.161:27017'},
             {_id: 1, host: '192.168.100.162:27017'},
             {_id: 2, host: '192.168.100.163:27017',"arbiterOnly":true}]
          }                
{
    "_id" : "myReplSet",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.100.161:27017"
        },
        {
            "_id" : 1,
            "host" : "192.168.100.162:27017"
        },
        {
            "_id" : 2,
            "host" : "192.168.100.163:27017",
            "arbiterOnly" : true
        }
    ]
}

> rs.initiate(config)
{ "ok" : 1 }

myReplSet:SECONDARY> rs.status()
{
    "set" : "myReplSet",
    "date" : ISODate("2022-02-19T09:32:12.541Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "votingMembersCount" : 3,
    "writableVotingMembersCount" : 2,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1645263130, 1),
            "t" : NumberLong(1)
        },
        "lastCommittedWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1645263130, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1645263130, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1645263130, 1),
            "t" : NumberLong(1)
        },
        "lastAppliedWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
        "lastDurableWallTime" : ISODate("2022-02-19T09:32:10.248Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1645263100, 4),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "electionTimeout",
        "lastElectionDate" : ISODate("2022-02-19T09:31:40.196Z"),
        "electionTerm" : NumberLong(1),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1645263090, 1),
            "t" : NumberLong(-1)
        },
        "numVotesNeeded" : 2,
        "priorityAtElection" : 1,
        "electionTimeoutMillis" : NumberLong(10000),
        "numCatchUpOps" : NumberLong(0),
        "newTermStartDate" : ISODate("2022-02-19T09:31:40.232Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2022-02-19T09:31:40.766Z")
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.100.161:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 455,
            "optime" : {
                "ts" : Timestamp(1645263130, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2022-02-19T09:32:10Z"),
            "lastAppliedWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
            "lastDurableWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "Could not find member to sync from",
            "electionTime" : Timestamp(1645263100, 1),
            "electionDate" : ISODate("2022-02-19T09:31:40Z"),
            "configVersion" : 1,
            "configTerm" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "192.168.100.162:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 42,
            "optime" : {
                "ts" : Timestamp(1645263130, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1645263130, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2022-02-19T09:32:10Z"),
            "optimeDurableDate" : ISODate("2022-02-19T09:32:10Z"),
            "lastAppliedWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
            "lastDurableWallTime" : ISODate("2022-02-19T09:32:10.248Z"),
            "lastHeartbeat" : ISODate("2022-02-19T09:32:12.221Z"),
            "lastHeartbeatRecv" : ISODate("2022-02-19T09:32:11.232Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "192.168.100.161:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : 1
        },
        {
            "_id" : 2,
            "name" : "192.168.100.163:27017",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 42,
            "lastHeartbeat" : ISODate("2022-02-19T09:32:12.222Z"),
            "lastHeartbeatRecv" : ISODate("2022-02-19T09:32:12.234Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1645263130, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1645263130, 1)
}

执行"rs.status"命令可以查看复制集中各成员节点的状态。其中,字段"health"是实例正常与否的标记,“1”代表状态正常,“0”代表状态异常;字段"state"用于表明实例在复制集中充当的角色,“1”代表primary,“0”代表slave;字段"stateStr"进一步用文字描述了此实例在复制集中的角色,本例中的值是"PRIMARY"。

3.3、复制集常用管理操作

rs.status();        //查看复制集状态
rs.isMaster();      // 查看当前是否为主节点
rs.conf();        //查看复制集配置信息

1、查看复制集状态
myReplSet:PRIMARY> rs.status()
{
    "set" : "myReplSet",
    "date" : ISODate("2021-09-24T09:35:30.813Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1632476127, 1),
            "t" : NumberLong(1)
        },
        "lastCommittedWallTime" : ISODate("2021-09-24T09:35:27.923Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1632476127, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityWallTime" : ISODate("2021-09-24T09:35:27.923Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1632476127, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1632476127, 1),
            "t" : NumberLong(1)
        },
        "lastAppliedWallTime" : ISODate("2021-09-24T09:35:27.923Z"),
        "lastDurableWallTime" : ISODate("2021-09-24T09:35:27.923Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1632476117, 1),
    "lastStableCheckpointTimestamp" : Timestamp(1632476117, 1),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "electionTimeout",
        "lastElectionDate" : ISODate("2021-09-24T09:21:17.879Z"),
        "electionTerm" : NumberLong(1),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1632475267, 1),
            "t" : NumberLong(-1)
        },
        "numVotesNeeded" : 2,
        "priorityAtElection" : 1,
        "electionTimeoutMillis" : NumberLong(10000),
        "numCatchUpOps" : NumberLong(0),
        "newTermStartDate" : ISODate("2021-09-24T09:21:17.905Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2021-09-24T09:21:18.701Z")
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.21.209.123:28017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1645,
            "optime" : {
                "ts" : Timestamp(1632476127, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-09-24T09:35:27Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1632475277, 1),
            "electionDate" : ISODate("2021-09-24T09:21:17Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.21.209.123:28018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 863,
            "optime" : {
                "ts" : Timestamp(1632476127, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1632476127, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-09-24T09:35:27Z"),
            "optimeDurableDate" : ISODate("2021-09-24T09:35:27Z"),
            "lastHeartbeat" : ISODate("2021-09-24T09:35:29.897Z"),
            "lastHeartbeatRecv" : ISODate("2021-09-24T09:35:29.171Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.21.209.123:28017",
            "syncSourceHost" : "172.21.209.123:28017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "172.21.209.123:28019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 863,
            "optime" : {
                "ts" : Timestamp(1632476127, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1632476127, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-09-24T09:35:27Z"),
            "optimeDurableDate" : ISODate("2021-09-24T09:35:27Z"),
            "lastHeartbeat" : ISODate("2021-09-24T09:35:29.896Z"),
            "lastHeartbeatRecv" : ISODate("2021-09-24T09:35:30.673Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.21.209.123:28017",
            "syncSourceHost" : "172.21.209.123:28017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632476127, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1632476127, 1)
}
myReplSet:PRIMARY> 

2、查看当前节点是否为主节点
myReplSet:PRIMARY> rs.isMaster()
{
    "hosts" : [
        "172.21.209.123:28017",
        "172.21.209.123:28018",
        "172.21.209.123:28019"
    ],
    "setName" : "myReplSet",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "172.21.209.123:28017",
    "me" : "172.21.209.123:28017",
    "electionId" : ObjectId("7fffffff0000000000000001"),
    "lastWrite" : {
        "opTime" : {
            "ts" : Timestamp(1632476167, 1),
            "t" : NumberLong(1)
        },
        "lastWriteDate" : ISODate("2021-09-24T09:36:07Z"),
        "majorityOpTime" : {
            "ts" : Timestamp(1632476167, 1),
            "t" : NumberLong(1)
        },
        "majorityWriteDate" : ISODate("2021-09-24T09:36:07Z")
    },
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 100000,
    "localTime" : ISODate("2021-09-24T09:36:10.629Z"),
    "logicalSessionTimeoutMinutes" : 30,
    "connectionId" : 6,
    "minWireVersion" : 0,
    "maxWireVersion" : 8,
    "readOnly" : false,
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632476167, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1632476167, 1)
}
myReplSet:PRIMARY> 

字段说明:
setName:指明复制集的名称,本例中的取值是"myReplSet"。
ismaster:指明当前连接的实例是否是primary角色。它是一个布尔类型:"true"说明此实例是primary角色,"false"说明此实例不是primary角色。
secondary:指明当前连接的实例是否是slave角色。它是一个布尔类型:"true"说明此实例是slave角色,"false"说明此实例不是slave角色。
hosts:指明Replica Sets各成员IP及端口信息。
ok:表明复制集的状态。“1”说明状态正常,“0”说明状态异常。

3、查看复制集配置信息

myReplSet:PRIMARY> rs.conf()
{
    "_id" : "myReplSet",
    "version" : 1,
    "protocolVersion" : NumberLong(1),
    "writeConcernMajorityJournalDefault" : true,
    "members" : [
        {
            "_id" : 0,
            "host" : "172.21.209.123:28017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "172.21.209.123:28018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "172.21.209.123:28019",
            "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("614d98833680adfa6bcd2c0e")
    }
}
myReplSet:PRIMARY> 


4、 复制集节点的添加和删除

1、添加删除常用操作
rs.remove("节点ip:port"); // 删除一个节点
rs.add("节点ip:port"); // 新增从节点
rs.addArb("节点ip:port"); // 新增仲裁节点

2、案例操作
连接到主节点
[mongod@mysql-master data]$   mongo --port 27017 admin

2.1 添加仲裁节点
myReplSet:PRIMARY> rs.addArb("172.21.209.123:28020")
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632476954, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1632476954, 1)
}

添加后查看状态:myReplSet:PRIMARY> rs.isMaster()
2.2 删除一个节点
myReplSet:PRIMARY> rs.remove("172.21.209.123:28019");
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632477110, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1632477110, 1)
}

删除后查看状态:myReplSet:PRIMARY> rs.isMaster()

2.3 新增加从节点
myReplSet:PRIMARY> rs.add("1172.21.209.123:28019")
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632477186, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1632477186, 1)
}

新增后查看状态:myReplSet:PRIMARY> rs.isMaster()

5、复制集的特殊节点

5.1 介绍和架构

  • arbiter节点:主要负责选主过程投票,但不存储任何数据,也不向客户端提供任何服务
  • hidden节点:隐藏节点,不参与选主,也不向客户端提供任何服务。
  • delay节点:延时节点,数据落后于主库一段时间,因为数据是延时的,也不应该提供服务或参与选主,所以通常会配合hidden(隐藏)
    一般情况下会将delay+hidden一起配置使用


    image.png
    image.png

5.2 配置延时节点(组合使用:delay+hidden)

1、配置延时
cfg=rs.conf() 
cfg.members[1].priority=0
cfg.members[1].hidden=true
cfg.members[1].slaveDelay=120
rs.reconfig(cfg)    

2、恢复延时配置
cfg=rs.conf() 
cfg.members[1].priority=1
cfg.members[1].hidden=false
cfg.members[1].slaveDelay=0
rs.reconfig(cfg)    

3、配置成功后,通过以下命令查询配置后的属性
rs.conf(); 

注:通过rs.status()可以查看到members[1]是一个数组,下标从0开始,选择需要配置的节点,members[1]表示第三个节点。

6、 复制集其他命令

1、查看副本集的配置信息
myReplSet:PRIMARY> rs.conf()

2、查看副本集各成员的状态
myReplSet:PRIMARY> rs.status()

3、副本集角色切换(不要人为随便操作)
myReplSet:PRIMARY> rs.stepDown()
注:
myReplSet:PRIMARY> rs.freeze(300)  //锁定从,使其不会转变成主库
freeze()和stepDown单位都是秒。

4、设置副本节点可读:在副本节点执行
登陆从节点
[mongod@mysql-master data]$  mongo --port 28018 admin

myReplSet:SECONDARY> rs.slaveOk()
eg:
myReplSet:SECONDARY> use student;
switched to db student
myReplSet:SECONDARY> db.createCollection('a')
{
    "operationTime" : Timestamp(1632478227, 1),
    "ok" : 0,
    "errmsg" : "not master",
    "code" : 10107,
    "codeName" : "NotWritablePrimary",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1632478227, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

5、查看副本节点(监控主从延时)
myReplSet:SECONDARY> rs.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 172.21.209.123:28018
    syncedTo: Fri Sep 24 2021 06:13:08 GMT-0400 (EDT)
    0 secs (0 hrs) behind the primary 
source: 1172.21.209.123:28019
    syncedTo: Wed Dec 31 1969 19:00:00 GMT-0500 (EST)
    1632478388 secs (453466.22 hrs) behind the primary 

MongoDB 日常运维实践总结
https://www.jianshu.com/p/b43d347d8518
MongoDB备份恢复及导入导出
https://www.jianshu.com/p/bd936c6f8e72

相关文章

  • Anolis8部署mongodb复制集RS(Replicatio

    Anolis8部署mongodb复制集RS(ReplicationSet),通过多实例实现,即3台服务器部署3个实...

  • 【MongoDB】— CentOS76部署mongodb复制集R

    centos7.6部署mongodb复制集RS(ReplicationSet),通过多实例实现,即一台服务器部署4...

  • mongodb集群--复制集RS--分布式

    1. MongoDB复制集RS(ReplicationSet) 1.1 基本原理 1.2 Replication ...

  • mongodb复制集RS

    1. 基本原理: 基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB) Paxos(mys...

  • mongodb复制集RS

    1. 基本原理: 基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB) Paxos(mys...

  • mongodb - root密码找回

    mongodb复制集root 账号密码忘记找回 环境要求: 可以登录部署机器 原理 mongodb的配置文件中 s...

  • MongoDB复制集部署

    MongoDB的高可用特使是用复制集实现的,本文介绍如何在CentOS7快速搭建一个复制集 部署单节点版本 yum...

  • mongodb——分布式

    复制集 mongodb在集群环境中,通过复制的形式对数据进行冗余。mongodb复制集有Primary、Secon...

  • MongoDB 分片集群技术

    1.1 MongoDB复制集简介 一组Mongodb复制集,就是一组mongod进程,这些进程维护同一个数据集合。...

  • mongodb4.0.2 复制集主从部署

    介绍 复制集(Replica Sets),是一个基于主/从复制机制的复制功能,进行同一数据的异步同步,从而使多台机...

网友评论

    本文标题:Anolis8部署mongodb复制集RS(Replicatio

    本文链接:https://www.haomeiwen.com/subject/sxxmcdtx.html