您好,登錄后才能下訂單哦!
小編給大家分享一下如何全自動安裝Mongo副本集,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
在opensuse12 sp1上使用shell腳本自動安裝部署Mongodb副本集,集群一共三個虛擬機節點
mongo# chmod +x deployinstance.sh initreplicaset.sh # 三個節點上分別運行該腳本 mongo# ./deployinstance.sh # ip1 和ip2 是mongodb中作為secondary的節點ip,然后在Primary節點上運行該腳本 mongo# ./initreplicaset.sh ip1 ip2
#!/bin/bash # Deploy mongodb,node-exporter,mongodb-exporter in suse 12 sp1 cronfile="/var/spool/cron/tabs/root" mongocmd="/usr/bin/mongod" if [ `ifconfig | grep -c bond` -ne 0 ]; then iface=bond else iface=eth if [ `ifconfig | grep -c eth2` -eq 0 ]; then iface_local_flag=1 else iface_local_flag=0 fi fi vm_private_ip=`ifconfig ${iface}0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1` if [ $iface_local_flag -eq 1 ]; then vm_public_ip=${vm_private_ip} else vm_public_ip=`ifconfig ${iface}1 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1` fi # echo -e "\033[41;36m the Current host ip is: ${vm_private_ip} \033[0m" echo -e "\033[41;36m Stoping firewall \033[0m" systemctl stop SuSEfirewall2.service systemctl disable SuSEfirewall2.service # install mongodb echo -e "\033[41;36m import mongodb public key \033[0m" sudo rpm --import https://www.mongodb.org/static/pgp/server-3.4.asc echo -e "\033[41;36m Adding mongodb repo \033[0m" # sudo zypper addrepo -f https://ftp5.gwdg.de/pub/opensuse/discontinued/distribution/12.3/repo/oss/ oss # sudo zypper addrepo -f https://ftp5.gwdg.de/pub/opensuse/discontinued/distribution/12.3/repo/non-oss/ non-oss sudo zypper addrepo --gpgcheck "https://repo.mongodb.org/zypper/suse/12/mongodb-org/3.4/x86_64/" mongodb sudo zypper -n install mongodb-org cp /etc/mongod.conf{,.`date +%F`} echo -e "\033[41;36m Adding mongodb data and logs directory \033[0m" mkdir -pv /data/mongodb/data mkdir -pv /var/log/mongodb/ echo -e "\033[41;36m Updating mongodb configure files \033[0m" cat << EOF > /etc/mongod.conf dbpath=/data/mongodb/data logpath=/var/log/mongodb/ pidfilepath=/var/run/mongodb.pid directoryperdb=true logappend=true replSet=rs1 bind_ip=${vm_private_ip} smallfiles=true port=27017 oplogSize=5120 fork=true journal=true logRotate=rename EOF cat << EOF > /data/mongodb/logRotate.sh #!/bin/bash #Rotate the MongoDB logs to prevent a single logfile from consuming too much disk space. mongocmd="/usr/bin/mongod" pidArray=\$(pidof \$mongocmd) for pid in \$pidArray;do if [ \$pid ];then kill -SIGUSR1 \$pid fi done exit EOF chmod +x /data/mongodb/logRotate.sh chown -R mongod:mongod /data echo -e "\033[41;36m Adding Cron Task \033[0m" sudo echo "59 23 * * * /data/mongodb/logRotate.sh" >> ${cronfile} echo -e "\033[41;36m Running Mongodb Process \033[0m" ${mongocmd} -f /etc/mongod.conf echo -e "\033[41;36m Checking Mongodb Process \033[0m" ps -ef | grep /etc/mongod | grep -v grep if [ `echo $?` -eq 0 ];then echo "mongodb Running now" else echo "mongodb start failed" exit 3 fi echo -e "\033[41;36m Deploy Node-exporter \033[0m" cd /opt wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz tar xf node_exporter-0.16.0.linux-amd64.tar.gz /usr/bin/nohup /opt/node_exporter-0.16.0.linux-amd64/node_exporter & echo -e "\033[41;36m Deploy Mongodb-exporter \033[0m" cd /opt wget https://github.com/dcu/mongodb_exporter/releases/download/v1.0.0/mongodb_exporter-linux-amd64 chmod +x mongodb_exporter-linux-amd64 /usr/bin/nohup /opt/mongodb_exporter-linux-amd64 -mongodb.uri mongodb://${vm_private_ip}:27017 ps -ef | grep node_exporter | grep -v grep if [ `ehco $? ` -eq 0 ];then echo -e "\033[41;36m Node-Exporter Install successfully. \033[0m" else echo -e "\033[41;36m Node-Exporter Install failed. \033[0m" fi ps -ef | grep mongodb_exporter-linux-amd64 | grep -v grep if [ `ehco $? ` -eq 0 ];then echo -e "\033[41;36m Mongo-Exporter Install successfully. \033[0m" else echo -e "\033[41;36m Mongo-Exporter Install failed. \033[0m" fi cat << EOF > /data/mongodb/checker.sh #!/bin/bash #check exporter is health ps -ef | grep node_exporter | grep -v grep if [ \`ehco \$? \` -nq 0 ];then /usr/bin/nohup /opt/node_exporter-0.16.0.linux-amd64/node_exporter & fi ps -ef | grep mongodb_exporter-linux-amd64 | grep -v grep if [ \`ehco \$? \` -nq 0 ];then /usr/bin/nohup /opt/mongodb_exporter-linux-amd64 -mongodb.uri mongodb://${vm_private_ip}:27017 else fi if [ \`ps -ef | grep mongod.conf | grep -v grep | wc -l\` -lt 0 ];then /usr/bin/mongod -f /etc/mongod.conf fi EOF chmod +x /data/mongodb/checker.sh sudo echo "*/2 * * * * /data/mongodb/checker.sh" >> ${cronfile} exit
#!/bin/bash echo "run this script in primary host" if [ `ifconfig | grep -c bond` -ne 0 ]; then iface=bond else iface=eth if [ `ifconfig | grep -c eth2` -eq 0 ]; then iface_local_flag=1 else iface_local_flag=0 fi fi vm_private_ip=`ifconfig ${iface}0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1` if [ $iface_local_flag -eq 1 ]; then vm_public_ip=${vm_private_ip} else vm_public_ip=`ifconfig ${iface}1 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1` fi install_filed=3 unknown_error=2 service_error=4 # echo "db.serverStatus()" | `which mongo` admin echo -e "\033[41;36m Checking mongo is ok \033[0m" ps -ef | grep mongo | grep -v grep if [ `echo $?` -eq 0 ];then echo "mongodb process is ok" else echo "mongodb is not running..." exit ${service_error} fi echo -e "\033[41;36m initiate replica set cluster \033[0m" echo "rs.initiate({_id:\"rs1\",members:[{_id:0,\"host\":\"${vm_private_ip}:27017\",priority:3},{_id:1,\"host\":\"$1:27017\",priority:2},{_id:2,\"host\":\"$2:27017\",priority:2}]})" | $mongo --host ${vm_private_ip} --port 27017 admin > /tmp/initiate.log grep 'ok' /tmp/initiate.log if [ `echo $?` -eq 0 ];then echo "initiate done." else echo "initiate failed" exit ${unknown_error} fi echo -e "\033[41;36m show replica set status \033[0m" echo "rs.status()" | $mongo --host ${vm_private_ip} --port 27017 admin && netstat -tunlp | grep mongod | grep -v grep echo -e "\033[41;36m set slaveOK() \033[0m" if `ping -c 4 $1 && ping -c 4 $2`;then echo "rs.slaveOk()" | $mongo --host ${vm_private_ip} --port 27017 admin echo "rs.slaveOk()" | $mongo --host $1 --port 27017 admin echo "rs.slaveOk()" | $mongo --host $2 --port 27017 admin else echo "the network is unreachable..." exit ${unknown_error} fi
部署完成
看完了這篇文章,相信你對“如何全自動安裝Mongo副本集”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。