sudo port install mongodb
# Make data directory
sudo mkdir -p /opt/local/var/db/mongodb_data
# Make logs directory
sudo mkdir -p /opt/local/var/log/mongodb
# Make config directory
sudo mkdir -p /opt/local/etc/mongodb
sudo vim /opt/local/etc/mongodb/mongod.conf
Enter the following:
# configuration file /opt/local/etc/mongodb/mongod.conf
# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/var/db/mongodb_data
# Only accept local connections
bind_ip = 127.0.0.1
# Running as daemon
fork = true
# Take log
logpath = /opt/local/var/log/mongodb/mongodb.log
logappend = true
Mongo won't start after machine restart by default. Use launchctl to load mongo jobs.
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mongodb.plist
It will alow to run mongostart and mongostop to manually start and stop the mongodb instance.
sudo vim ~/.bash_profile and append following script to the end of the file.
# Custom mongostart scripts starting mongo with configuration file
alias mongostart="sudo mongod -f /opt/local/etc/mongodb/mongod.conf"
# Custom mongostop alias killing mongo process
mongostop_func () {
local mongopid=`less /opt/local/var/db/mongodb_data/mongod.lock`;
if [[ $mongopid =~ [[:digit:]] ]]; then
sudo kill -15 $mongopid;
echo mongod process $mongopid terminated;
else
echo mongo process $mongopid not exist;
fi
}
alias mongostop="mongostop_func"
Lastly execute source ~/.bash_profile from the terminal
Use mongostart from the Terminal to start mongo
Use mongostop to stop it