一键安装 MongoDB 数据库脚本

#!/bin/bash
#
#********************************************************************
#Author: Easy
#QQ: 718911514
#Date: 2024-05-29
#FileName: install_mongodb.sh
#URL: https://blog.youkuaiyun.com/yihaitao2008?type=blog
#Description: The test script
#Copyright (C): 2024 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

1. 脚本基本信息

#!/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
#********************************************************************
  • #!/bin/bash:指定了脚本解释器为 Bash。
  • # 开头的行是注释,用于描述脚本的基本信息,包括作者、日期、文件名、描述等。

2. 定义变量

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
  • file:MongoDB 压缩包文件名。
  • url:MongoDB 下载链接。
  • db_dir:MongoDB 数据库存放路径。
  • install_dir:MongoDB 安装路径。
  • port:MongoDB 服务端口号。

3. 定义函数

color () {
    # ... 省略部分内容 ...
}
os_type () {
    # ... 省略部分内容 ...
}
check () {
    # ... 省略部分内容 ...
}
file_prepare () {
    # ... 省略部分内容 ...
}
install_mongodb () {
    # ... 省略部分内容 ...
}

这里定义了一些函数,用于实现脚本中不同的功能模块,比如输出彩色消息、检测操作系统类型、检查环境、准备安装文件以及安装 MongoDB 等。

4. 调用函数和执行过程

check
file_prepare
install_mongodb

最后,脚本通过调用定义的函数来实现 MongoDB 的安装过程,先检查环境,然后准备安装文件,最后执行 MongoDB 的安装操作。

这个脚本实现了在 Linux 系统上安装 MongoDB 的自动化过程,并通过定义函数来模块化不同的功能,使得代码结构清晰、易于维护。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向日葵般灿烂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值