验证中...
Languages: Shell
Categories: 数据库相关
Latest update 2019-11-10 00:30
setup-mongo-cluster.sh
Raw Copy
#!/bin/bash
# Script used to automate mongo cluster setup locally
#
# sirchen 2019.11.10
if [ $# -ne 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
cmd=$1
BIN=$2
echo_with_color() {
echo -e "\033[34m$1\033[0m"
}
# simple 3 mongod RS without config,mongos
start() {
rm -rf node1/*
rm -rf node2/*
mkdir -p node1 node2 arbiter
echo_with_color "start 3 mongod"
$BIN/mongod --replSet myapp --dbpath ./node1 --port 40000 &
$BIN/mongod --replSet myapp --dbpath ./node2 --port 40001 &
$BIN/mongod --replSet myapp --dbpath ./arbiter --port 40002 &
sleep 5
echo_with_color "rs.initiate "
$BIN/mongo --port 40000 --eval "rs.initiate()"
echo_with_color "add secondary node..."
$BIN/mongo --port 40000 --eval "rs.add(\"VM_5_74_centos:40001\")"
echo_with_color "add arbiter node ..."
$BIN/mongo --port 40000 --eval "rs.add(\"VM_5_74_centos:40002\", {artiterOnly: true})"
echo_with_color "rs.status()"
$BIN/mongo --port 40000 --eval "rs.status()"
echo_with_color "enabling secondary read ..."
$BIN/mongo --port 40001 --eval "rs.slaveOk()"
echo_with_color "write one entry ..."
# write commands into js file is also ok
$BIN/mongo --port 40000 << eof
use db1
db.t.insert({id:1})
eof
echo_with_color "checking first entry's oplog ..."
$BIN/mongo --port 40000 << eof
use local
db.oplog.rs.findOne({op:"i"})
eof
echo_with_color "checking oplog current status ..."
$BIN/mongo --port 40000 --eval "db.getReplicationInfo()"
}
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-* 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
;;
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
;;
esac

Comment list( 0 )

Sign in for post a comment

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603