linux下快速安装mongodb4.0.4

1、下载源码包

# https://www.mongodb.org/dl/linux/x86_64  #手动下载地址

cd /home/soft
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz 
tar xf mongodb-linux-x86_64-4.0.4.tgz
mv mongodb-linux-x86_64-4.0.4 /home/mongodb
View Code

2、添加配置文件

cd /home/mongodb/
mkdir data logs  #创建数据文件存放目录和日志文件存放目录
vim mongodb.conf   #默认没有此文件,需要自己创建
dbpath=/home/mongodb/data  #数据文件存放目录
logpath=/home/mongodb/logs/mongodb.log  #日志文件存放目录
fork=true   #启用守护进程,true为后台运行
logappend=true  #开启日志追加添加日志
bind_ip=0.0.0.0  #默认是127.0.0.1,开启远程访问
port=27017 #默认端口,可自定义
#auth=true   #涉及到auth权限认证,根据情况是否开启
View Code

3、启动mongo

sed -i '$a export MONGODB_HOME=/home/mongodb\nexport PATH=$PATH:$MONGODB_HOME/bin' /etc/profile   #这里可以添加mongo环境变量。
. /etc/profile
mongod --version  #可查看mongo版本号

mongod --config /home/mongodb/mongodb.conf  #启动
netstat -lntp|grep 27017  #查看端口
mongo mongodb://127.0.0.1:27017  #本地登录

sed -i '$a /home/mongodb/bin/mongod --config /home/mongodb/mongodb.conf' /etc/rc.local #添加开机自启动
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT  #iptables添加27017端口、重启防火墙
View Code

4、安装脚本

cat install_mongodb.sh
#!/bin/bash
#自定义选择版本号
mongo_version="4.0.4"
mongo_package="mongodb-linux-x86_64-${mongo_version}.tgz"
MONGO_PACKET="/home/soft"
PORT="27017"
MONGO_BASEDIR="/home/mongodb"
YELLOW_COLOR='\E[1;33m'
RED_COLOR='\E[1;31m'
RES='\E[0m'
function yellow(){
          [ $# -ne 1 ] && exit 1
          echo -e  "${YELLOW_COLOR}$1${RES}" 
}
function red(){
          [ $# -ne 1 ] && exit 1
          echo -e  "${RED_COLOR}$1${RES}" 
}
function BAR(){
        i=0
         str=""
          arry=("\\" "|" "/" "-")
           while [ $i -le 100 ]
              do
               let index=i%4
                if [ $i -le 20 ];then
                    let color=44
                    let bg=34
                elif [ $i -le 45 ];then
                     let color=43
                     let bg=33
                 elif [ $i -le 75 ];then
                     let color=41
                     let bg=31
                else
                    let color=42
                    let bg=32
                fi
            printf "      \033[${color};${bg}m%-s\033[0m %d %c\r" "$str" "$i" "${arry[$index]}"
          usleep 3000
         let i=i+1
       str+="#"
         done
     printf "\n"
}
#Check if user is root 
function check_user(){
if [ $UID != 0 ];then
        echo -e "\033[31m      Error: You must be root to run this script, please use root to install\033[0m"
    exit 1
fi
clear
cat <<EOF
             ---------------------------------------------------------------------------------------------------
            |***************A script to auto-compile & install `red mongodb-linux-x86_64-${mongo_version}` on Redhat/CentOS Linux***************|
             ---------------------------------------------------------------------------------------------------

EOF
}
function check_env(){
           mongod --version && echo True >/tmp/bool.txt || echo False >/tmp/bool.txt
           Distrib="`cat /tmp/bool.txt`"
          if [ "$Distrib" = True ];then
             echo -e "\033[32m      Warning: mongodb-linux-x86_64-${mongo_version} is already installed! \033[0m"
             sleep 1 ;
             BAR
             echo -e "\033[31m      [退出] \033[0m"
             exit 1
          else
             echo -e "\033[32m      Warning: mongodb-linux-x86_64-${mongo_version} is not install \033[0m"
             echo -e "\033[34m      Install mongodb-linux-x86_64-${mongo_version},Please input y \033[0m"
             read -p "(Please input `yellow y`|`red n`): " installmongo
             case "$installmongo" in
                 y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
                    echo -e "\033[34m      You will install mongodb-linux-x86_64-${mongo_version} \033[0m"
                    installmongo
                    ;;
                 n|N)
                   echo -e "\033[31m      [退出] \033[0m"
                   exit 0
                   ;;
                  *)
                    echo -e "\033[31m      INPUT error,You will exit install mongodb-linux-x86_64-${mongo_version} \033[0m"
                    exit 1
              esac
fi
}
function installmongo(){
                [ ! -d ${MONGO_PACKET} ] && mkdir -p ${MONGO_PACKET}
                [ ! -d ${MONGO_BASEDIR} ] && mkdir -p ${MONGO_BASEDIR}
                [ ! -d ${MONGO_BASEDIR}/data ] && mkdir -p ${MONGO_BASEDIR}/data
                [ ! -d ${MONGO_BASEDIR}/logs ] && mkdir -p ${MONGO_BASEDIR}/logs
                if [ ! -f ${MONGO_PACKET}/${mongo_package} ] ;then
                   echo -e "\033[31m      There is without ${mongo_package} ! \033[0m"
                    sleep 1;
                      echo -e "\033[32m      ${mongo_package} is downloading...... \033[0m"
                      cd ${MONGO_PACKET} &&\ 
                      [ -f ${mongo_package} ] || wget https://fastdl.mongodb.org/linux/${mongo_package}
                    sleep 1;
                   echo -e "\033[32m      Installing mongo${mongo_version},please waiting...... \033[0m"
                  start_install
               else
                  echo -e "\033[32m      Installing mongo${mongo_version},please waiting...... \033[0m"
                  start_install
               fi
}
function start_install(){
                cd ${MONGO_PACKET} && tar xf ${mongo_package}
                echo -e "\033[32m      正在解压安装包... \033[0m" 
                mv mongodb-linux-x86_64-${mongo_version}/* ${MONGO_BASEDIR}
                rm -fr mongodb-linux-x86_64-${mongo_version} && cd ${MONGO_BASEDIR} \
              #echo -e 'dbpath=${MONGO_BASEDIR}/data\nlogpath=${MONGO_BASEDIR}/logs/mongodb.log\nfork=true\nlogappend=true\nbind_ip=0.0.0.0\nport=${PORT}' >${MONGO_BASEDIR}/mongodb.conf
cat >mongodb.conf <<EOF
dbpath=${MONGO_BASEDIR}/data  #数据文件存放目录
logpath=${MONGO_BASEDIR}/logs/mongodb.log  #日志文件存放目录
fork=true         #启用守护进程,true为后台运行
logappend=true    #开启日志追加添加日志
bind_ip=0.0.0.0   #默认是127.0.0.1,开启远程访问
port=$PORT        #默认端口,可自定义
#auth=true        #涉及到auth权限认证,根据情况是否开启
EOF
              sed  -i '/MONGODB_HOME*/d' /etc/profile && sed  -i '/MONGO_BASEDIR*/d' /etc/profile && source /etc/profile
              #sed -i '$a export MONGODB_HOME=${MONGO_BASEDIR}\nexport PATH=$PATH:${MONGODB_HOME}/bin' /etc/profile && source /etc/profile
cat >>/etc/profile<<EOF
export MONGODB_HOME=${MONGO_BASEDIR}
export PATH=$PATH:${MONGODB_HOME}/bin
EOF
              source /etc/profile
              mongod --config ${MONGO_BASEDIR}/mongodb.conf
              netstat -lntp|grep "$PORT" && echo "mongo is Running Successfull!" || echo "mongo is Running Failed!"
              sleep 2 && exit 0
}
check_user
check_env
View Code

5、启动脚本

cat mongodb
#!/bin/bash
#mongod - Startup script for mongod
# chkconfig: - 2345 
# description: Mongodb database.
# processname: mongod
# Source function library
. /etc/rc.d/init.d/functions
# OPTIONS
OPTIONS=" --dbpath=/home/mongodb/data --logpath=/home/mongodb/logs/mongodb.log --logappend &"
mongod="/home/mongodb/bin/mongod"
lockfile="/var/lock/subsys/mongod"
start(){
     echo -n $"Starting mongod: "
     daemon $mongod $OPTIONS
     RETVAL=$?
     echo
     [ $RETVAL -eq 0 ] && touch $lockfile
}

stop(){
      echo -n $"Stopping mongod: "
      killproc $mongod -QUIT
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && rm -f $lockfile
}
restart (){
      stop
      start
}
ulimit -n 12000
RETVAL=0
case "$1" in
      start)
             status $mongod
             start
             ;;
      stop)
            status $mongod
            stop
            ;;
 restart|reload|force-reload)
             status $mongod
             restart
             ;;
 condrestart)
           [ -f $lockfile ] && restart || :
            ;;
     status)
           status $mongod
           RETVAL=$?
           ;;
       *)
          echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
          RETVAL=1
esac
exit $RETVAL
View Code

 mv mongodb /etc/init.d/ && chmod +x /etc/init.d/mongodb 

 

转载于:https://www.cnblogs.com/qianjingchen/articles/10083851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值