Code Monkey home page Code Monkey logo

mongodb-replicaset's Introduction

mongodb-replicaset

Sample scripts to start MongoDB Replica Sets

What you are going to find here?

There is a set of scripts inside project folders. They help you to bring replica set up and down. Each folder has a specific content detailed below

01 - Replica Set configuration

Start a 3-node replica set and activate replication

Enter in sample folder

cd $PATH_TO_PROJECT/01-simple-replset

First time starting cluster and replication

./start-cluster.sh
./start-replication.sh

Stop and restart cluster (Mongo starts replication automatically if replication is already started)

./stop-cluster.sh
./restart-cluster.sh

Purge cluster (After purging you need to start cluster and replication again)

./purge-cluster.sh

Purge and start cluster at same script (you need to start replication again)

./purge-start-cluster.sh
./start-replication.sh

Change each node configuration as you wish: ports, storage, oplogSize, security, logs, profiling...

net:
   port: 27017

storage:
   dbPath: db
   directoryPerDB: true
   journal:
      enabled: true
   mmapv1:
      smallFiles: true

replication:
   oplogSizeMB: 50
   replSetName: rs

systemLog:
   destination: file
   path: db/mongodb.log
   logAppend: true
   timeStampFormat: iso8601-utc

operationProfiling:
   slowOpThresholdMs: 100

02 - Replica Set with one arbiter

Start a 2-node and one-arbiter replica set and activate replication

replication-cluster.json is quite different from the other

{
   _id: "rs",
   members: [
      {
         _id: 0,
         host: "localhost:27020",
         priority: 2
      },
      {
         _id: 1,
         host: "localhost:27021",
         priority: 1
      },
      {
         _id: 2,
         host: "localhost:27022",
         arbiterOnly: true
      }
   ]
}

There is a "arbiterOnly: true" in 3rd node. Arbiters don't contains data, but they can participate in an election

03 - Replica Set with security enabled

Start a 3-node replica set with security enabled and activate replication

mongodb.conf files has a security session in each node

...
security:
   authorization: enabled
   keyFile: mongodb-keyfile
...

This script creates a default user called dbAdmin with root role. The authentication process can be done like these

MongoDB shell console
...
> use admin
> db.auth("dbAdmin","dbAdmin")
...

or

mongo --port 27023 -u dbAdmin -p dbAdmin --authenticationDatabase=admin

Once logged, you can create other users or change script to create users before start cluster - TODO: build a json file that contains users

...
if(replStatus && replStatus.ok === 1){
	
	db = db.getSiblingDB("admin");

	if(!db.auth("dbAdmin","dbAdmin")){
		print(" ### Creating DB Admin user...");

		var adminUser = db.createUser({ user: "dbAdmin", pwd: "dbAdmin", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, {role:"root", db: "admin"} ] });

		var dbAdimOk = db.auth("admin","latam");

		print("DB Admin authentication after create: "+dbAdimOk);
	} else {
		print(" ### Logged with DB Admin user");
	}
} else {
...

You may know that there is a mongodb-keyfile inside each node folder. Mongo instances communicate each other using that key.

TODO - 04 - Replica Set with tags

mongodb-replicaset's People

Contributors

marcusberro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.