Shell脚本一键部署MongoDB
Shell脚本一键部署MongoDB
基本实施思路:
安装MongoDB→创建多实例(创建数量可根据需求自定义)→开启所有实例并展示端口
#!/bin/bash #自动安装MongoDB # #检查MongoDB是否已安装 local="/etc/yum.repos.d/MongoDB.repo" rpm -q mongodb-org &> /dev/null if [ $? -ne 0 ];then #配置本地YUM源 ls /etc/yum.repos.d/ | grep Mongo &> /dev/null if [ $? -ne 0 ];then touch $local echo "[mongodb-org]" >> $local echo "name=mongodb" >> $local echo "baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/3.6/x86_64/" >> $local echo "gpgcheck=1" >> $local echo "enabled=1" >> $local echo "gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc" >> $local yum list yum install mongodb-org -y else echo "配置本地YUM源配置完成" yum list yum install mongodb-org -y fi else echo "MongoDB已安装~~~~~" fi #反复比对密钥(可能不会一次成功) for ((i=1;i<=10;i++)) do rpm -q mongodb-org &> /dev/null if [ $? -ne 0 ];then yum install mongodb-org -y fi done #监听所有网段 sed -i "/bindIp/s/127.0.0.1/0.0.0.0/" /etc/mongod.conf systemctl start mongod.service #检测是否启动正常 netstat -atnp | grep 27017 &> /dev/null if [ $? -eq 0 ];then echo "MongoDB服务启动,一号实例建立完成" else echo "启动失败" exit 1 fi echo "==========================================================" echo "即将开始创建实例" sleep 5 #死循环进行多实例的创建 for ((i=2;i>0;i++)) do #判断数据目录与日志文件是否存在 if [ ! -e /data/mongodb/mongodb$i ] && [ ! -e /data/mongodb/mongod$i.log ];then read -p "是否开始创建第$i个实例(y/n):" create p=`expr $i - 1` case $create in y) #建立数据存放路径 mkdir -p /data/mongodb/mongodb$i #新建多实例对应的日志文件 touch /data/mongodb/mongodb$i.log #日志文件提权 chmod 777 /data/mongodb/mongodb$i.log #复制原配置文件 cp -p /etc/mongod.conf /etc/mongod$i.conf #修改对应的数据文件存放路径&日志文件路径 sed -i "/path/s/\/var\/log\/mongodb\/mongod.log/\/data\/mongodb\/mongodb$i.log/" /etc/mongod$i.conf sed -i "/dbPath/s/\/var\/lib\/mongo/\/data\/mongodb\/mongodb$i/" /etc/mongod$i.conf point=`cat /etc/mongod.conf | grep port | awk ‘{print $2}‘` #更改实例对应的端口号 sed -i "/port/s/27017/`expr $point + $p`/" /etc/mongod$i.conf #启动对应实例 mongod -f /etc/mongod$i.conf netstat -atnp | grep $point &> /dev/null if [ $? -eq 0 ];then echo "$i号实例开启成功" else echo "致命错误" exit 1 fi ;; n) echo "本次共创建$p个实例(包括一号实例)" #展示所有实例端口号 netstat -atnp | grep mongod | awk ‘{print $4}‘ | awk -F: ‘{print $2}‘ > shili echo "端口号为:" cat shili break ;; *) echo "请输入y/n,别搞些有的没的" esac else echo "$i号实例已创建" fi done rm -rf shili
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19