Setup MongoDB
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
- Open a new terminal session.
- 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.
- 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.
- MongoDB Installation — Paste the following command into your terminal to install MongoDB to your computer.
brew install mongodb
- 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.
- 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.
- 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
- Download — Open the MongoDB download center page, and download the latest version of MongoDB for your windows machine.
- Install — Open the installation file that you downloaded. Follow the steps to install MongoDB on your computer.
- 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
- 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)
- 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.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours