如何全自动安装Mongo副本集
发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,小编给大家分享一下如何全自动安装Mongo副本集,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!在opensuse12 sp1上使用shell脚本自动安装部署Mongodb副本集,集群
千家信息网最后更新 2025年02月06日如何全自动安装Mongo副本集
小编给大家分享一下如何全自动安装Mongo副本集,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
在opensuse12 sp1上使用shell脚本自动安装部署Mongodb副本集,集群一共三个虚拟机节点
Deploy Mongodb Replica Set
mongo# chmod +x deployinstance.sh initreplicaset.sh# 三个节点上分别运行该脚本mongo# ./deployinstance.sh# ip1 和ip2 是mongodb中作为secondary的节点ip,然后在Primary节点上运行该脚本mongo# ./initreplicaset.sh ip1 ip2
deployinstance.sh
#!/bin/bash# Deploy mongodb,node-exporter,mongodb-exporter in suse 12 sp1cronfile="/var/spool/cron/tabs/root"mongocmd="/usr/bin/mongod"if [ `ifconfig | grep -c bond` -ne 0 ]; then iface=bondelse iface=eth if [ `ifconfig | grep -c eth2` -eq 0 ]; then iface_local_flag=1 else iface_local_flag=0 fifivm_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.servicesystemctl disable SuSEfirewall2.service# install mongodbecho -e "\033[41;36m import mongodb public key \033[0m"sudo rpm --import https://www.mongodb.org/static/pgp/server-3.4.ascecho -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-osssudo zypper addrepo --gpgcheck "https://repo.mongodb.org/zypper/suse/12/mongodb-org/3.4/x86_64/" mongodbsudo zypper -n install mongodb-orgcp /etc/mongod.conf{,.`date +%F`}echo -e "\033[41;36m Adding mongodb data and logs directory \033[0m"mkdir -pv /data/mongodb/datamkdir -pv /var/log/mongodb/echo -e "\033[41;36m Updating mongodb configure files \033[0m"cat << EOF > /etc/mongod.confdbpath=/data/mongodb/datalogpath=/var/log/mongodb/pidfilepath=/var/run/mongodb.piddirectoryperdb=truelogappend=truereplSet=rs1bind_ip=${vm_private_ip}smallfiles=trueport=27017oplogSize=5120fork=truejournal=truelogRotate=renameEOFcat << 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 exitEOFchmod +x /data/mongodb/logRotate.shchown -R mongod:mongod /dataecho -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.confecho -e "\033[41;36m Checking Mongodb Process \033[0m"ps -ef | grep /etc/mongod | grep -v grepif [ `echo $?` -eq 0 ];then echo "mongodb Running now"else echo "mongodb start failed" exit 3fiecho -e "\033[41;36m Deploy Node-exporter \033[0m"cd /optwget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gztar 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 /optwget https://github.com/dcu/mongodb_exporter/releases/download/v1.0.0/mongodb_exporter-linux-amd64chmod +x mongodb_exporter-linux-amd64/usr/bin/nohup /opt/mongodb_exporter-linux-amd64 -mongodb.uri mongodb://${vm_private_ip}:27017ps -ef | grep node_exporter | grep -v grepif [ `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"fips -ef | grep mongodb_exporter-linux-amd64 | grep -v grepif [ `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"ficat << EOF > /data/mongodb/checker.sh#!/bin/bash#check exporter is healthps -ef | grep node_exporter | grep -v grepif [ \`ehco \$? \` -nq 0 ];then /usr/bin/nohup /opt/node_exporter-0.16.0.linux-amd64/node_exporter & fips -ef | grep mongodb_exporter-linux-amd64 | grep -v grepif [ \`ehco \$? \` -nq 0 ];then /usr/bin/nohup /opt/mongodb_exporter-linux-amd64 -mongodb.uri mongodb://${vm_private_ip}:27017elsefiif [ \`ps -ef | grep mongod.conf | grep -v grep | wc -l\` -lt 0 ];then /usr/bin/mongod -f /etc/mongod.conffiEOFchmod +x /data/mongodb/checker.shsudo echo "*/2 * * * * /data/mongodb/checker.sh" >> ${cronfile}exit
initreplicaset.sh
#!/bin/bashecho "run this script in primary host"if [ `ifconfig | grep -c bond` -ne 0 ]; then iface=bondelse iface=eth if [ `ifconfig | grep -c eth2` -eq 0 ]; then iface_local_flag=1 else iface_local_flag=0 fifivm_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`fiinstall_filed=3unknown_error=2service_error=4# echo "db.serverStatus()" | `which mongo` adminecho -e "\033[41;36m Checking mongo is ok \033[0m"ps -ef | grep mongo | grep -v grepif [ `echo $?` -eq 0 ];then echo "mongodb process is ok"else echo "mongodb is not running..." exit ${service_error}fiecho -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.loggrep 'ok' /tmp/initiate.logif [ `echo $?` -eq 0 ];then echo "initiate done."else echo "initiate failed" exit ${unknown_error}fiecho -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 grepecho -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 adminelse echo "the network is unreachable..." exit ${unknown_error}fi
部署完成
看完了这篇文章,相信你对"如何全自动安装Mongo副本集"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
节点
副本
脚本
全自动
三个
篇文章
运行
完了
更多
知识
行业
资讯
资讯频道
集群
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
计算机网络安全手抄报边框
学校网络安全清查表
比较优秀的数据库
服务器轴流风机怎么样
c语言软件开发步骤
问道数据库cwjyzx
软件开发工作适合学什么专业
甘肃广电网络安全
潍坊旭辰网络技术有限公司
魔兽赛季服逐风服务器
网络安全管理实训心得
阿里云服务器的管理
关系数据库的各项操作的目的
bim数据库构建组成数据表
生动的网络技术课堂
斑马网络技术面试
互联网大会数知科技
杭州盛炬网络技术有限公司
网络安全周什么时候
如何加强信息共享网络安全防护
vpc网络技术
联通4g网络技术方案
东成服务器交换机回收
北京市公安局网络安全总队号码
长宁区综合软件开发管理方法
网络安全知识从哪开始学
上海网络软件开发记录
服务器后端语言
云象网络技术开发
mysql数据库锁表问题