mongodb起動shellスクリプト

6113 ワード

文書ディレクトリ
  • mongodb起動スクリプト
  • 1.スクリプトファイル
  • の作成
  • 2.mongodb起動shellスクリプトコード
  • 3.システムサービスと電源オン
  • 4.mongodbオープン、停止、再起動操作
  • クラスタ起動スクリプト
  • mongodb起動スクリプト
    1.スクリプトファイルの作成
    vim /etc/init.d/mongodb
    

    2.mongodb起動shellスクリプトコード
    #!/bin/bash
    # author:wudd
    # mongodb    
    name=mongod
    route_name=mongos
    path_bin=/httx/run/mongodb/bin/
    path=/httx/run/mongodb/
    path_conf=/httx/run/mongodb/conf/
    shard_configs=(config.conf rep1.conf rep2.conf rep3.conf)
    route_config=mongos.conf
    case "$1" in
      start)
        if [  ! -n "$2" ];then
          echo "       !"
        else
          start $2
        fi
      ;;
      stop)
        if [  ! -n "$2" ];then
          echo "       !"
        else
          stop $2
        fi
      ;;
      restart)
        if [  ! -n "$2" ];then
          echo "       !"
        else
          restart $2
        fi
      ;;
      restartShard)
        restartShard
      ;;
      restartRoute)
        restartRoute
      ;;
    esac
    
    #              
    function isContainElement(){
        resultCode=0
      for var in ${shard_configs[@]};
      do
        if [ "$1" == "$var" ];then
          resultCode=1
        else
            resultCode=0
        fi
      done
      return $resultCode
    }
    
    
    #           
    function isExistFile(){
      file=${path_conf}$1
      if [ -f "$file" ];then
        return 1
      else
        return 0
      fi
    }
    
    #       
    function start(){
      isExistFile $1
      if [ $? -eq 0 ];then
        echo "          【$1】    !"
      else
        echo "-----------------------------   【$1】     -----------------------------"
        
        #              ,   ,      mongos
        run_file_name=${name}
        if [ "$1" == "${route_config}" ];then
          run_file_name=${route_name}
        else
          run_file_name=${name}
        fi
        echo "     :${path_bin}$run_file_name -f ${path_conf}$1"
        ${path_bin}$run_file_name -f ${path_conf}$1
        if [ $? -eq 0 ];then
          echo "【$1】    "
        else
          echo "【$1】    "
        fi
        echo "-----------------------------   【$1】     -----------------------------"
      fi
    }
    
    #         
    function stop(){
      isExistFile $1
      if [ $? -eq 0 ];then
        echo "          【$1】    !"
      else
        echo "-----------------------------   【$1】     -----------------------------"
        server_pid=`ps -ef | grep $1 |grep -v grep | awk '{print $2}'`
        if [ "$server_pid" != "" ];then
            echo "  【$1】  :$server_pid"
            kill -9 $server_pid
          if [ $? -eq 0 ];then
            echo "【$1】    "
          else
            echo "【$1】    "
          fi
        else
            echo "【$1】      "
        fi
        echo "-----------------------------   【$1】     -----------------------------"
      fi
    }
    
    #         
    function restart(){
      isExistFile $1
      if [ $? -eq 0 ];then
        echo "          【$1】    !"
      else
        echo "-----------------------------   【$1】     -----------------------------"
        server_pid=`ps -ef | grep $1 |grep -v grep | awk '{print $2}'`
        if [ "$server_pid" != "" ];then
          echo "  【$1】  :$server_pid"
          kill -9 $server_pid
          if [ $? -eq 0 ];then
            echo "【$1】    "
          else
            echo "【$1】    "
          fi
        else
          echo "【$1】      "
        fi
    
        #              ,   ,      mongos
        run_file_name=${name}
        if [ "$1" == "${route_config}" ];then
          run_file_name=${route_name}
        else
          run_file_name=${name}
        fi
    
        echo "     :${path_bin}$run_file_name -f ${path_conf}$1"
        ${path_bin}$run_file_name -f ${path_conf}$1
        if [ $? -eq 0 ];then
          echo "【$1】    "
        else
          echo "【$1】    "
        fi
        echo "-----------------------------   【$1】     -----------------------------"
      fi
    }
    
    #       
    function restartShard(){
      echo "-----------------------------          start -----------------------------"
      for var in ${shard_configs[@]};
      do
        restart $var
      done
      echo "-----------------------------          end -----------------------------"
    }
    
    #       
    function restartRoute(){
      echo "-----------------------------       start -----------------------------"
      restart ${route_config}
      echo "-----------------------------       start -----------------------------"
    }
    

    3.システムサービスへの加入と起動
    cd /etc/init.d/
    #      
    chmod a+x mongodb
    #      
    chkconfig --add mongodb
    #      
    chkconfig mongodb on
    #      
    shutdown -r
    

    4.mongodbオープン、停止、再起動操作
    #  
    service mongodb start
    #  
    service mongodb stop
    #  
    service mongodb restart
    #    
    service mongodb restartShard
    #      
    service mongodb restartRoute
    

    クラスタ起動スクリプト
    #!/bin/bash
    
    #sudo mkdir -p /data/{db1,db2,db3,db30,db31,db32,configdb}
    
    ##--Setup Replaction Server--
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db1 --logpath=/var/log/db1.log --logappend --journal --replSet inner --port 27020 --shardsvr --nohttpinterface
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db2 --logpath=/var/log/db2.log --logappend --journal --replSet inner --port 27021 --shardsvr --nohttpinterface
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db3 --logpath=/var/log/db3.log --logappend --journal --replSet inner --port 27022 --shardsvr --nohttpinterface
    ##--SetUp Replaction Server--
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db30 --logpath=/var/log/db30.log --logappend  --journal --replSet jack --port 27030 --shardsvr --nohttpinterface
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db31 --logpath=/var/log/db31.log --logappend  --journal --replSet jack --port 27031 --shardsvr --nohttpinterface
    sudo /usr/local/mongodb/bin/mongod --fork --dbpath=/data/db32 --logpath=/var/log/db32.log --logappend  --journal --replSet jack --port 27032 --shardsvr --nohttpinterface
    
    ##--Sharding Configure config_server mongos--
    sudo /usr/local/mongodb/bin/mongod --configsvr
    sleep 20
    sudo /usr/local/mongodb/bin/mongos --fork --logappend --logpath=/var/log/dbconf.log --configdb 127.0.0.1:27019