Weizhang2017 / mongoDB_replica_template

A template to set up a MongoDB replica set

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set up MongoDB Replication
  1. Create database directory for each member in a replica set e.g.
  • /mongodb/replica/db1/
  • /mongodb/replica/db2/
  • /mongodb/replica/db3/

Minimum number of members in a replica set is 3.

  1. Start databases
  • mongod --dbpath /mongodb/Replica/db1/ --replSet replica --port 27017 --fork --logpath /mongodb/Replica/db1/mongod.log
  • mongod --dbpath /mongodb/Replica/db2/ --replSet replica --port 27018 --fork --logpath /mongodb/Replica/db2/mongod.log
  • mongod --dbpath /mongodb/Replica/db3/ --replSet replica --port 27019 --fork --logpath /mongodb/Replica/db3/mongod.log

Each database should be with a different port. Make sure mongod has permission to access the database directory.

  1. Create a configuration file e.g.
var configuration = {
        _id: 'replica',
        members: [
                {_id:0, host: 'localhost:27017'},
                {_id:1, host: 'localhost:27018', slaveDelay:20, priority:0},
                {_id:2, host: 'localhost:27019'},
        ]
}

slaveDelay is the time the secondary database operation will delay behind the primary database. Priority parameter determines the member’s priority in elections. 0 means the member will not be elected as primary database

  1. Copy and paste the configuration into mongo shell and initiate the configuration
rs.initiate(configuration)
  1. Connect to the replica set with Mongoengine
from mongoengine import connect
connect('test', replicaset='replica')

mongoengine will automatically handle failover

About

A template to set up a MongoDB replica set


Languages

Language:JavaScript 57.0%Language:Python 43.0%