[MongoDB]: Sharded cluster script
Shell script to run a 3 sharded cluster (3 ReplicaSet) on a single server.
[root@dbversity.com bin]# cat shard_creation_script.sh
##### Killing the existing Mongo processes ################
for i in `ps -ef | egrep ‘shardsvr|configsvr|replSet|configdb’ | grep -v egrep | awk -F” ” ‘{print $2}’`; do kill -9 $i; done
##### Creating Mongo data & log files ################
mkdir -p /opt/mongodb/data /opt/mongodb/logs
rm -rf /opt/mongodb/data/* /opt/mongodb/logs/* /data/1/shard1_1/* /data/1/shard1_2/* /data/1/shard2_1/* /data/2/shard2_2/* /data/1/shard3_1/* /data/1/shard3_2/*
cd /opt/mongodb/data/
mkdir -p config1 config2 config3 arbiter1 arbiter2 arbiter3 router /data/1/shard1_1 /data/1/shard1_2 /data/2/shard2_1 /data/2/shard2_2 /data/3/shard3_1 /data/3/shard3_2
cd /opt/mongodb/bin
##### Starting the Mongo Config,Shard,Arbiter & Router services ################
## Config Servers #####
numactl –interleave=all ./mongod –configsvr –dbpath /opt/mongodb/data/config1 –logpath /opt/mongodb/logs/config1.log –port 39000 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –configsvr –dbpath /opt/mongodb/data/config2 –logpath /opt/mongodb/logs/config2.log –port 39001 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –configsvr –dbpath /opt/mongodb/data/config3 –logpath /opt/mongodb/logs/config3.log –port 39002 –config /etc/mongod.conf &
## Replica Set 1 ######
numactl –interleave=all ./mongod –shardsvr –replSet rs1 –dbpath /data/1/shard1_1 –logpath /opt/mongodb/logs/shard1_1.log –port 27010 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –shardsvr –replSet rs1 –dbpath /data/1/shard1_2 –logpath /opt/mongodb/logs/shard1_2.log –port 27011 –config /etc/mongod.conf &
## Replica Set 2 ######
numactl –interleave=all ./mongod –shardsvr –replSet rs2 –dbpath /data/2/shard2_1 –logpath /opt/mongodb/logs/shard2_1.log –port 27020 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –shardsvr –replSet rs2 –dbpath /data/2/shard2_2 –logpath /opt/mongodb/logs/shard2_2.log –port 27021 –config /etc/mongod.conf &
## Replica Set 3 ######
numactl –interleave=all ./mongod –shardsvr –replSet rs3 –dbpath /data/3/shard3_1 –logpath /opt/mongodb/logs/shard3_1.log –port 27030 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –shardsvr –replSet rs3 –dbpath /data/3/shard3_2 –logpath /opt/mongodb/logs/shard3_2.log –port 27031 –config /etc/mongod.conf &
## Arbiters ####
numactl –interleave=all ./mongod –replSet rs1 –dbpath /opt/mongodb/data/arbiter1 –logpath /opt/mongodb/logs/arbiter1.log –port 27012 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –replSet rs2 –dbpath /opt/mongodb/data/arbiter2 –logpath /opt/mongodb/logs/arbiter2.log –port 27022 –config /etc/mongod.conf &
numactl –interleave=all ./mongod –replSet rs3 –dbpath /opt/mongodb/data/arbiter3 –logpath /opt/mongodb/logs/arbiter3.log –port 27032 –config /etc/mongod.conf &
sleep 200
numactl –interleave=all ./mongos –configdb xx.xx.xx.xx:39000,xx.xx.xx.xx:39001,xx.xx.xx.xx:39002 –logpath /opt/mongodb/logs/router.log –port 10000 &
sleep 200
./mongo dbversity.com:27010/admin –eval “rs.initiate()”
./mongo dbversity.com:27020/admin –eval “rs.initiate()”
./mongo dbversity.com:27030/admin –eval “rs.initiate()”
sleep 100
echo -e “\n\n Replica sets are being added. \n\n”
./mongo dbversity.com:27010/admin –eval “rs.add(\”dbversity.com:27011\”)”
./mongo dbversity.com:27020/admin –eval “rs.add(\”dbversity.com:27021\”)”
./mongo dbversity.com:27030/admin –eval “rs.add(\”dbversity.com:27031\”)”
./mongo dbversity.com:27010/admin –eval “rs.addArb(\”dbversity.com:27012\”)”
./mongo dbversity.com:27020/admin –eval “rs.addArb(\”dbversity.com:27022\”)”
./mongo dbversity.com:27030/admin –eval “rs.addArb(\”dbversity.com:27032\”)”
sleep 200
./mongo dbversity.com:10000/admin –eval “sh.addShard(\”rs1/dbversity.com:27010,dbversity.com:27011\”)”
./mongo dbversity.com:10000/admin –eval “sh.addShard(\”rs2/dbversity.com:27020,dbversity.com:27021\”)”
./mongo dbversity.com:10000/admin –eval “sh.addShard(\”rs3/dbversity.com:27030,dbversity.com:27031\”)”
[root@dbversity.com bin]#