- 搭建1个config节点,一个mongos节点,两个分片,每个分片3个节点。
- 传入参数为mongos节点端口号,config节点端口号,分片节点端口号
- 可根据需求修改存放的目录basedir
#!/bin/bash
# 定义 usage 函数
usage() {
echo "Usage : mongos_port config_port shard_node_port_1 .... shard_node_port_n"
echo "shard_node num must be multiples of 3!"
exit 1
}
# 解析命令行参数
while [ "$1" != "" ]; do
case $1 in
-h | --help )
usage
exit
;;
* )
break
;;
esac
shift
done
## 安装位置
basedir=/data/application/test
mongodir=${basedir}/mongo
install_mongo(){
##安装mongo到指定位置
wget -P ${basedir} https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.27.tgz
tar -zxvf ${basedir}/mongodb-linux-x86_64-rhel70-5.0.27.tgz -C ${basedir}
if [[ $? == 0 ]];then
echo "解压完成"
else
echo "解压失败"
fi
mv ${basedir}/mongodb-linux-x86_64-rhel70-5.0.27 ${mongodir}
}
## 创建不同节点的目录
create_config_node(){
config_port=$2
## config节点目录
mkdir ${basedir}/config
## config节点data目录
mkdir ${basedir}/config/data_${config_port}
touch ${basedir}/config/mongodb_${config_port}.log
cat >> ${basedir}/config/mongodb_${config_port}.cnf << EOF
systemLog:
destination: file
logAppend: true
path: ${basedir}/config/mongodb_${config_port}.log
storage:
dbPath: ${basedir}/config/data_${config_port}
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: ${basedir}/config/mongodb_${config_port}.pid # location of pidfile
# network interfaces
net:
port: ${config_port}
bindIp: 127.0.0.1
# shard
sharding:
clusterRole: configsvr
# repliuca set
replication:
replSetName: config
EOF
}
## 创建mongos节点
create_mongos_node(){
mongos_port=$1
mkdir ${basedir}/mongos
touch ${basedir}/mongos/mongos_${config_port}.log
cat >> ${basedir}/mongos/mongos_${mongos_port}.cnf << EOF
systemLog:
destination: file
logAppend: true
path: ${basedir}/mongos/mongos_${mongos_port}.log
processManagement:
fork: true # fork and run in background
pidFilePath: ${basedir}/mongos/mongos_${mongos_port}.pid # location of pidfile
# timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: ${mongos_port}
bindIp: 127.0.0.1
sharding:
configDB: config/127.0.0.1:${config_port}
EOF
}
create_dir(){
myport=$1
mkdir ${basedir}/mongo_${myport}
mkdir ${basedir}/mongo_${myport}/data_${myport}
touch ${basedir}/mongo_${myport}/mongodb_${myport}.log
cat >> ${basedir}/mongo_${myport}/mongodb_${myport}.cnf << EOF
systemLog:
destination: file
logAppend: true
path: ${basedir}/mongo_${myport}/mongodb_${myport}.log
storage:
dbPath: ${basedir}/mongo_${myport}/data_${myport}
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: ${basedir}/mongo_${myport}/mongodb_${myport}.pid # location of pidfile
# timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: ${myport}
bindIp: 127.0.0.1
# shard
sharding:
clusterRole: shardsvr
# repliuca set
replication:
replSetName: shard0$2
EOF
}
create_shard_node(){
i=0
shard_cnt=0
for port in $@;do
((i++))
if (( $i > 2 ));then
echo $port
## 创建对应端口号的分片节点
create_dir $port $((shard_cnt/3))
((shard_cnt++))
fi
done
}
start_cluster(){
## 启动mongodb集群
## 初始化集群,先启动config,在启动分片,最后启动mongos
## config节点
${mongodir}/bin/mongod --config=${basedir}/config/mongodb_${config_port}.cnf &
if [[ $? == 0 ]]; then
echo "config server $1 start success!"
fi
sleep 5
## 初始化config
${mongodir}/bin/mongo --host=127.0.0.1 --port=${config_port} --eval "rs.initiate()"
if [[ $? == 0 ]]; then
echo "mongos server $2 start success!"
fi
## 分片节点
i=0
for port in $@;do
if (( $i >= 2 ));then
${mongodir}/bin/mongod --config=${basedir}/mongo_${port}/mongodb_${port}.cnf &
if [[ $? == 0 ]]; then
echo "shard node $port start success!"
fi
fi
((i++))
done
sleep 5
## mongos 节点
${mongodir}/bin/mongos --config=${basedir}/mongos/mongos_${mongos_port}.cnf &
}
main(){
install_mongo
create_config_node $@
create_mongos_node $@
create_shard_node $@
start_cluster $@
}
main $@