Skip to content

Instantly share code, notes, and snippets.

@leetreveil
Last active December 8, 2023 06:34
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save leetreveil/7233677 to your computer and use it in GitHub Desktop.
Save leetreveil/7233677 to your computer and use it in GitHub Desktop.
Shell scripts to create a mongodb replica set and sharded cluster locally
#!/bin/bash
# shell script to create a simple mongodb replica set (tested on osx)
set -e
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
default=$(tput sgr0)
function finish {
pids=(`cat ~/mongosvr/rs-*.pid`)
for pid in "${pids[@]}"
do
kill $pid
wait $pid
done
}
trap finish EXIT
mkdir -p ~/mongosvr/rs-0
mkdir -p ~/mongosvr/rs-1
mkdir -p ~/mongosvr/rs-2
mongod --shardsvr --dbpath ~/mongosvr/rs-0 --replSet set --rest --port 27091 \
--config . --pidfilepath ~/mongosvr/rs-0.pid 2>&1 | sed "s/.*/$red&$default/" &
mongod --shardsvr --dbpath ~/mongosvr/rs-1 --replSet set --rest --port 27092 \
--config . --pidfilepath ~/mongosvr/rs-1.pid 2>&1 | sed "s/.*/$green&$default/" &
mongod --shardsvr --dbpath ~/mongosvr/rs-2 --replSet set --rest --port 27093 \
--config . --pidfilepath ~/mongosvr/rs-2.pid 2>&1 | sed "s/.*/$yellow&$default/" &
# wait a bit for the first server to come up
sleep 5
# call rs.initiate({...})
cfg="{
_id: 'set',
members: [
{_id: 1, host: 'localhost:27091'},
{_id: 2, host: 'localhost:27092'},
{_id: 3, host: 'localhost:27093'}
]
}"
mongo localhost:27091 --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : $cfg}))"
# sleep forever
cat
#!/bin/bash
# shell script to create a simple mongodb sharded cluster locally.
# Requires a replica set to be already running, you can run
# start-mongo-replset.sh first to start a replica set.
set -e
red=$(tput setaf 1)
green=$(tput setaf 2)
default=$(tput sgr0)
function finish {
pid=`cat ~/mongosvr/shard-config-0.pid`
kill $pid
wait $pid
}
trap finish EXIT
mkdir -p ~/mongosvr/config-0
# start up the mongodb config server for the shards
mongod --configsvr --dbpath ~/mongosvr/config-0 --port 27019 \
--config . --pidfilepath ~/mongosvr/shard-config-0.pid 2>&1 | sed "s/.*/$red&$default/" &
sleep 3
mongos --configdb localhost:27019 | sed "s/.*/$green&$default/" &
sleep 3
# add the first replica set instance as a shard, the others will be discovered automatically by mongos
mongo --eval "JSON.stringify(sh._adminCommand( { addShard : 'set/localhost:27091' } , true ))"
# enable sharding on the test database
mongo --eval "JSON.stringify(sh._adminCommand( { enableSharding : 'test' } ))"
# sleep forever
cat
@alivxlive
Copy link

nice script, thank you

@dominicrico
Copy link

Thank you for that script ;) It's perfect for my project!

@Signo74
Copy link

Signo74 commented Mar 16, 2016

Great script! Thank you for sharing.

@roysG
Copy link

roysG commented Mar 30, 2016

Two questions,
1.Does it works also for mongo 3.x ?
2.Does it tested on ubuntu?

Thanks.

@mdaftabsmu
Copy link

Hi Sir,
I am try to create mongo db replica set remotely as service .
kindly guide me step -by step .

@joeytwiddle
Copy link

joeytwiddle commented Nov 24, 2017

Thanks for this! But this script does not support MongoDB 3.4 in its current form. (I am guessing it was written for 3.2.)

For MongoDB 3.4, each config server also needs to be a replicaset (CSRS).

I have published a similar script for MongoDB 3.4 here.

@jeasonchan
Copy link

thx!

@manoharvare
Copy link

Great Script

@xeoshow
Copy link

xeoshow commented Oct 20, 2021

Could it support mongodb 5.0 ? Thanks~

@MadhavChinthaginjala
Copy link

Write a shell script to install, start and initiate the Mongo dB replica set on your local machine with the below requirements.

  1. The MongoDB version must be v5.0.
  2. There must be 3 members in the replica set with port numbers 27017,27018 and 27019 respectively.
  3. Replica set name should be rso.
  4. The 3rd member should be an arbiter.
  5. The WiredTiger cache for all the members should be 1GB
  6. The configuration to disable javascript execution should be added.

please help me with this in 1 hr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment