Automatic installation of MongoDB Master, Slave, arbiter scripts

Source: Internet
Author: User
Tags chmod pkill

Recently, a new project needs to use MongoDB's master,slave,arbiter architecture, go to the official website to turn over the document, write a simple script. The script can be found on my GitHub https://github.com/sangrealest


#!/bin/bash#author:shanker#time:2016/03/04slaveip= ' 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  configmongosport= ' 27015 ' #Mongo  config  server configmongoconfigip= ' 127.0.0.1 ' mongoconfigname= ' database-config ' mongoconfigdbpath= '/var/lib/ Mongodc ' mongoconfiglogpath= '/var/log/mongodc ' mongoconfigport= ' 27014 ' function createconfig () {#     read -p  "Please inpute role name (master/slave/arbiter)"  role     role=$1    mongoconfig= "./mongod-$role"     if [  ! -f  "$mongoConfig"  ]    then         echo  "dbpath=/var/lib/mongodb-$role"  >> $mongoConfig          echo&nbsP; " logpath=/var/log/mongodb-$role/mongodb.log " >> $mongoConfig          echo  "Logappend=true"  >> $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  "Replset = rs0"  >> $mongoConfig         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&NBSP, .... /.    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 "    case $1 in        master| master| MASTER)             member= ("$MasterName: $ Mastermongoport " " $SlaveName: $SlaveMongoPort ")              echo  "Rs.initiate ()"  | /usr/bin/mongo  $MasterName: $MasterMongoPort              sleep 3             for count in ${member[@]}             do                 echo  "Rs.add (\" $count \ ")"  | /usr/bin/mongo  $MasterName : $MasterMongoPort                  sleep 3            done             echo  "Rs.addarb (\" $ArbiterName: $ArbiterMongoPort \ ")" |/usr/bin/mongo $ Mastername: $MasterMongoPort             echo  " Rs.status () "|/usr/bin/mongo  $MasterName: $MasterMongoPort         ;;                 *)          exit 0        ;;     esac}case $1 in    master| master| Master)         createConfig  "master"          installMongoService  "Master"          setupreplset  "Master"     ;;     slave| slave| SLAVE)         createConfig  "SLAVE"          installMongoService  "Slave"         setupreplset   "Slave"     ;;     arbiter| arbiter| Arbiter)         createConfig  "Arbiter"          installMongoService  "Arbiter"          setupreplset  "Arbiter"     ;;     all| all| All)          #Install  Slave         createConfig  "Slave"         installMongoService  "Slave"         setupReplSet  "slave"           #install  ARBITER  &NBsp;     createconfig  "Arbiter"          installmongoservice  "Arbiter"         setupReplSet  "arbiter"          #Install  Master         createConfig  "Master"         installMongoService  " Master "        setupReplSet " master "    ;;     uninstall| 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} '             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*         fi    ;;     *)       &Nbsp; echo  "Please inpute $0 master, slave or arbiter"          exit 0    ;;     esac


This article is from "Tianya Horizon" blog, please be sure to keep this source http://shanker.blog.51cto.com/1189689/1747654

Automatic installation of MongoDB Master, Slave, arbiter scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.