Environment: RedHat
Installation script:
[Root@pc download]# cat install_mongo.sh #请记得先将安装包放到/download folder dir= '/download ' basedir= '/usr/local/mongodb ' Datadir= '/data/mongo ' #声明创建用户的sql sql= ' Db.createuser (\ n {\ n User: "admin", \ n pwd: "admin123", \ n roles: [{ Role: "Useradminanydatabase", DB: "admin"}]\n}\n) \ n db.grantrolestouser ("admin", [{role: "Readwriteanydatabase", DB: "admin"}]) \ n ' #安装 cd $Dir tar-xvf mongodb-linux-x86_64-3.0.4.tgz mv mongodb-linux-x86_64-3.0.4 $BaseDir mkdir
$DataDir CD $DataDir mkdir data touch logs Groupadd MongoDB useradd-g mongodb mongodb chown-r mongodb:mongodb $BaseDir #配置PATH sed-i '/^path=/s/$/:\/usr\/local\/mongodb\/bin/'/root/.bash_profile source/root/.bash_profile sed-i '/^PATH =/s/$/:\/usr\/local\/mongodb\/bin/'/home/mongodb/.bash_profile source/home/mongodb/.bash_profile #创建配置文件 Echo Logpath=${datadir}/logs >> $BaseDir/mongod.cnf echo logappend = True >> $BaseDir/mongod.cnf Echo ' #fork and R UN in background ' >> $BaseDir/mongod. cnf echo Fork = True >> $BaseDir/mongod.cnf echo ' #port = 27017 ' >> $BaseDir/mongod.cnf echo Dbpath=${datadir }/data >> $BaseDir/mongod.cnf Echo ' #location of Pidfile ' >> $BaseDir/mongod.cnf Echo Pidfilepath=${datadir}
/mongod.pid >> $BaseDir/mongod.cnf #启动数据库 echo ' Start database ... ' $BaseDir/bin/mongod--config $BaseDir/mongod.cnf #设置开机自动启动 echo ${basedir}/bin/mongod--config ${basedir}/mongod.cnf >>/etc/rc.local #设置以service MongoDB Start mode launch database echo ' #!/bin/sh ' >>/etc/init.d/mongod echo config=${basedir}/mongod.cnf >>/etc/init.d/ Mongod echo program=${basedir}/bin/mongod >>/etc/init.d/mongod pidcommand= "Ps-ef | grep ' Mongod--config ' | Grep-v grep | awk ' {print \$2} ' "Echo mongopid=" $PidCommand ">>/etc/init.d/mongod Echo ' >>/etc/init.d/mongod Echo ' Test-x $PROGRAM | | Exit 0 ' >>/etc/init.d/mongod echo ' >>/etc/init.d/mongod echo ' case ' $ "in ' >>/etc/init.d/mongod E Cho ' Start) '>>/etc/init.d/mongod Echo ' echo ' starting MongoDB Server ... "' >>/etc/init.d/mongod Echo ' $PROGRA M--config $CONFIG & ' >>/etc/init.d/mongod echo ';; ' >>/etc/init.d/mongod echo ' stop ' >>/etc /init.d/mongod echo ' echo ' stopping MongoDB Server ... "' >>/etc/init.d/mongod echo ' if [! -Z "$MONGOPID"]; Then ' >>/etc/init.d/mongod Echo ' kill-15 $MONGOPID ' >>/etc/init.d/mongod echo ' fi ' >> /etc/init.d/mongod echo ';; ' >>/etc/init.d/mongod echo ' status ' >>/etc/init.d/mongod echo ' if [ -Z "$MONGOPID"]; Then ' >>/etc/init.d/mongod echo ' echo ' MongoDB are not running! "' >>/etc/init.d/mongod Echo ' Else ' >>/etc/init.d/mongod echo ' echo ' MongoDB is running! ("$MONGOPID") "' >>/etc/init.d/mongod echo ' fi ' >>/etc/init.d/mongod echo ';; ' >>/etc/init.d/m Ongod echo ' Restart ' >>/etc/init.d/moNgod echo ' echo ' shutting down MongoDB Server ... "' >>/etc/init.d/mongod echo ' if [! -Z "$MONGOPID"]; Then ' >>/etc/init.d/mongod Echo ' kill-15 $MONGOPID ' >>/etc/init.d/mongod echo ' fi ' >>/ Etc/init.d/mongod Echo ' echo ' starting MongoDB ... ' >>/etc/init.d/mongod echo ' $PROGRAM--config $CONFIG & ' >>/etc/init.d/mongod echo ';; ' >>/etc/init.d/mongod Echo ' *) ' >>/etc/init.d/mongod Echo ' Log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}" ' >>/etc/init.d/mongod echo ' exit 1 ' > >/etc/init.d/mongod echo ' esac ' >>/etc/init.d/mongod echo ' exit 0 ' >>/etc/init.d/mongod chmod 744/etc/init.d/mongod #创建用户 echo ' Create user ... ' echo-e $sql |
MONGO admin--shell #验证能否以service方式启动 Echo ' Verify that you can restart MongoDB with service ... ' Service Mongod stop sleep 5;
Service Mongod start #修改配置文件, authenticated access to MONGO Echo-e "Auth=true" >> $BaseDir/mongod.cnf sleep 5; SerVice Mongod restart echo "Please manually verify that the AUTH is configured successfully!" #mongo admin #db. Auth ("admin", "admin123"); echo "finally hit the Enter."
"
---unload script:
Basedir= '/usr/local/mongodb '
datadir= '/data/mongo '
service mongod stop
rm-rf $BaseDir
RM-RF $ DataDir
rm-rf/etc/init.d/mongod
userdel mongodb
#groupdel mongodb
#删除 PATH
#root用户
sed-i ' /^path=/s/:\/usr\/local\/mongodb\/bin//'/root/.bash_profile
source/root/.bash_profile
#mysql用户
Sed-i '/^path=/s/:\/usr\/local\/mongodb\/bin//'/home/mongodb/.bash_profile
source/home/mongodb/.bash_ Profile
#取消开机自动启动
sed-i '/mongod--config/d '/etc/rc.local