Skip to content

Instantly share code, notes, and snippets.

@checkaayush
Last active May 30, 2019 10:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save checkaayush/90fdac5ac098dce411f2abafb31af965 to your computer and use it in GitHub Desktop.
Save checkaayush/90fdac5ac098dce411f2abafb31af965 to your computer and use it in GitHub Desktop.
Makefile to setup MongoDB Replica Set for development
# Setup local MongoDB Replica Set for development.
DB_ROOT ?= /tmp
DB1_PATH ?= ${DB_ROOT}/db1
DB2_PATH ?= ${DB_ROOT}/db2
DB3_PATH ?= ${DB_ROOT}/db3
DB1_PORT ?= 30000
DB2_PORT ?= 40000
DB3_PORT ?= 50000
REPLSET_NAME ?= demo
# Prefer running recipes over files with same names
.PHONY: clean start stop
.IGNORE: start stop
# make will only echo something when VERBOSE=1 is used
ifndef VERBOSE
.SILENT:
endif
help:
@echo
@echo "Setup MongoDB Replica Set"
@echo
@echo " Commands: "
@echo
@echo " help - Show this message."
@echo " start - Start Replica Set."
@echo " stop - Stop Replica Set."
start:
echo "\nSetting up MongoDB Replica Set ${REPLSET_NAME}...\n"
mkdir -p ${DB1_PATH} ${DB2_PATH} ${DB3_PATH}
fuser --silent -n tcp -k ${DB1_PORT} ${DB2_PORT} ${DB3_PORT}
mongod --port ${DB1_PORT} --dbpath ${DB1_PATH} --replSet ${REPLSET_NAME} --fork --logpath ${DB1_PATH}/mongod.log
mongod --port ${DB2_PORT} --dbpath ${DB2_PATH} --replSet ${REPLSET_NAME} --fork --logpath ${DB2_PATH}/mongod.log
mongod --port ${DB3_PORT} --dbpath ${DB3_PATH} --replSet ${REPLSET_NAME} --fork --logpath ${DB3_PATH}/mongod.log
mongo --port ${DB1_PORT} --eval 'rs.initiate({"_id":"${REPLSET_NAME}", "members": [{"_id": 0, "host": "localhost:${DB1_PORT}", "priority": 10}, {"_id": 1, "host": "localhost:${DB2_PORT}"}, {"_id": 2, "host": "localhost:${DB3_PORT}"}]})'
echo "\nDone. Connect to it on port ${DB1_PORT}.\n"
stop:
echo "\nStopping MongoDB Replica Set ${REPLSET_NAME}..."
fuser --silent -n tcp -k ${DB1_PORT} ${DB2_PORT} ${DB3_PORT}
echo "\nDone.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment