Get the App
SLTechnology News&Howtos  ›  Servers  › 

Principle of mongodb replica set

Shulou Source: shulou.com Published: 2022-06-02 17:42:54 10月03日 Update

1. Principle:

The replica cluster sends a heartbeat every 2 seconds and is marked as unreachable if it is not received after 10 seconds.

MongoDB applies the database operation on the primary database and then records the operation in the operation log of the primary database. The auxiliary member then copies and applies these operations to the asynchronous process. All replica set members contain a copy of the operation log in the local.oplog.rs collection, with a default size of 5% of the disk, but no query operations are recorded. OplogSize can specify the size in / etc/mongdb.conf

Replication: oplogSizeMB: replSetName: secondaryIndexPrefetch: enableMajorityReadConcern:

Member roles in the 2.mongdb replica set:

Primary: responsible for writing data

Seconary: responsible for synchronizing data on the master and providing read service

Arbitration node: do not keep data, do not participate in the election, only carry out the election.

The replica set is elected as a priority, and the range 0-1000 minute 0 is the arbitration node and does not participate in the election. The default is 1

3. How to set it up?

MongoDB Enterprise shard-rs:PRIMARY > config=rs.conf () MongoDB Enterprise shard-rs:PRIMARY > config {"_ id": "shard-rs", "version": 3, "protocolVersion": NumberLong (1), "writeConcernMajorityJournalDefault": true, "members": [{"_ id": 0 "host": "worker2:27020", "arbiterOnly": false, "buildIndexes": true, "hidden": false, "priority": 1 "tags": {}, "slaveDelay": NumberLong (0), "votes": 1}, {"_ id": 1 "host": "worker2:27021", "arbiterOnly": false, "buildIndexes": true, "hidden": false, "priority": 1 "tags": {}, "slaveDelay": NumberLong (0), "votes": 1}, {"_ id": 2 "host": "worker2:27022", "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 ("5dd8bf958b8ea486b84c23b2")}}

The priority of a fragment with an id value of 2 is changed to 3. When primary, a fragment with an id value of 2 becomes prmary.

Config.members [2] .priority = 3

Deliberately stop the 27020 process.

Kill-9 82609

Check whether a host with an id value of 2 becomes a primary

Enter the mongdb of 27022:

MongoDB Enterprise shard-rs:PRIMARY > use adminswitched to db adminMongoDB Enterprise shard-rs:PRIMARY > db.isMaster () {"hosts": ["worker2:27020", "worker2:27021", "worker2:27022"], "setName": "shard-rs", "setVersion": 4, "ismaster": true # become primary "secondary": false

Tags: Data replica database priority size member log node arbitration application election principle middle finger host tag disk scope role later process Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Redmi Microsoft Docker