Skip to content

Instantly share code, notes, and snippets.

@haccks
Last active February 10, 2024 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haccks/a7c11ba1b9eb3afe7e1f6f3dcd8f8660 to your computer and use it in GitHub Desktop.
Save haccks/a7c11ba1b9eb3afe7e1f6f3dcd8f8660 to your computer and use it in GitHub Desktop.
MongoDB Installation and Usage On MacOS
  1. Install mongodb community edition
  2. Install and configure mongosh
  3. Create a configuration file /etc/mongod.conf (you may need to create it with sudo). Copy the following snippet and replace <username> with your username and paste it:
processManagement:
   fork: true
net:
   bindIp: localhost
   port: 27017
storage:
   dbPath: /Users/<username>/data/db
systemLog:
   destination: file
   path: "/Users/<username>/data/log/mongodb/mongod.log"
   logAppend: true
storage:
   journal:
      enabled: true
  1. Open ~/.bash_profile and append the followin bash snippet
alias mongodstart="mongod --config /etc/mongod.conf"

mongod_stop() {
 local mongopid=`less ~/data/db/mongod.lock`;
 if [[ $mongopid =~ [[:digit:]] ]]; then
     sudo kill -15 $mongopid;
     echo mongod process $mongopid terminated;
 else
     echo mongod process $mongopid not exist;
 fi
}

alias mongodstop="mongod_stop"
  1. Run source ~/.bash_profile
  2. Start and stop mongod server using mongodstart and mongodstop command
  3. Once the deamon started successfully, run mongosh to connect to your local mongodb database (You can use mongo but mongosh is better!*)

*Read mongod, mongo, mongosh, mongos, what now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment