Install MongoDB on Windows

  • Consider specific MongoDB build type which you need actually: Three build types of MongoDB for Windows are available.
  • MongoDB for Windows Server 2008 R2 series: This build type runs only on 2008R2, Windows 7 64-bit, Windows 8, and latest versions of Windows.
  • MongoDB for Windows 64-bit: This build type of MongoDB runs on any 64-bit version of Windows latest than Windows XP, involve Windows Server 2008 R2 and Windows 7 64-bit.
  • MongoDB for Windows 32-bit: MongoDB runs on any 32-bit version of Windows latest than Windows XP, 32-bit version of MongoDB are only designed for older system and use for testing and developing systems. Windows 32-bit versions of MongoDB support only database smaller than 2GB.If you want to know that which specific version of windows you are running at your system, then simply use following command on command prompt. Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:\Users\abc>wmic os get osarchitecture OSArchitecture 64-bit C:\Users\abc>
  • Download MongoDB for Windows
  • Download the latest version release from the MongoDB Download page. Make sure you download the latest version of MongoDB for your Windows. Note that 64-bit versions of MongoDB do not run with 32-bit Windows.
  • MongoDB Installation OptionsIn Windows Explorer, simply locate downloaded MongoDB msi file, then Double-click the msi file. A series of screens will guide you through the installation procedure. There are multiple options such as Typical, Custom and Complete installation. MongoDB default installation goes into Program Files, so if you want to change it then go for Custom installation.
  • MongoDB installation folder move to another location (Optional)Sometimes we want to move the installation to another directory, we can use move command for this. This command needs to be run as administrator. For example, to move the folder to C:\MongoDB follow the steps shown in below images.
MongoDB install windows
  • MongoDB does not depend at any other system because it is self contained. You can execute MongoDB from the folder of your choice and can install it in any folder (for, example D:\test\mongodb)

Running MongoDB Server

We need to create a directory where MongoDB server will store all it’s data. The MongoDB default data directory path is \data\db. Make this folder using the following commands from Command Prompt. Note that if the directory doesn’t exists, MongoDB will try to create it. But it’s good to create it before hand.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\abc>cd \
C:\>md \data\db
C:\>

To start MongoDB server, we need to run mongod.exe. For example, from the Command Prompt;

C:\>C:\MongoDB\bin\mongod.exe

This MongoDB starts the main databases process. The waiting for connection message in the console results determines that the mongod.exe process is complete.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\abc>cd \
C:\>MongoDB\bin\mongod.exe
MongoDB\bin\mongod.exe --help for help and startup options
2014-11-11T13:36:19.016+0530 [initandlisten] MongoDB starting : pid=5292 port=27
017 dbpath=\data\db\ 64-bit host=Meghna
2014-11-11T13:36:19.017+0530 [initandlisten] targetMinOS: Windows 7/Windows Serv
er 2008 R2
2014-11-11T13:36:19.017+0530 [initandlisten] db version v2.6.5
2014-11-11T13:36:19.017+0530 [initandlisten] git version: e99d4fcb4279c0279796f2
37aa92fe3b64560bf6
2014-11-11T13:36:19.017+0530 [initandlisten] build info: windows sys.getwindowsv
ersion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
BOOST_LIB_VERSION=1_49
2014-11-11T13:36:19.017+0530 [initandlisten] allocator: system
2014-11-11T13:36:19.017+0530 [initandlisten] options: {}
2014-11-11T13:36:19.092+0530 [initandlisten] journal dir=\data\db\journal
2014-11-11T13:36:19.094+0530 [initandlisten] recover : no journal files present,
 no recovery needed
2014-11-11T13:36:19.186+0530 [initandlisten] allocating new ns file \data\db\loc
al.ns, filling with zeroes...
2014-11-11T13:36:20.002+0530 [FileAllocator] allocating new datafile \data\db\lo
cal.0, filling with zeroes...
2014-11-11T13:36:20.003+0530 [FileAllocator] creating directory \data\db\_tmp
2014-11-11T13:36:20.031+0530 [FileAllocator] done allocating datafile \data\db\l
ocal.0, size: 64MB,  took 0.018 secs
2014-11-11T13:36:20.035+0530 [initandlisten] build index on: local.startup_log p
roperties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-11-11T13:36:20.036+0530 [initandlisten]     added index to empty collection

2014-11-11T13:36:20.037+0530 [initandlisten] command local.$cmd command: create
{ create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0
 numYields:0  reslen:37 851ms
2014-11-11T13:36:20.038+0530 [initandlisten] waiting for connections on port 270
17

You can use a different path for data files using the –dbpath option to mongod.exe, for example:

C:\>C:\MongoDB\bin\mongod.exe --dbpath D:\test\mongodb\data

If your path contains white spaces, then you need to use double quotes as shown below.

C:\>C:\MongoDB\bin\mongod.exe --dbpath "D:\test\mongo db data"

The security level depends on your system configurations, Windows provide pop up a Security Alert dialog box about blocking few features of C:\Folder Name\MongoDB\bin\mongod.exe from communication on networks.

All the clients should choose Private Networks. For example, my home or work network and click Allow access. For more information on security and MongoDB, please look into the Security Documentation.

Connecting to MongoDB server through Command Prompt

We can connect to MongoDB server through mongo.exe client application, just open another command prompt and execute it.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\abc>C:\MongoDB\bin\mongo.exe
MongoDB shell version: 2.6.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.org/
Questions? Try the support group
        https://groups.google.com/group/mongodb-user
>

That’s it, once connected we can issue any MongoDB shell commands. To close the client connection, just fire “exit” command or simply close the command prompt.

MongoDB as Windows Service

The problem with above installation is that we need to start MongoDB server manually and then keep the command prompt open. The solution is to configure it as Windows service, so that it can start as soon as system is up and running. Since it will be running in background, there is no need to keep command prompt open.

Before we configure our MongoDB installation as Windows service, let’s create a configuration file for the MongoDB logs.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\abc>cd \
C:\>md MongoDB-logs

Now using echo command we can create a configuration file. If you get any permission issues, then run the command prompt as administrator.

C:\WINDOWS\system32>echo logpath=C:\MongoDB-logs\mongo.log > C:\MongoDB\mongod.conf

Now run below command as administrator to setup MongoDB as service.

C:\>C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.conf --install
C:\>

If you want to change MongoDB server port or any other configurations, then you can provide them in the above configuration file.

MongoDB Windows Service

If you want to install services for multiple example of mongod.exe, install every service with a unique –serviceName and –serviceDisplayName. Use the multiple examples only when sufficient system resources exist and your design requires it.

Uninstall MongoDB Windows

If you want to remove the MongoDB service, then run below command as administrator.

C:\>C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.conf --remove
C:\>

That’s all for installing MongoDB on Windows and running it as Windows service. We will look into different MongoDB commands in coming posts.