在分片集群架构中,如果需要读取完整的数据,只能通过路由节点读取。而数据实际存储在分片节点中,因此其中必然会多出一些节点间的网络数据传输的消耗。
8.分片集群总控脚本
由于集群中节点个数和种类比较多,编写了一个总控脚本,用于简化集群的启动和停止以及状态查看操作。
完整脚本内容如下:
[root@coe2coe data]# cat cluster.sh
#!/bin/bash
##################################################################
# FileName :startcluster.sh
# Author : coe2coe@qq.com
# Created :2018-10-02
# Description :
#################################################################
start()
{
IP=$(ip addr |grep inet |grep brd |awk -F' ' '{ print $2}'|awk -F'/' '{print $1}')
if [ "$IP" == "" ]
then
echo -e "Failed to get IP on this host."
exit 1
fi
CONFIG_PORTS="27117 27118 27119"
SHARD_PORTS="27017 27018 27019"
ROUTE_PORTS="27217 27218 27219"
CONFIG_ADDRESSES="$IP:27117,$IP:27118,$IP:27119"
echo -e "Starting mongodb cluster at {$IP}....."
echo -e "Starting config nodes @{$CONFIG_PORTS} ..."
/data/mongo/data/config/startconfig.sh $CONFIG_PORTS
echo -e "Starting shard nodes @{$SHARD_PORTS}...."
/data/mongo/data/shard/startshard.sh $SHARD_PORTS
echo -e "Starting route nodes @{$ROUTE_PORTS} with CONFIG:{$CONFIG_ADDRESSES}...."
/data/mongo/data/route/startroute.sh $CONFIG_ADDRESSES $ROUTE_PORTS
echo -e "===ALL DONE====="
}
stop()
{
PIDS=$(pidof mongod mongos 2>/dev/null )
if [ "$PIDS" == "" ]
then
echo -e "NO such process found!"
exit 1
fi
echo -e "Stopping mongod and mongos:{$PIDS} ...."
kill -9 ${PIDS}
exit 0
}
status()
{
C_PIDS=$(ps -elf |grep mongod |grep configsvr |grep -v grep |awk '{print $4}' |xargs )
D_PIDS=$(ps -elf |grep mongod |grep shardsvr |grep -v grep |awk '{print $4}' |xargs )
R_PIDS=$(ps -elf |grep mongos |grep -v grep |awk '{print $4}' |xargs )
if [ "$C_PIDS" == "" ]
then
C_STATUS="NOT running"
else
C_STATUS="Running"
fi
if [ "$D_PIDS" == "" ]
then
D_STATUS="NOT running"
else
D_STATUS="Running"
fi
if [ "$R_PIDS" == "" ]
then
R_STATUS="NOT running"
else
R_STATUS="Running"
fi
echo -e "config nodes:{$C_PIDS}:{${C_STATUS}}"
echo -e "shard nodes :{$D_PIDS}:{${D_STATUS}}"
echo -e "route nodes :{$R_PIDS}:{${R_STATUS}}"
exit 0
}
usage()
{
echo -e "Usage: $0 [start|stop|status]"
exit 1
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
usage
;;
esac
脚本功能如下:
启动集群:
[root@coe2coe data]# ./cluster.sh start
Starting mongodb cluster at {11.1.1.11}.....
Starting config nodes @{27117 27118 27119} ...
starting mongodb configsvr @HOMEDIR:{/data/mongo/data/config/27117}
about to fork child process, waiting until server is ready for connections.
forked process: 5569
child process started successfully, parent exiting
starting mongodb configsvr @HOMEDIR:{/data/mongo/data/config/27118}
about to fork child process, waiting until server is ready for connections.
forked process: 5652
child process started successfully, parent exiting
starting mongodb configsvr @HOMEDIR:{/data/mongo/data/config/27119}
about to fork child process, waiting until server is ready for connections.
forked process: 5737
child process started successfully, parent exiting
Starting shard nodes @{27017 27018 27019}....
starting mongodb shardsvr @HOMEDIR:{/data/mongo/data/shard/27017}
about to fork child process, waiting until server is ready for connections.
forked process: 5826
child process started successfully, parent exiting
starting mongodb shardsvr @HOMEDIR:{/data/mongo/data/shard/27018}
about to fork child process, waiting until server is ready for connections.
forked process: 5888
child process started successfully, parent exiting
starting mongodb shardsvr @HOMEDIR:{/data/mongo/data/shard/27019}
about to fork child process, waiting until server is ready for connections.
forked process: 5934
child process started successfully, parent exiting
Starting route nodes @{27217 27218 27219} with CONFIG:{11.1.1.11:27117,11.1.1.11:27118,11.1.1.11:27119}....
CONFIG_SERVERS:{11.1.1.11:27117,11.1.1.11:27118,11.1.1.11:27119}
DBPORTS:{27217}
starting mongodb routing @HOMEDIR:{/data/mongo/data/route/27217}
about to fork child process, waiting until server is ready for connections.
forked process: 5982
child process started successfully, parent exiting
starting mongodb routing @HOMEDIR:{/data/mongo/data/route/27218}
about to fork child process, waiting until server is ready for connections.
forked process: 6015
child process started successfully, parent exiting
starting mongodb routing @HOMEDIR:{/data/mongo/data/route/27219}
about to fork child process, waiting until server is ready for connections.
forked process: 6044
child process started successfully, parent exiting
===ALL DONE=====
查看集群状态:
[root@coe2coe data]# ./cluster.sh status
config nodes:{5569 5652 5737}:{Running}
shard nodes :{5826 5888 5934}:{Running}
route nodes :{5982 6015 6044}:{Running}
停止集群:
[root@coe2coe data]# ./cluster.sh stop
Stopping mongod and mongos:{5934 5888 5826 5737 5652 5569 6044 6015 5982} ....
停止集群后查看状态:
[root@coe2coe data]# ./cluster.sh status
config nodes:{}:{NOT running}
shard nodes :{}:{NOT running}
route nodes :{}:{NOT running}
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx