🌑

Stephen Cheng

How to Install and Run MongoDB on Mac OS

 

Stephen Cheng

Intro

MongoDB is a document database which belongs to a family of databases called NoSQL - not only SQL. In MongoDB, records are documents which behave a lot like JSON objects in JavaScript. Values in documents can be looked up by their field’s key. Documents can have some fields/keys and not others, which makes Mongo extremely flexible. This is much different than SQL databases like MySQL, where fields correspond to columns in a table and individual records correspond to rows.

Install and Run MongoDB with Homebrew

  • Make sure Homebrew already installed on Mac (Homebrew is a package manager for the Mac – it makes installing most open source software).

  • Open the Mac terminal application and type brew update, then type brew install mongodb

  • After MongoDB is downloaded, create a directory to store MongoDB data files, type mkdir -p /data/db

  • Make sure the directory has the right permissions:

1
2
> sudo chown -R `id -un` /data/db
> # Enter your psd
  • Run the Mongo daemon by typing mongod in your terminal window.

  • Run the Mongo shell by typing mongo in another terminal window.

  • To exit the Mongo shell run quit(), to stop the Mongo daemon hit ctrl+c


Install and Run MongoDB by Downloading it Manually

  • 1) Go to the MongoDB website and download the correct version of MongoDB.

  • 2) After downloading MongoDB, move the gzipped tar file to the folder where you want MongoDB installed by typing commands like these in your terminal:

1
2
> cd Downloads
> mv mongodb-macos-x86_64-4.2.2.tgz ~/
  • 3) Extract MongoDB from the downloaded archive and change the name of the directory to something more concise by following commands:
1
2
> cd ~/ > tar -zxvf mongodb-macos-x86_64-4.2.2.tgz
> mv mongodb-macos-x86_64-4.2.2 mongodb
  • 4) Create a directory to store MongoDB data files, type mkdir -p /data/db

  • 5) Make sure the directory has the right permissions:

1
2
> sudo chown -R `id -un` /data/db
> # Enter your psd
  • 6) If you can’t create /data/db directory, you can also use other directory to replace it by running command like this:
1
~/mongodb/bin/mongod --dbpath ~/mongodb/data

then jump to step 8.

  • 7) Run the Mongo daemon by typing ~/mongodb/bin/mongod in your terminal window.

  • 8) Run the Mongo shell by typing ~/mongodb/bin/mongo in another terminal window.

  • 9) To exit the Mongo shell run quit(), to stop the Mongo daemon hit ctrl+c

— Mar 11, 2019

Search

    Made with ❤️ and ☀️ on Earth.