Sunday, 15 March 2015

How to create a Window Service for MongoDB

Leave a Comment
From Sitecore 7.5 onwards MongoDB will be used as the new collection database for the Sitecore Experience Database (xDB). In the xDB, the collection database acts as a primary storage or central repository for storing contact, device, interaction, history and analytics information. In this blog post I am going to explain how to install MongoDB on Windows and how to create a Window Service for MongoDB that starts MongoDB automatically at boot time.
  1. Download the latest stable production release installer file (.msi) of MongoDB from the MongoDB download section. Ensure you download the correct version of MongoDB (32 Bit or 64 Bit) for your Windows system.
  2. Double click on downloaded .msi installer file. You can specify a different installation directory if you choose the “Custom” installation option. I’ve installed MongoDB 3.0.0 version to C:\mongodb.
  3. You can set up the MongoDB server as a Windows Service that starts automatically at boot time. If you have installed MongoDB in different installation directory, you have to modify the paths as appropriate in next configuration steps.
  4. Open command prompt with administrator privileges. MongoDB requires a data directory to store all data. MongoDB’s default data directory path is \data\db.
  5. Create directories for your database and log files. Execute below commands in command prompt:
    mkdir c:\mongodb\data\db
    mkdir c:\mongodb\data\log
  6. Create a configuration (.cfg) file. This file includes configuration options for mongod but it must include a valid setting for logpath and dbpath. The following command creates a configuration file, specifying both the logpath and the dbpath settings in the configuration file with respect to your installation directory:
    echo logpath=c:\mongodb\data\log\mongod.log> "C:\mongodb\mongod.cfg"
    echo dbpath=c:\mongodb\data\db>> "C:\mongodb\mongod.cfg"
  7. Create the MongoDB service by executing below command.
    sc.exe create MongoDB binPath= "\"C:\mongodb\bin\mongod.exe\" --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
    sc.exe requires a space between “=” and the configuration values (eg “binPath= ”), and a “\” to escape double quotes. The  following log message will display once MongoDB Service is successfully created:
    [SC] CreateService SUCCESS
  8. Start the MongoDB service by typing below command:
    net start MongoDB
    you’ll get below log message once MongoDB service is successfully started.
    The MongoDB service was started successfully.
Comments and suggestions are most welcome. Happy coding!


Post a Comment