Install, setup and start MongoDB on Windows
This post will provide the full path from downloading required binary archive/package for particular Windows version to starting up MongoDB in various ways.
Following are the high level steps:
- Download the MongoDB binary archive
- Extract MongoDB archive
- Setup up configuration parameters and start/stop MongoDB
- using command line
- using windows services
Download the MongoDB binary archive
For Windows platform, MongoDB distributes zip archive. Go to following downloads page from browser http://www.mongodb.org/downloads. Depends on system architecture, it comes in two distribution as:
- 32-bit
- 64-bit
Again, MongoDB distribution for Windows 64-bit ships with two flavours:
- one for Windows server 2008 and Windows 7, Server 2012 (download link “*2008R2+” )
- other for rest of 64-bit Windows OS.
This distinction for x64 is made based on newer OS features which helps in enhanced performance of MongoDB.
Choose the production releases for downloading
After you download, you will get zip archive named like mongodb-<platform>-<architecture>-<version>.zip
Extract MongoDB archive
Once we have MongoDB archive, go ahead and extract archive using any zip extract program. After extracting, you will get the directories inside archive as follows:
here , bin directory contains the binaries in form of executables , such as mongod.exe, mongo.exe, monogexport.exe etc.
Setup up configuration parameters and start/stop MongoDB
For starting and stopping the mongodb server, we need only the bin\mongod.exe, which is the daemon process executable for MongoDB. In short, it is the executable which drives up the MongoDB in general For starting up, we need to provide the parameters for executable, which i call it here as config parameters or params. We can setup the config parameters using the two ways
- Using command line options or
- Using config file
Using command line options
With use of these command line options, we configure mongo daemon process. Basically, there lots of options we can specify but i will give only those which required for this tutorial. Following are some of them:
–dbpath <path> : the existent directory path, which is required to store data files. this is most important option we need to specify, Note that, the directory path you are providing should exists otherwise process won’t start. If this path contains spaces then put all its path in double qoutes. e.g. –dbpath “c:\Program Files”
–logpath <log-file-path>: the existent file path, used by mongo daemon process,for flush out loggs instead of in standard console. If this path contains spaces then put all its path in double qoutes
–port <port> : port no. where mongod process listen for connection from client, it defaults to 27017 if not specified
Note : While using the command prompt on some Windows OS like windows 7 or Windows server 2008, Run it with administrator privileges as shown as follows
Use the following commands to start the server process
Change to bin directory
> I:\>cd Servers\mongodb\bin
now type following command to start the mongod process
> mongod --dbpath I:\Servers\data --port 27017
While starting, Windows firewall may block the process as shown as follows
Click “Allow access“ to proceed. After successful execution of command , it will show logging info in standard console itself as shown follows:
> I:\Servers\mongodb\bin>mongod --dbpath I:\Servers\data --port 27017 Tue Apr 09 22:49:13 [initandlisten] MongoDB starting : pid=4380 port=27017 dbpath=I:\Servers\data 64-bit host=Myi-PC Tue Apr 09 22:49:13 [initandlisten] db version v2.2.1, pdfile version 4.5 Tue Apr 09 22:49:13 [initandlisten] git version: d6764bf8dfe0685521b8bc7b98fd1fab8cfeb5ae Tue Apr 09 22:49:13 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49 Tue Apr 09 22:49:13 [initandlisten] options: { dbpath: "I:\Servers\data", port: 27017 } Tue Apr 09 22:49:13 [initandlisten] journal dir=I:/Servers/data/journal Tue Apr 09 22:49:13 [initandlisten] recover : no journal files present, no recovery needed Tue Apr 09 22:49:13 [initandlisten] waiting for connections on port 27017 Tue Apr 09 22:49:13 [websvr] admin web console waiting for connections on port 28017</em>
If you specify the logpath option, then logging will direct to that log file instead of showing up on standard console
> mongod --dbpath I:\Servers\data --port 27017 --logpath I:\Servers\logs\mongod.log all output going to: I:\Servers\logs\mongod.log
and prompt will wait there and you can find all the logs at specified log file location. You can stop this process with use of keys Ctrl+C or Ctrl +D from keyboard.
Using the config file
Instead of specifying command line option, we can specify same with use of file, which i call it here as config file. Config file is just normal file, containing the parameters in the key=value form and each is on the every line of file. In this, we basically provide path to file (which contains the configurations) as command line option as “-f” or “–config”. Following is the snippet for the config file:
#This is an example config file for MongoDB #basic dbpath = I:\Servers\mongodb\data port = 27017 logpath = I:\Servers\mongodb\logs\mongo.log
You can save this file with any extension, but specify full path with extension, while stating process as shown in following commands. From command prompt, you will use either of following:
> mongod -f I:\Servers\mongodb\config\mongodb.conf
or
> mongod --config I:\Servers\mongodb\config\mongodb.conf
Start/Stop MongoDB using the Windows services
Support for installing mongod server as service comes out of the box. Mongodb daemon executable provides amazing support for the installation of services using few command line parameters without using additional components for this purpose Just we need set the few command line params and we are way to go and they are as follows. Following are required parameters:
–install : command line switch to install the service
–remove : command line switch to remove the service
–serviceName <name> : the name for mongod windows service and must adhere to naming services in windows like only accepting the alphanumeric chars with no spaces
–serviceDisplayName <display-name> : Display name for service that is shown in services console, put this in double quotes if contains the spaces
–serviceDescription <description> : Small description about service, put this in double quotes if contains the spaces
While installing as service we must provide log file path as counterpart to starting it from command line, because while starting service we don’t as standard console.I will be using the config file for some configurations
> mongod -f "I:\Servers\mongodb\config\mongodb.conf" --install --serviceName mdb27017 --serviceDisplayName "MongoDB Server Instance 27017" --serviceDescription "MongoDB Server Instance running on 27017"
In log path specified, you can check for the whether windows services started or not. Above will install the mongodb as Windows service, check Services console using services.msc
Now we can start or stop MongoDB using Windows services console as shown above. You can remove the service using following:
> mongod -f "I:\Servers\mongodb\config\mongodb.conf" --remove--serviceName mdb27017 --serviceDisplayName "MongoDB Server Instance 27017" --serviceDescription "MongoDB Server Instance running on 27017"