Install MongoDB as a Windows Service

In the previous post we have seen that how to install and configure mongo database server and how to connect to it using interactive javascript client.If you have read my previous post you will notice that while starting the mongo database server using mongod we are passing the location of where our data is going to reside using the –dbpath argument like this.

mongod --dbpath C:sauravmongodatadb

Actually this is not the ideal way of doing this and moreover not an recommended way because every time passing the dbpath location is an extra overhead.So, here comes the config file as a resuce, we are going to define this dbpath property and some other property in the config file such that mongod will read this config file while starting mongo database server.

Below is the list of steps that will explain how to read from a configuration file.

  1. Create a configuration file mongod.cfg in any location of your choice but i will recommend it to keep in the same location where u have extracted the contents of mongo zip file. In my case my config file is present here –
    mongod.exe --config C:sauravmongomongod.cfg
  2. Add the following contents in your cfg file.
    #Location where my data will reside
    dbpath=C:sauravmongodbdata
    
    #Location of my log file
    logpath=C:sauravmongomongod.log
  3. Now start mongo database server and pass this config file as an argument like this –
    mongod.exe --config C:sauravmongomongod.cfg

    Now when you execute this command you will see something like this in your command prompt.

    Install MongoDB as a Windows Service-1
    Install MongoDB as a Windows Service-1

    You will notice that all the logs are redirected to the location that we had defined in cfg file.
    Now you might be wondering that , how does it help instead of passing dbpath location we are passing the config file location , it is the same thing an extra overhead is still their. Answer to this problem is in next step.

  4. Now we will install Mongodb as a windows service , so that whenever our system  reboots it will start mongod as a windows service. As per the mongodb docs you can install it using –install.
    From your command prompt execute this command.

    mongod --config C:sauravmongomongod.cfg --install

    Note : Make sure you open this command prompt instance as an adminstrator otherwise you will get an “Access Denied Exception”.

    Trying to install Windows service 'MongoDB'
    Error connecting to the Service Control Manager: Access is denied. (5)

    Once you execute this command you will see this message in your cmd console.

    Install MongoDB as a Windows Service-2
    Install MongoDB as a Windows Service-2

    You can also see MongoDB service in the task manager.By default it will be in stopped state.After installing mongod as a windows service by executing this command.

    net start MongoDB

    This command will start MongoDB as a windows service ,as you can see from the below snapshot.

    Install MongoDB as a Windows Service-3
    Install MongoDB as a Windows Service-3

    For stopping mongod as a windows service you can execute this command –

    net stop MongoDB

So , now we are all set and configured mongod as a windows service.

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

One thought on “Install MongoDB as a Windows Service

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>