Databases 4 min read

Bash Script for Installing MongoDB on Linux

This article provides a complete Bash script that automates downloading, extracting, configuring, and starting MongoDB on Linux systems, handling OS detection, prerequisite installation, file preparation, and color‑coded status messages for success, failure, and warnings.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Bash Script for Installing MongoDB on Linux

This script automates the installation of MongoDB on Linux, including OS detection, prerequisite package installation, downloading the MongoDB tarball, extracting it, setting up directories, creating symbolic links, updating the system PATH, and starting the mongod service with appropriate logging and color‑coded status messages.

#!/bin/bash #****************************************** #FileName: install_mongodb.sh #Description: The test script #****************************************** file=mongodb-linux-x86_64-ubuntu1804-4.4.4.tgz url=https://fastdl.mongodb.org/linux/$file install_dir=/usr/local db_dir=/data/db 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 "$1" && $SETCOLOR_NORMAL } os_type () { awk -F '[=]"' '/^NAME/{print $2}' /etc/os-release } check () { [ -e $db_dir -o -e $install_dir/mongodb ] && { color $SETCOLOR_SUCCESS "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 $SETCOLOR_WARNING "不支持当前操作系统"; exit fi } file_prepare () { if [ ! -e $file ]; then curl -O $url || { color $SETCOLOR_FAILURE "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 if [ $? -eq 0 ]; then color $SETCOLOR_SUCCESS "MongoDB 数据库安装成功" || color $SETCOLOR_FAILURE "MongoDB 数据库安装失败" fi } check file_prepare install_mongodb

After execution, the script verifies the installation, prepares the necessary files, installs MongoDB, and starts the server, providing clear success or error messages with colored output for better readability.

LinuxInstallationbashMongoDBscript
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.