Prerequisites
-
Install “Command Line Tools for Xcode” (often required for compilation and homebrew)
-
Install or have access to Xcode (optional, but sometimes required when building from source)
You can obtain the command‑line tools from Apple’s developer site (e.g. https://developer.apple.com).
#!/bin/bash
# --------------------------------------------------------------
# MongoDB Full Automation Setup Script for macOS
# Author: Raj
# --------------------------------------------------------------
MONGO_DATA_DIR="/usr/local/var/mongodb"
MONGO_LOG="/usr/local/var/log/mongodb/mongo.log"
BREW_SERVICES=$(brew --prefix)/bin/brew
BIND_IP="127.0.0.1"
MONGO_PORT=27017
PROFILE_FILE="$HOME/.zprofile"
echo " Starting MongoDB installation and setup for macOS..."
#--------------------------------------------------
# 1. Pre-checks: Xcode Tools, Homebrew
#--------------------------------------------------
echo "🔧 Checking for Command Line Tools..."
xcode-select -p &>/dev/null
if [ $? -ne 0 ]; then
echo "Command Line Tools not installed. Installing..."
xcode-select --install
exit 1
else
echo "Command Line Tools are installed."
fi
echo "🔧 Checking Homebrew..."
if ! command -v brew &>/dev/null; then
echo " Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
echo " Homebrew found."
fi
brew update
#--------------------------------------------------
# 2. Install MongoDB via Homebrew
#--------------------------------------------------
echo " Installing MongoDB Community Edition..."
brew tap mongodb/brew
brew install mongodb-community@7.0
if [ $? -ne 0 ]; then
echo " MongoDB installation failed."
exit 1
fi
echo " MongoDB installed."
#--------------------------------------------------
# 3. Create MongoDB Data Directory and Set Permissions
#--------------------------------------------------
echo " Ensuring MongoDB data and log directories exist..."
sudo mkdir -p "$MONGO_DATA_DIR"
sudo mkdir -p "$(dirname $MONGO_LOG)"
sudo touch "$MONGO_LOG"
sudo chown -R $(whoami) "$MONGO_DATA_DIR" "$(dirname $MONGO_LOG)"
echo " Data and log directories created and owned by user."
#--------------------------------------------------
# 4. Check/Update Kernel Settings (ulimits/sysctl)
#--------------------------------------------------
echo " Checking kernel limits..."
ULIMIT_NOFILE=$(ulimit -n)
if [ "$ULIMIT_NOFILE" -lt 64000 ]; then
echo " Open file limit is low ($ULIMIT_NOFILE). Recommend setting to 64000."
echo "You can add this to $PROFILE_FILE:"
echo 'ulimit -n 64000'
else
echo " Open file limit is sufficient: $ULIMIT_NOFILE"
fi
#--------------------------------------------------
# 5. Start MongoDB Service (brew services or launchctl)
#--------------------------------------------------
echo " Starting MongoDB service..."
brew services start mongodb-community@7.0
sleep 5
#--------------------------------------------------
# 6. Post-Checks: Port, Service Status, MongoDB Ping
#--------------------------------------------------
echo " Checking if MongoDB is running on port $MONGO_PORT..."
if lsof -iTCP:$MONGO_PORT -sTCP:LISTEN | grep -q mongod; then
echo " mongod is listening on port $MONGO_PORT."
else
echo " mongod not detected on port $MONGO_PORT."
exit 1
fi
echo " Verifying MongoDB shell connectivity..."
if mongosh --eval "db.runCommand({ ping: 1 })" &>/dev/null; then
echo " Successfully connected to MongoDB."
else
echo " Unable to connect to MongoDB using mongosh."
fi
#--------------------------------------------------
# 7. Monitor Log File
#--------------------------------------------------
echo " Last 20 lines of MongoDB log:"
tail -n 20 "$MONGO_LOG"
echo " MongoDB setup and verification complete!"
echo "--------------------------------------------"
echo " Data Directory: $MONGO_DATA_DIR"
echo " Log File: $MONGO_LOG"
echo " Port: $MONGO_PORT"
echo " Bind IP: $BIND_IP"
example:
requirement is: Command Line Tools for Xcode": http://connect.apple.com
go to apple.com and download Xcode.
Last login: Mon Feb 11 23:00:42 on ttys000
machost_01:~ raj$ brew update
-sh: brew: command not found
machost_01:~ raj$ brew
-sh: brew: command not found
machost_01:~ raj$ cd /usr/local/
machost_01:local raj$ find Cellar
find: Cellar: No such file or directory
machost_01:local raj$ gcc -v
-sh: gcc: command not found
machost_01:local raj$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin11.0]
machost_01:local raj$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to admin:
/usr/local/.
Press ENTER to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/.
WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.
To proceed, enter your password, or type Ctrl-C to abort.
Password:
Sorry, try again.
Password:
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/.
==> Downloading and Installing Homebrew...
Failed during: Error: /usr/bin/xcode-select returned unexpected error. init -q
machost_01:local raj$ gcc -v
-sh: gcc: command not found
machost_01:local raj$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press ENTER to continue or any other key to abort
==> Downloading and Installing Homebrew...
remote: Counting objects: 101297, done.
remote: Compressing objects: 100% (43460/43460), done.
remote: Total 101297 (delta 71564), reused 83861 (delta 56957)
Receiving objects: 100% (101297/101297), 15.12 MiB | 2.19 MiB/s, done.
Resolving deltas: 100% (71564/71564), done.
From https://github.com/mxcl/homebrew
* [new branch] master -> origin/master
Checking out files: 100% (2612/2612), done.
HEAD is now at 52b8be1 doctor: make brew link more clear
Warning: Install the "Command Line Tools for Xcode": http://connect.apple.com
==> Installation successful!
You should run `brew doctor' *before* you install anything.
Now type: brew help
machost_01:local raj$ brew doctor
Warning: Experimental support for using Xcode without the "Command Line Tools".
You have only installed Xcode. If stuff is not building, try installing the
"Command Line Tools for Xcode" package provided by Apple.
machost_01:local raj$
machost_01:local raj$ brew doctor
Warning: Experimental support for using Xcode without the "Command Line Tools".
You have only installed Xcode. If stuff is not building, try installing the
"Command Line Tools for Xcode" package provided by Apple.
machost_01:local raj$
machost_01:local raj$
machost_01:local raj$
machost_01:local raj$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup
machost_01:local raj$
machost_01:local raj$ brew update
Already up-to-date.
machost_01:local raj$
machost_01:local raj$
machost_01:local raj$ pwd
/usr/local
machost_01:local raj$ brew install mongo
==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.2.3.tgz
######################################################################## 100.0%
==> Caveats
To have launchd start mongodb at login:
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
mongod
==> Summary
🍺 /usr/local/Cellar/mongodb/2.2.3-x86_64: 20 files, 171M, built in 45 seconds