mongodb 3.0.4 shell安裝卸載指令碼

來源:互聯網
上載者:User
環境:RedHat
安裝指令碼:
[root@PC download]# cat install_mongo.sh #請記得先將安裝包放到/download檔案夾下Dir='/download'BaseDir='/usr/local/mongodb'DataDir='/data/mongo'#聲明建立使用者的sqlsql='db.createUser(\n  {\n    user: "admin",\n    pwd: "admin123",\n    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]\n  }\n)\ndb.grantRolesToUser( "admin" , [ { role: "readWriteAnyDatabase", db: "admin" } ]);\n'#安裝cd $Dirtar -xvf mongodb-linux-x86_64-3.0.4.tgzmv mongodb-linux-x86_64-3.0.4 $BaseDir mkdir  $DataDircd $DataDirmkdir datatouch logs groupadd mongodbuseradd -g mongodb mongodbchown -R mongodb:mongodb $BaseDir#配置PATHsed -i '/^PATH=/s/$/:\/usr\/local\/mongodb\/bin/' /root/.bash_profilesource /root/.bash_profilesed -i '/^PATH=/s/$/:\/usr\/local\/mongodb\/bin/' /home/mongodb/.bash_profilesource /home/mongodb/.bash_profile#建立設定檔echo logpath=${DataDir}/logs >> $BaseDir/mongod.cnfecho logappend = true  >> $BaseDir/mongod.cnfecho '#fork and run in background' >> $BaseDir/mongod.cnfecho fork = true >> $BaseDir/mongod.cnfecho '#port = 27017' >> $BaseDir/mongod.cnfecho dbpath=${DataDir}/data >> $BaseDir/mongod.cnfecho '#location of pidfile' >> $BaseDir/mongod.cnfecho pidfilepath=${DataDir}/mongod.pid  >> $BaseDir/mongod.cnf#啟動資料庫echo '開始啟動資料庫......'$BaseDir/bin/mongod --config $BaseDir/mongod.cnf #設定開機自動啟動echo ${BaseDir}/bin/mongod --config ${BaseDir}/mongod.cnf >> /etc/rc.local#設定以service mongodb start方式啟動資料庫echo '#!/bin/sh' >> /etc/init.d/mongodecho  CONFIG=${BaseDir}/mongod.cnf >> /etc/init.d/mongodecho PROGRAM=${BaseDir}/bin/mongod >> /etc/init.d/mongodPidCommand="ps -ef | grep 'mongod --config' | grep -v grep | awk '{print \$2}'"echo MONGOPID='`'$PidCommand'`' >> /etc/init.d/mongodecho ' ' >> /etc/init.d/mongodecho 'test -x $PROGRAM || exit 0' >> /etc/init.d/mongodecho ' ' >> /etc/init.d/mongodecho 'case "$1" in' >> /etc/init.d/mongodecho '  start)' >> /etc/init.d/mongodecho '      echo "Starting MongoDB Server..."' >> /etc/init.d/mongodecho '      $PROGRAM --config $CONFIG &' >> /etc/init.d/mongodecho '  ;;' >> /etc/init.d/mongodecho '  stop)' >> /etc/init.d/mongodecho '      echo "Stopping MongoDB Server..."' >> /etc/init.d/mongodecho '      if [ ! -z "$MONGOPID" ]; then' >> /etc/init.d/mongodecho '          kill -15 $MONGOPID' >> /etc/init.d/mongodecho '      fi' >> /etc/init.d/mongodecho '  ;;' >> /etc/init.d/mongodecho '  status)' >> /etc/init.d/mongodecho '      if [  -z "$MONGOPID" ]; then' >> /etc/init.d/mongodecho '          echo "MongoDB is not running!"' >> /etc/init.d/mongodecho '      else' >> /etc/init.d/mongodecho '          echo "MongoDB is running!("$MONGOPID")"' >> /etc/init.d/mongodecho '      fi' >> /etc/init.d/mongodecho '  ;;' >> /etc/init.d/mongodecho '  restart)' >> /etc/init.d/mongodecho '     echo "Shutting down MongoDB Server..."' >> /etc/init.d/mongodecho '     if [ ! -z "$MONGOPID" ]; then' >> /etc/init.d/mongodecho '         kill -15 $MONGOPID' >> /etc/init.d/mongodecho '     fi' >> /etc/init.d/mongodecho '     echo "Starting MongoDB..."' >> /etc/init.d/mongodecho '     $PROGRAM --config $CONFIG &' >> /etc/init.d/mongodecho '  ;;' >> /etc/init.d/mongodecho '  *)' >> /etc/init.d/mongodecho '  log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}" ' >> /etc/init.d/mongodecho 'exit 1' >> /etc/init.d/mongodecho 'esac' >> /etc/init.d/mongodecho 'exit 0          ' >> /etc/init.d/mongodchmod 744 /etc/init.d/mongod#建立使用者echo '建立使用者...'echo -e $sql | mongo admin --shell#驗證能否以service方式啟動echo '驗證能否以service方式重啟mongodb......'service mongod stopsleep 5;service mongod start#修改設定檔,以認證的方式訪問mongoecho -e "auth=true" >> $BaseDir/mongod.cnfsleep 5;service mongod restartecho "請手動驗證下auth是否配置成功!"#mongo admin #db.auth("admin","admin123");echo "最後敲下斷行符號即可。"
---卸載指令碼:
BaseDir='/usr/local/mongodb'DataDir='/data/mongo'service mongod stoprm -rf $BaseDirrm -rf $DataDirrm -rf /etc/init.d/mongoduserdel mongodb#groupdel mongodb#刪除 PATH#root使用者sed -i '/^PATH=/s/:\/usr\/local\/mongodb\/bin//' /root/.bash_profilesource /root/.bash_profile#mysql使用者sed -i '/^PATH=/s/:\/usr\/local\/mongodb\/bin//' /home/mongodb/.bash_profilesource /home/mongodb/.bash_profile#取消開機自動啟動sed -i '/mongod --config/d' /etc/rc.local
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.