How to Install MongoDB on Windows, Start, Uninstall With Examples

Today we will learn how to install MongoDB on Windows. Most of us use Windows for our personal use. That’s why I am writing this tutorial to help you install MongoDB on Windows. We will also learn how to start and uninstall MongoDB on Windows.

MongoDB for Windows

Starting from MongoDB version 2.2, MongoDB doesn’t support Windows XP. So if you are on Windows XP, either upgrade it or use the lower versions of MongoDB. This tutorial is intended to help you in installing MongoDB on Windows latest versions i.e Windows 7 and Windows 8, 8.1 etc.

Install MongoDB on Windows

Follow below steps to perform the installation of MongoDB into your windows system.

  1. Consider specific MongoDB build type which you need actuallyThree build types of MongoDB for Windows are available.
    1. 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.
    2. 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.
    3. 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:UsersPankaj>wmic os get osarchitecture
    OSArchitecture
    64-bit
    C:UsersPankaj>
    
  2. Download MongoDB for Windows
  3. MongoDB-Downloads-Options-450x273
  4. 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.
  5. 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.
  6. 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.
  7. Command-Prompt-As-Administrator
  8.  

     

    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:testmongodb)

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 datadb. 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:UsersPankaj>cd
C:>md datadb
C:>

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


C:>C:MongoDBbinmongod.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:UsersPankaj>cd
C:>MongoDBbinmongod.exe
MongoDBbinmongod.exe --help for help and startup options
2014-11-11T13:36:19.016+0530 [initandlisten] MongoDB starting : pid=5292 port=27
017 dbpath=datadb 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=datadbjournal
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 datadbloc
al.ns, filling with zeroes...
2014-11-11T13:36:20.002+0530 [FileAllocator] allocating new datafile datadblo
cal.0, filling with zeroes...
2014-11-11T13:36:20.003+0530 [FileAllocator] creating directory datadb_tmp
2014-11-11T13:36:20.031+0530 [FileAllocator] done allocating datafile datadbl
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:MongoDBbinmongod.exe --dbpath D:testmongodbdata

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


C:>C:MongoDBbinmongod.exe --dbpath "D:testmongo 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 NameMongoDBbinmongod.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:UsersPankaj>C:MongoDBbinmongo.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:UsersPankaj>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:WINDOWSsystem32>echo logpath=C:MongoDB-logsmongo.log > C:MongoDBmongod.conf

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


C:>C:MongoDBbinmongod.exe --config C:MongoDBmongod.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-450x199

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:MongoDBbinmongod.exe --config C:MongoDBmongod.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.

By admin

Leave a Reply

%d bloggers like this: