Home » MongoDB » How to install and Configure MongoDB for Windows

How to install and Configure MongoDB for Windows

In this tutorial we will learn how to setup MongoDB in our local machines. We will be covering steps to install MongoDB and related tools on Windows operating system.

Install MongoDB on Windows:

Installing MongoDB on a Windows machine is very easy. Below are the steps involved in installing the MongoDB:

Step 1: Go to MongoDB Community Download Page and download MongoDB executable .msi file from the MongoDB Community Server tab. Select the latest version for Windows, 4.2.6 is the latest version if you are on Windows 7, 8 or 10.Once download completes double click on the file to run the installer.

MongoDb Installation


Step 2: Click Next when the MongoDB installation windows pops up.

MongoDb Installation


Step 3: Accept the MongoDB user Agreement and click Next.

MongoDb Installation


Step 4: Choose the Setup type, choose Complete.

MongoDb Installation


Step 5: Click Install to begin the installation.

MongoDb Installation


Step 6: Select "Run service as Network Service user". Make a note of the data directory, we’ll need this later.

MongoDb Installation


  • Service Name. Specify the service name. Default name is MongoDB. If you already have a service with the specified name, you must choose another name.
  • Data Directory. Specify the data directory, which corresponds to the --dbpath. If the directory does not exist, the installer will create the directory and sets the directory access to the service user.
  • Log Directory. Specify the Log directory, which corresponds to the --logpath. If the directory does not exist, the installer will create the directory and sets the directory access to the service user.

Step 7: Click on the Install button to start the installation.

MongoDb Installation


Step 8: Click on the Finish button to complete the installation.

MongoDb Installation


Start Using MongoDB

If You Installed MongoDB as a Windows Service the MongoDB service is started upon successful installation. To begin using MongoDB, connect a mongo.exe shell to the running MongoDB instance. Either:

  • From Windows Explorer/File Explorer, go to C:\Program Files\MongoDB\Server\4.2\bin\ directory and double-click on mongo.exe.
  • Or, open a Command Interpreter with Administrative privileges and run:
"C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"						

Run MongoDB Community Edition from the Command Line

You can run MongoDB Community Edition from the Windows command prompt/interpreter (cmd.exe) instead of as a service. Open a Windows command prompt/interpreter (cmd.exe) as an Administrator.

1) Create database directory

Create the data directory where MongoDB stores data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB.

From the Command Prompt, create the data directories by using below command:

cd C:\
md "\data\db"

2) Start your MongoDB database

To start MongoDB, run mongod.exe.

"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"

The --dbpath option points to your database directory.


3) Connect to MongoDB.

To connect a mongo.exe shell to the MongoDB instance, open another Command line with Administrative privileges and run:

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

MongoDb Installation


Another way to start MongoDB server and connecting to MongoDB cell

Open another Command line with Administrative privileges and run "net start MongoDB".

net start MongoDB

You should see a message "MongoDB service was started successfully" as below.

MongoDb Installation


For accessing MongoDB cell to execute the command, just navigate to bin directory of your MongoDB installation and type mongo command.

mongo

MongoDb Installation


That’s it everything is done to use MongoDB. Now you can use MongoDB shell and you can run the command to perform various DB operations.

In the next tutorials we will learn how to work in the MongoDB shell.

If you want to exit the shell, you can do that by typing quit() or use Ctrl-C and then you can stop the MongoDB service with this command:

net stop MongoDB
Previous Next Article
comments powered by Disqus