Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

MongoDB 3 replica set configuration

farenda 2016-09-23 0

MongoDB 3 Replica Set configuration consists of a few steps and can be done even on a localhost. In this post we show how to configure two node replication.

Quick facts about Replica Sets

  • Replica Set is a set of duplicated nodes.
  • Replica Set nodes can be of different type (see below).
  • Replication in MongoDB is asynchronous.
  • To get configuration of Replica Set use rs.conf() command.
  • To change configuration use rs.reconfig(new_config) command.

Types of Replica Set nodes:

  • Regular: can be come primary and participate in voting
  • Arbiter: only for voting purposes
  • Delayed/Regular: cannot become primary, but can vote (it’s usually for recovery).
  • Hidden: cannot become primary, but can vote (usually analytic node)

Configuration in two steps

Create and start Replica Set nodes

First we have to create directories for separate instances (here two) of MongoDB and start them on different ports (to not clash on localhost):

mkdir -p /tmp/data/rs1 /tmp/data/rs2
mongod --replSet farenda --logpath "1.log" --dbpath /tmp/data/rs1 --port 7017 --oplogSize 64 --smallfiles --fork
mongod --replSet farenda --logpath "2.log" --dbpath /tmp/data/rs2 --port 7018 --oplogSize 64 --smallfiles --fork

Initialize each node with Replica Set parameters

Now we need to inform each node about locations of other Replica Set nodes:

var config = {
  _id: "farenda",
  members: [
    { _id : 0, host : "localhost:7017"}, // the same port as above
    { _id : 1, host : "localhost:7018"}
  ]
};

rs.initiate(config);
rs.status();

References

  • Check out MongoDB Reference Manual for details on configuration parameters
  • Check MongoDB 3 Tutorial for other posts on MongoDB 3
Share with the World!
Categories MongoDB Tags mongodb
Previous: MongoDB 3 array query operations
Next: Linux cut command by example

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored