自动安装MongoDBMaster,Slave,Arbiter脚本-成都快上网建站

自动安装MongoDBMaster,Slave,Arbiter脚本

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

成都创新互联公司自2013年起,是专业互联网技术服务公司,拥有项目成都做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元宾县做网站,已为上家服务,为宾县各地企业和个人服务,联系电话:18980820575

最近有一个新项目需要用到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

文章题目:自动安装MongoDBMaster,Slave,Arbiter脚本
本文URL:http://kswjz.com/article/ppjsjj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流