Setup MongoDB

Codecademy Team
Setup MongoDB on your computer.

MongoDB is a database program that you will use to save and access information about users of the websites that you build. By the end of this article, you will be able to run MongoDB on your computer.

We have provided setup instructions for Mac and Windows operating systems. If your computer notifies you of any firewall or permissioning issues, you should Allow the process.

Mac OS X

  1. Open a new terminal session.
  2. Check if Homebrew is already installed on your computer — Homebrew is software that your computer uses to install other software. We will use it in the steps that follow to install MongoDB. Run the following command in your terminal to check if Homebrew is already installed:
    $ brew --version
    
    • If your computer does not have Homebrew, it will respond with `command not found`. If you see this, move to step three.
    • If Homebrew is installed, your terminal will output `Homebrew 1.3.6`. If you see this output, check the version of your operating system. If your operating system is High Sierra, you must uninstall brew, then proceed to step 3 — High Sierra introduced a permissioning issue that impacts old versions of homebrew.
    • If you see `Homebrew 1.x.x` and have not updated to High Sierra, skip to step 4.
  3. Homebrew Installation — Paste the command from the Homebrew installation page into your terminal. The terminal may ask you to enter your password. This step may take a few minutes.
  4. MongoDB Installation — Paste the following command into your terminal to install MongoDB to your computer.
    brew install mongodb
    
  5. Create a Mongo data directory — This directory is where MongoDB stores database files for full-stack web applications that you run on your computer. Paste the following command into your terminal to create the Mongo data directory:
    mkdir -p ~/data/db
    

    You will not see any output in your terminal from the command above. It will create a new folder in your Home Directory.

  6. Start a Mongo process — Before you run a full-stack web application locally, you must start a Mongo process in the background. The following command starts an instance of MongoDB that handles data requests, manages data access, and performs background management operations while you use your web application. If you are prompted with a popup that says, "Do you want the application mongod to accept incoming network connections", click Allow.
    mongod --dbpath ~/data/db
    

    If Mongo starts correctly, you should see something like:

    connection accepted from ... #1 (1 connection now open)
    

    Nice Job! You just started an instance of MongoDB.

  7. How to kill a Mongo process — When you're done using your Mongo process, or think your current process has gotten into a bad state, navigate to the window that is running your Mongo process and follow the steps below:

    Press CTRL+C to exit the Mongo process

Windows

  1. Download — Open the MongoDB download center page, and download the latest version of MongoDB for your windows machine.
  2. Install — Open the installation file that you downloaded. Follow the steps to install MongoDB on your computer.
  3. Create a Mongo data directory — This directory is where MongoDB stores database files for full-stack web applications that you run locally. Open powershell, and enter the following command to create the Mongo data directory:
    md \data\db
    
  4. Start a Mongo process — Before you run a full-stack web application locally, you must start a Mongo process in the background. The following command starts an instance of MongoDB that handles data requests, manages data access, and performs background management operations while you use your web application.
    & 'C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe'
    

    If Mongo starts correctly, you should see something like:

    connection accepted from ... #1 (1 connection now open)
    
  5. How to kill a Mongo process — When you're done using your Mongo process, or think your current process is in a bad state, navigate to the window that is running Mongo and:

    • Press "control" and the letter "C" (CTRL+C) to exit the Mongo process. Press CTRL+C and close your terminal window.