温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

自动安装MongoDB Master, Slave, Arbiter脚本

发布时间:2020-03-03 21:24:31 来源:网络 阅读:1570 作者:shanker 栏目:MongoDB数据库

发现有人抄袭我的文章,竟然不写出处,想想也挺开心的,还会被抄袭,以前上初中的时候都是抄袭别人的作业。这篇文章我都改动过好多次了,那哥们抄袭的还是之前有点bug的代码。望他看到后,重新抄一遍,免得误人子弟了。



最近有一个新项目需要用到MongoDB的Master,Slave,Arbiter的架构,去官网翻了一下文档,写了一个简陋的脚本。脚本可以在我的github上找到https://github.com/sangrealest。

安装之前确保已经提前设置好dns,或者hosts文件已经设置好。防火墙做好相应的规则,或者关闭防火墙。


首先生成mongodb的证书文件,然后copy到Master, Slave, Arbiter目录下,并添加到配置文件里面。

openss rand -base64 741 >/etc/mongodb.key
chmod 600 /etc/mongodb.key
#!/bin/bash
#Author:Shanker
#set -x
#set -o nounset

source ./mongo_config

function createConfig(){
    role=$1
    mongoConfig="./mongod-$role"
    if [ ! -f "$mongoConfig" ]
    then
        echo "dbpath=/var/lib/mongodb-$role" >>$mongoConfig
        echo "logpath=/var/log/mongodb-$role/mongodb.log" >>$mongoConfig
        echo "logappend=true" >>$mongoConfig
        echo "keyFile=$MongoKeyFile" >>$mongoConfig
        if [ "$role" == "master" ]
        then
            echo "port=$MasterMongoPort" >>$mongoConfig
        elif [ "$role" == "slave" ]
        then
            echo "port=$SlaveMongoPort" >>$mongoConfig
        elif [ "$role" == "arbiter" ]
        then
            echo "port=$ArbiterMongoPort" >>$mongoConfig
        fi
        echo "nohttpinterface=true" >>$mongoConfig
        echo "nojournal=true" >>$mongoConfig
        echo "replSet=rs0" >>$mongoConfig

        cp $mongoConfig /etc/
        chmod 644 /etc/$mongoConfig

    fi

}

function installMongoService(){
    
    echo "installing mongoserver"
    if [ ! -f "/usr/bin/mongo" ]
    then
        tar zxvf ./mongodb-linux-x86_64-2.4.9.tgz
        cd ./mongodb-linux-x86_64-2.4.9/bin/
        cp * /usr/bin
        cd ../..
    else
        echo "mongo server file already exist"
    fi

    cp mongod-$1.sh /etc/init.d/
    chmod 755 /etc/init.d/mongod-$1.sh
    /etc/init.d/mongod-$1.sh start
    i=1
    until ((i=="0"))
    do
        /bin/cat /var/log/mongodb-$1/mongodb.log | grep "waiting for connection"
        i=$?
        sleep 3
        echo "waiting for mongodb ready"
    done
    echo "mongodb is ready"
    sleep 4
}

function setupReplSet(){

    echo "setup mongodb replicationset"
    member="$SlaveName:$SlaveMongoPort"
    echo "rs.initiate()" | /usr/bin/mongo $MasterName:$MasterMongoPort
    sleep 3
    echo "rs.add(\"$count\")" | /usr/bin/mongo $MasterName:$MasterMongoPort
    sleep 3
    echo "rs.addArb(\"$ArbiterName:$ArbiterMongoPort\")"|/usr/bin/mongo $MasterName:$MasterMongoPort
    echo "rs.status()"|/usr/bin/mongo $MasterName:$MasterMongoPort
}

case $1 in
    "master"|"Master")
        createConfig "master"
        installMongoService "master"
        setupReplSet 
        ;;

    "slave"|"Slave")
        createConfig "slave"
        installMongoService "slave"
        ;;

    "arbiter"|"Arbiter")
        createConfig "arbiter"
        installMongoService "arbiter"
        ;;
    "uninstall"|"Uninstall")
        echo "this will remove all mongo files!!!"

        if [ -f "/usr/bin/pkill" ]
        then
             pkill -9 mongod
        else
            kill -9 `ps aux | grep mongo | grep -v grep | awk -F" " '{print $2}'`
        fi
        rm -rf /etc/mongo*
        rm -rf /etc/init.d/mongo*
        rm -rf /var/lib/mongo*
        rm -rf /var/log/mongo*
        rm -rf /usr/bin/mongo*
        rm -rf /var/run/mongo*
        ;;
    *)
        echo "Please inpute $0 Master, slave or ARBITER"
        exit 0
         ;;
esac


$ cat mongo_config 
SlaveIP='10.128.129.45'
SlaveName='Databse-Slave'
SlaveMongoPort='27017'
ArbiterIP='10.128.129.46'
ArbiterName='Database-Arbiter'
ArbiterMongoPort='27017'
MasterIP='10.128.129.44'
MasterName='Database-Master'
MasterMongoPort='27017'
#Mongos config
MongosPort='27015'
#Mongo COnfig Server Config
MongoConfigIP='127.0.0.1'
MongoConfigName='Database-Config'
MongoConfigDBPath='/var/lib/mongodc'
MongoConfiglogpath='/var/log/mongodc'
MongoConfigPort='27014'
MongoKeyFile='/etc/mongodb.key'



$ cat mongod-master.sh 
#!/bin/bash
#source /etc/mongod-start.conf
DBLocation='/var/lib/mongodb-master/'
LogFolder='/var/log/mongodb-master/'
StartProcess='/usr/bin/mongod -f /etc/mongod-master'
PidFile='/var/lib/mongodb-master/mongod-master.lock'
uid=`id | cut -d\( -f1 | cut -d= -f2`
if [ ! -d "$DBLocation" ];then
mkdir $DBLocation
fi 
if [ ! -d "$LogFolder" ];then
mkdir $LogFolder
fi 
if [ ! -f "$PidFile" ];then
touch $PidFile
fi 
PidFileNum=`/bin/cat $PidFile`
CurrentPid=`ps aux|grep "$StartProcess"|grep -v "grep"|awk -F" " '{print $2}'`
ProcessStart()
{
        if [ "x$CurrentPid" != "x" ];then
                echo "mongod process already runing!! PID is $CurrentPid. "
        else
                exec $StartProcess &  
sleep 2
                NewPidNum=`ps aux|grep "$StartProcess"|grep -v "grep"|awk -F" " '{print $2}'`
                echo "mongod process is running, pid is $NewPidNum"
                #PidFileNum=`/bin/cat $PidFile`
                #
                #if [ $NewPidNum != $PidFileNum ];then
                #       echo "Process is runing, but pid number is not match the pid file."
                #       echo "pid number is $NewPidNum"
                #fi
        fi
}
ProcessStop()
{
        if [ "x$CurrentPid" == "x" ];then
                echo "mongod process already Stop!! "
        else
                kill -15 $CurrentPid
                sleep 3
                NewPidNum=`ps aux|grep "\"$StartProcess\""|grep -v "grep"|awk -F" " '{print $2}'`
                if [ "x$NewPidNum" == "x" ];then
                        echo "mongod process is stop."
                else
                        kill -9 $NewPidNum
                        if [ "x$NewPidNum" != "x" ];then
                                echo "Can't kill mongod process, please call system administrator."
                        else
                                echo "mongod is stop."
                        fi
                fi
        fi
}
ProcessStatus()
{
        if [ "x$CurrentPid" != "x" ];then
                echo "mongod process is runing!! PID is $CurrentPid. "
        else
                echo "mongod not running."
        fi
}
case "$1" in
  start)
        ProcessStart
        ;;
  stop)
        ProcessStop
        ;;
  restart)
        ProcessStop
        CurrentPid=''
        ProcessStart
        ;;
  status)
        ProcessStatus
        ;;
  *)
        echo $"Usage: mongod {start|stop|status|restart}"
        RETVAL=2
        ;;
esac


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI