環境: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