NoSQL Zone is brought to you in partnership with:

Abhijeet is a DZone MVB and is not an employee of DZone and has posted 4 posts at DZone. You can read more from them at their website. View Full User Profile

Install, Set Up and Start MongoDB on Windows

  • submit to reddit

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

shell > 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')
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

shell > 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 text file, containing the parameters in the key=value form and each one is specified 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
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

shell > mongod -f  I:\Servers\mongodb\config\mongodb.conf
shell > mongod --config I:\Servers\mongodb\config\mongodb.conf
Start/Stop MongoDB using the Windows services

Support for installing MongoDB as Windows 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.

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

// note that following should go only in only one line
shell> 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.

For removing installed service use following

// note that following should go only in only one line
shell > mongod -f "I:\Servers\mongodb\config\mongodb.conf"
 --remove --serviceName mdb27017
 --serviceDisplayName "MongoDB Server Instance 27017"
 --serviceDescription "MongoDB Server Instance running on 27017"
Published at DZone with permission of Abhijeet Sutar, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)