Try   HackMD

MongoDB Basic Cluster Administration
ch2 Replication

tags: MongoDB University M103

MongoDB Replica Set

  • Primary

  • Secondary

  • arbiter

    • Holds no data
    • Can vote in an election
    • Cannot become primary
  • Replica set can be up to 50 members

    • only 7 voting members
  • Replica set are groups of mongod
  • High availability and Failover
  • Members can have different roles and specific purposes

election trigger

  • Adding a new node to the replica set,
  • initiating a replica set,
  • performing replica set maintenance using methods such as rs.stepDown() or rs.reconfig()
  • the secondary members losing connectivity to the primary for more than the configured timeout (10 seconds by default).

Setting Up a Replica Set

Use setting file demo

storage: dbPath: /var/mongod/db/node1 net: bindIp: 127.0.0.1,192.168.103.100 port: 27011 security: authorization: enabled keyFile: /var/mongod/kpi/m103-keyfile systemLog: destination: file path: /var/mongodb/db/node1/mongod.log logAppend: true processManagement: fork: true replication: replSetName: m103-example

Replication Configuration Document

Replication Configuration

  • JSON Object that defines the configuration options of our replica set
  • Can be configured manually from the shell
  • There are set of mongo shell replication help make it easyer to manage
    • rs.add
    • rs.initiate
    • rs.remove
    • rs.status
    • rs.config
    • rs.reconfig

Replication Commands

  • rs.status()

    • Reports health on replica set nodes
    • Uses data from heartbeats
  • rs.isMaster()

    • Describes a node's role in the replica set
    • Shorter output than rs.status()
  • db.serverStatus('repl')

    • section of the db.serverStatus() output
    • Similar to the output of rs.isMaster()
  • rs.printReplicationInfo()

    • only returns the oplog data relative to current node
    • content first and last oplog events timestamps

Local DB

use local
  • me
  • oplog.rs
  • replset.election
  • replset.minvalid
  • startup_log
  • system.replset
  • system.rollback_id

Usually, do not modify them.

Any data written to local database is not written to oplog.rs or changing any of the system configuration collections will stay there and will not be replicated

oplog.rs

  • capped collection
  • takes 5% of free disk by default
var stats = db.oplog.rs.stats() stats.capped // true stats.size stats.maxSize

Reads and Writes on a Replica Set

To enable to read on secondary node

rs.slaveOk()

We can never write to secondary node