一键安装 MongoDB 数据库脚本

#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#QQ:            29308620
#Date:          2021-02-19
#FileName:      install_mongodb.sh
#URL:           http://www.wangxiaochun.com
#Description:       The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
file=mongodb-linux-x86_64-ubuntu1804-4.4.4.tgz
url=https://fastdl.mongodb.org/linux/$file
db_dir=/data/db
install_dir=/usr/local
port=27017

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$2" && $MOVE_TO_COL
    echo -n "["
    if [ $1 = "success" -o $1 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $1 = "failure" -o $1 = "1"  ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo                                                                                                                              
}

os_type (){
   awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}

check () {
    [ -e $db_dir -o -e $install_dir/mongodb ] && { color 1 "MongoDB 数据库已安装";exit; }
    if [ `os_type` = "CentOS" ];then
        rpm -q curl  &> /dev/null || yum install -y -q curl
    elif [ `os_type` = "Ubuntu" ];then
        dpkg -l curl &> /dev/null || apt -y install curl
    else
        color 1 不支持当前操作系统
        exit
    fi
}

file_prepare () {
    if [ ! -e $file ];then
        curl -O  $url || { color 1  "MongoDB 数据库文件下载失败"; exit; } 
    fi
}
install_mongodb () {
    tar xf $file -C $install_dir
    mkdir -p $db_dir
    ln -s $install_dir/mongodb-linux-x86_64-* $install_dir/mongodb
    echo PATH=$install_dir/mongodb/bin/:'$PATH' > /etc/profile.d/mongodb.sh
    . /etc/profile.d/mongodb.sh
    mongod --dbpath $db_dir --bind_ip_all --port $port --logpath $db_dir/mongod.log --fork
    [ $? -eq 0 ] && color 0 "MongoDB 数据库安装成功!" || color 1 "MongoDB 数据库安装失败!"
}

check 
file_prepare
install_mongodb
分享到:更多 ()

评论 5

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #5

    老王再写写吧

    公博义2年前 (2021-09-03)回复
  2. #4

    老王同志讲课真是帅爆了..爆了爆了..

    ddd2年前 (2021-09-06)回复
  3. #3

    老王yyds

    知本知至2年前 (2021-09-17)回复
  4. #2

    你这越来越不行了傲,王哥

    小徐聊IT2年前 (2021-11-10)回复
  5. #1

    王老师,今年三十、初一怎么没写脚本呀,我还等着看呢

    N411年前 (2022-02-01)回复