mongodb 3.0.4 shell安装卸载脚本

环境:RedHat
安装脚本:
[root@PC download]# cat install_mongo.sh 
#请记得先将安装包放到/download文件夹下

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 run 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 '开始启动数据库......'
$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/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 "$1" in' >> /etc/init.d/mongod
echo '  start)' >> /etc/init.d/mongod
echo '      echo "Starting MongoDB Server..."' >> /etc/init.d/mongod
echo '      $PROGRAM --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 is 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/mongod
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 '创建用户...'
echo -e $sql | mongo admin --shell

#验证能否以service方式启动
echo '验证能否以service方式重启mongodb......'
service mongod stop
sleep 5;
service mongod start

#修改配置文件,以认证的方式访问mongo
echo -e "auth=true" >> $BaseDir/mongod.cnf
sleep 5;
service mongod restart
echo "请手动验证下auth是否配置成功!"
#mongo admin 
#db.auth("admin","admin123");

echo "最后敲下回车即可。"
---卸载脚本:
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

你可能感兴趣的:(#,Mongodb,安装,4,Mongodb,学习)