安裝MongoDB→創(chuàng)建多實(shí)例(創(chuàng)建數(shù)量可根據(jù)需求自定義)→開(kāi)啟所有實(shí)例并展示端口
讓客戶(hù)滿意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、虛擬空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、寧武網(wǎng)站維護(hù)、網(wǎng)站推廣。
#!/bin/bash
#自動(dòng)安裝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
#反復(fù)比對(duì)密鑰(可能不會(huì)一次成功)
for ((i=1;i<=10;i++))
do
rpm -q mongodb-org &> /dev/null
if [ $? -ne 0 ];then
yum install mongodb-org -y
fi
done
#監(jiān)聽(tīng)所有網(wǎng)段
sed -i "/bindIp/s/127.0.0.1/0.0.0.0/" /etc/mongod.conf
systemctl start mongod.service
#檢測(cè)是否啟動(dòng)正常
netstat -atnp | grep 27017 &> /dev/null
if [ $? -eq 0 ];then
echo "MongoDB服務(wù)啟動(dòng),一號(hào)實(shí)例建立完成"
else
echo "啟動(dòng)失敗"
exit 1
fi
echo "=========================================================="
echo "即將開(kāi)始創(chuàng)建實(shí)例"
sleep 5
#死循環(huán)進(jìn)行多實(shí)例的創(chuàng)建
for ((i=2;i>0;i++))
do
#判斷數(shù)據(jù)目錄與日志文件是否存在
if [ ! -e /data/mongodb/mongodb$i ] && [ ! -e /data/mongodb/mongod$i.log ];then
read -p "是否開(kāi)始創(chuàng)建第$i個(gè)實(shí)例(y/n):" create
p=`expr $i - 1`
case $create in
y)
#建立數(shù)據(jù)存放路徑
mkdir -p /data/mongodb/mongodb$i
#新建多實(shí)例對(duì)應(yīng)的日志文件
touch /data/mongodb/mongodb$i.log
#日志文件提權(quán)
chmod 777 /data/mongodb/mongodb$i.log
#復(fù)制原配置文件
cp -p /etc/mongod.conf /etc/mongod$i.conf
#修改對(duì)應(yīng)的數(shù)據(jù)文件存放路徑&日志文件路徑
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}'`
#更改實(shí)例對(duì)應(yīng)的端口號(hào)
sed -i "/port/s/27017/`expr $point + $p`/" /etc/mongod$i.conf
#啟動(dòng)對(duì)應(yīng)實(shí)例
mongod -f /etc/mongod$i.conf
netstat -atnp | grep $point &> /dev/null
if [ $? -eq 0 ];then
echo "$i號(hào)實(shí)例開(kāi)啟成功"
else
echo "致命錯(cuò)誤"
exit 1
fi
;;
n)
echo "本次共創(chuàng)建$p個(gè)實(shí)例(包括一號(hào)實(shí)例)"
#展示所有實(shí)例端口號(hào)
netstat -atnp | grep mongod | awk '{print $4}' | awk -F: '{print $2}' > shili
echo "端口號(hào)為:"
cat shili
break ;;
*)
echo "請(qǐng)輸入y/n,別搞些有的沒(méi)的"
esac
else
echo "$i號(hào)實(shí)例已創(chuàng)建"
fi
done
rm -rf shili