Skip to content

Instantly share code, notes, and snippets.

@baiwfg2
Created June 3, 2020 11:16
Show Gist options
  • Save baiwfg2/fac0a35686acdda12ce540d7313c7352 to your computer and use it in GitHub Desktop.
Save baiwfg2/fac0a35686acdda12ce540d7313c7352 to your computer and use it in GitHub Desktop.
A script that launches a mongo cluster quickly
#!/bin/bash
set -e
# Script used to automate mongo cluster setup locally
#
# sirchen 2019.11.10
if [ $# -lt 2 ]; then
echo "$0 <cmd> <bin root absolute dir>"
echo "You'd better choose a clean dir to execute the script, because there gonna many files generated within current working dir"
echo "Command execution order: start, config, addshard, stat"
exit 1
fi
interval=1000
cmd=$1
BIN=$2
engine="wiredTiger"
if [ x$3 != x"" ]; then
interval=$3
fi
if [ x$3 = x"rocksdb" ]; then
engine=$3
fi
echo_with_color() {
echo -e "\033[34m$1\033[0m"
}
# simple 3 mongod RS without config,mongos
start_debug() {
printf -v nodes '%s ' node{1..8}
rm -f 5k*
dbpath=db-wt
if [ $engine = "rocksdb" ]; then dbpath="db-rocks"; fi
rm -rf $dbpath
mkdir -p $dbpath
cd $dbpath
mkdir -p $nodes
cd ..
# how to set one log component to 5, set others to 1
echo_with_color "start 8 mongod"
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node1 --port 50001 -vv --logpath 5k1.log &
#./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node1 --port 50000 --keyFile keyfile --logpath 50000.log
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node2 --port 50002 -vv --logpath 5k2.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node3 --port 50003 -v --logpath 5k3.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node4 --port 50004 --logpath 5k4.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node5 --port 50005 --logpath 5k5.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node6 --port 50006 --logpath 5k6.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node7 --port 50007 --logpath 5k7.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node8 --port 50008 --logpath 5k8.log &
sleep 5
echo_with_color "rs.initiate "
./mongo --port 50001 --eval "rs.initiate()"
sleep 5
echo_with_color "add 2nd node..."
./mongo --port 50001 --eval "rs.add(\"localhost:50002\")"
echo_with_color "add 3th arbiter node ..."
./mongo --port 50001 --eval "rs.add(\"localhost:50003\", {arbiterOnly: true})"
echo_with_color "add 4th node with priority=0 (votes default 1) ..."
./mongo --port 50001 --eval "rs.add({host: \"localhost:50004\", priority: 0})"
echo_with_color "add 5th node with votes=0 (non-voting member must have priority=0)..."
./mongo --port 50001 --eval "rs.add({host: \"localhost:50005\", votes: 0, priority: 0})"
echo_with_color "add 6th node with hidden(priority must be 0) and votes=1..."
./mongo --port 50001 --eval "rs.add({host: \"localhost:50006\", hidden: true, priority: 0})"
# when votes=0, cannot ack write majority
echo_with_color "add 7th node with hidden and votes=0..."
./mongo --port 50001 --eval "rs.add({host: \"localhost:50007\", hidden: true, votes: 0, priority: 0})"
echo_with_color "add 8th node with Delayed RS member..."
./mongo --port 50001 --eval "rs.add({host: \"localhost:50008\", hidden: true, slaveDelay: 10, priority: 0})"
echo_with_color "rs.status()"
./mongo --port 50001 --eval "rs.status()"
# to prevent from failover
./mongo --port 50001 --eval "cfg=rs.conf(); cfg.settings.heartbeatTimeoutSecs=3600; cfg.settings.electionTimeoutMillis=3600000; rs.reconfig(cfg)"
echo_with_color "enabling secondary read ..."
./mongo --port 50002 --eval "rs.slaveOk()"
}
# start a 3 nodes cluster locally
start_simple() {
printf -v nodes '%s ' node{1..3}
rm -f 5k*
dbpath=db-wt
if [ $engine = "rocksdb" ]; then dbpath="db-rocks"; fi
rm -rf $dbpath
mkdir -p $dbpath
cd $dbpath
mkdir -p $nodes
cd ..
# how to set one log component to 5, set others to 1
echo_with_color "start 3 mongod"
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node1 --port 50001 --logpath 5k1.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node2 --port 50002 --logpath 5k2.log &
./mongod --replSet rs1 --storageEngine $engine --dbpath ./$dbpath/node3 --port 50003 --logpath 5k3.log &
sleep 5
echo_with_color "rs.initiate "
./mongo --port 50001 << eof
rs.initiate({ _id: "rs1", version: 1, members: [
{ _id:0, host: "localhost:50001" },
{ _id:1, host: "localhost:50002" },
{ _id:2, host: "localhost:50003" }
],
settings: { chainingAllowed: false}
})
eof
sleep 5
}
write_data() {
./mongo --port 50001 << eof
use db1
for (var i=1; i<10000; i++) {
db.t.insert({id: i, v1: i+1, ar:[i+2,i+3]})
sleep($interval)
db.t.remove({id:i}, { writeConcern: { w: "majority"} })
}
eof
}
config_shard() {
config="{_id:"configrs",members: [ { _id:0, host:"127.0.0.1:20000" } ]}"
$BIN/mongo --port 20000 << eof
config={_id:"configrs",members: [ { _id:0, host:"127.0.0.1:20000" } ]}
use admin
rs.initiate(config)
eof
$BIN/mongo --port 30001 << eof
config={_id:"shard1",members: [ { _id:0, host:"127.0.0.1:30001" }, { _id:1, host:"127.0.0.1:30002" },{ _id:2, host:"127.0.0.1:30003" } ]}
use admin
rs.initiate(config)
eof
$BIN/mongo --port 40001 << eof
config={_id:"shard2",members: [ { _id:0, host:"127.0.0.1:40001" }, { _id:1, host:"127.0.0.1:40002" },{ _id:2, host:"127.0.0.1:40003" } ]}
use admin
rs.initiate(config)
eof
}
add_shard_to_mongos() {
$BIN/mongo --port 30000 << eof
use admin
db.runCommand({addshard: "shard1/127.0.0.1:30001,127.0.0.1:30002,127.0.0.1:30003"})
db.runCommand({addshard: "shard2/127.0.0.1:40001,127.0.0.1:40002,127.0.0.1:40003"})
eof
}
shard_stat_from_mongos() {
$BIN/mongo --port 30000 << eof
use admin
db.runCommand({listshards:1})
sh.status()
eof
}
shard_db() {
if [ $# -ne 1 ]; then
echo "no db name"
exit 1
fi
db=$1
$BIN/mongo --port 30000 << eof
use admin
db.runCommand({enablesharding: "$db"})
db.runCommand({shardcollection: "$db.t", key: {k: 'hashed'}})
eof
}
common_commands() {
echo_with_color "buildinfo ..."
$BIN/mongo --port 30000 << eof
db.runCommand({buildinfo:1})
eof
echo_with_color "listdatabases ..."
$BIN/mongo --port 30000 << eof
use admin
db.runCommand({listdatabases:1})
eof
#db.runCommand({serverStatus:1}) # very long
#db.runCommand({ping:1})
}
# https://www.jianshu.com/p/f41e35e20e2b
# https://codereview.stackexchange.com/questions/172294/bash-script-to-deploy-a-mongodb-cluster-on-local-machine
start_shard() {
rm -rf shard1/data-* shard2/data-* configrs/data/*
rm -rf *.log
mkdir -p shard1/data-30001 shard1/data-30002 shard1/data-30003
mkdir -p shard2/data-40001 shard2/data-40002 shard2/data-40003
mkdir -p configrs/data
echo_with_color "starting shard1 ..."
$BIN/mongod -f mongo-conf/30001.conf
$BIN/mongod -f mongo-conf/30002.conf
$BIN/mongod -f mongo-conf/30003.conf
echo_with_color "starting shard2 ..."
$BIN/mongod -f mongo-conf/40001.conf
$BIN/mongod -f mongo-conf/40002.conf
$BIN/mongod -f mongo-conf/40003.conf
echo_with_color "starting config server ..."
$BIN/mongod -f mongo-conf/config.conf
echo_with_color "starting mongos server ..."
$BIN/mongos -f mongo-conf/mongos.conf
}
regular_commands() {
echo
}
stop() {
killall $BIN/mongod
killall $BIN/mongos
}
case $cmd in
run)
$0 start $BIN
$0 config $BIN
$0 addshard $BIN
$0 stat $BIN
;;
start)
start_shard
;;
debug)
start_debug
;;
simple)
start_simple
;;
stop)
stop
;;
rc)
regular_commands
;;
config)
config_shard
;;
addshard)
add_shard_to_mongos
;;
stat)
shard_stat_from_mongos
;;
sharddb)
shard_db db1
;;
common-commands|cc)
common_commands
;;
write)
write_data
;;
esac
@musamaq00
Copy link

If you’re looking for the latest coffee and food options, you can check out the full Caffe Nero Menu to see all the delicious choices available.

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