Install MongoDB

Deployment Type:

Author: MongoDB Documentation Team

This guide describes how to install MongoDB locally. If you would like to use MongoDB in the Cloud using Atlas, our managed database product, see Create an Atlas Account and Cluster.

Time required: 10 minutes

What You’ll Need

MongoDB supports a variety of 64-bit platforms. Refer to the Supported Platforms table to verify that MongoDB is supported on the platform to which you wish to install it.

Procedure

Install MongoDB

Download the binaries from the MongoDB Download Center.

  1. Open Windows Explorer/File Explorer.

  2. Change the directory path to where you downloaded the MongoDB .msi file. By default, this is %HOMEPATH%\Downloads.

  3. Double-click the .msi file.

  4. The Windows Installer guides you through the installation process.

    If you choose the Custom installation option, you may specify an installation directory.

    MongoDB does not have any other system dependencies. You can install and run MongoDB from any folder you choose.

    Note

    This tutorial assumes that you installed MongoDB in C:\Program Files\MongoDB\Server\4.0\.

Platform Support

MongoDB only supports macOS versions 10.11 and later on Intel x86-64.

1

Download the binary files for the desired release of MongoDB.

Download the binaries from the MongoDB Download Center.

2

Extract the files from the downloaded archive.

For example, from a system shell, you can extract through the tar command:

tar -zxvf mongodb-osx-ssl-x86_64-1.0.tgz
3

Copy the extracted archive to the target directory.

Copy the extracted folder to the location from which MongoDB will run.

4

Ensure the location of the binaries is in the PATH variable.

The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

For example, you can add the following line to your shell’s rc file (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace <mongodb-install-directory> with the path to the extracted MongoDB archive.

Note

These instructions are for installing MongoDB directly from an archive file. If you would rather use your linux distribution’s package manager, refer to the installation instructions for your distribution in the MongoDB Manual.

1

Download the binary files for the desired release of MongoDB.

Download the binaries from the MongoDB Download Center.

2

Extract the files from the downloaded archive.

Extract the archive by double-clicking on the tar file or using the tar command from the command line, as in the following:

tar -xvzf <tgz file>
3

Copy the extracted archive to the target directory.

Copy the extracted folder to the location from which MongoDB will run.

4

Ensure the location of the binaries is in the PATH variable.

The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

For example, you can add the following line to your shell’s rc file (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace <mongodb-install-directory> with the path to the extracted MongoDB archive.

Run MongoDB

Warning

Do not make mongod.exe visible on public networks without running in “Secure Mode” with the auth setting. MongoDB is designed to be run in trusted environments, and the database does not enable “Secure Mode” by default.

1

Set up the MongoDB environment.

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the following command in a Command Prompt:

md \data\db

You can specify an alternate path for data files using the --dbpath option to mongod.exe, for example:

"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --dbpath d:\test\mongodb\data

If your path includes spaces, enclose the entire path in double quotes, for example:

"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --dbpath "d:\test\mongo db data"

You may also specify the dbpath in a configuration file.

2

Start MongoDB.

To start MongoDB, run mongod.exe. For example, from the Command Prompt:

"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.

Depending on the security level of your system, Windows may pop up a Security Alert dialog box about blocking “some features” of C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe from communicating on networks. All users should select Private Networks, such as my home or work network and click Allow access. For additional information on security and MongoDB, please see the Security Documentation.

3

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

[initandlisten] waiting for connections on port 27017

The output should be visible in the terminal or shell window.

You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

4

Connect to MongoDB.

To connect to MongoDB through the ~bin.mongo.exe shell, open another Command Prompt.

"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"
1

Create the data directory

Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the /data/db directory. If you create a directory other than this one, you must specify that directory in the dbpath option when starting the mongod process later in this procedure.

The following example command creates the default /data/db directory:

mkdir -p /data/db
2

Set permissions for the data directory

Before running mongod for the first time, ensure that the user account running mongod has read and write permissions for the directory.

3

Run MongoDB

To run MongoDB, run the mongod process at the system prompt. If necessary, specify the path of the mongod or the data directory. See the following examples.

Run without specifying paths

If your system PATH variable includes the location of the mongod binary and if you use the default data directory (i.e., /data/db), simply enter mongod at the system prompt:

mongod
Specify the path of the mongod

If your PATH does not include the location of the mongod binary, enter the full path to the mongod binary at the system prompt:

<path to binary>/mongod
Specify the path of the data directory

If you do not use the default data directory (i.e., /data/db), specify the path to the data directory using the --dbpath option:

mongod --dbpath <path to data directory>
4

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

[initandlisten] waiting for connections on port 27017

The output should be visible in the terminal or shell window.

You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

Important

If you are using SELinux on a Red Hat Linux-based system, (Red Hat Enterprise Linux or CentOS Linux), you must configure SELinux to allow MongoDB to start. Refer to install-rhel-configure-selinux for instructions.

1

Create the data directory

Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the /data/db directory. If you create a directory other than this one, you must specify that directory in the dbpath option when starting the mongod process later in this procedure.

The following example command creates the default /data/db directory:

mkdir -p /data/db
2

Set permissions for the data directory

Before running mongod for the first time, ensure that the user account running mongod has read and write permissions for the directory.

3

Run MongoDB

To run MongoDB, run the mongod process at the system prompt. If necessary, specify the path of the mongod or the data directory. See the following examples.

Run without specifying paths

If your system PATH variable includes the location of the mongod binary and if you use the default data directory (i.e., /data/db), simply enter mongod at the system prompt:

mongod
Specify the path of the mongod

If your PATH does not include the location of the mongod binary, enter the full path to the mongod binary at the system prompt:

<path to binary>/mongod
Specify the path of the data directory

If you do not use the default data directory (i.e., /data/db), specify the path to the data directory using the --dbpath option:

mongod --dbpath <path to data directory>
4

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

[initandlisten] waiting for connections on port 27017

The output should be visible in the terminal or shell window.

You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

Summary

If you have successfully completed this guide, you have installed MongoDB and are ready to connect to your mongod instance and start inserting data.

What’s Next

The next guide walks you through connecting to your new MongoDB instance.

See Also