How to install MongoDB on Windows as a Service



When we working on MongoDB , We use console to run MongoDB. But some time we need MongoDB run as a services. In this tutorial I am giving details to run MongoDB as a Windows Services.

These are steps to run MongoDB as a Services
  1. Download MongoDB
  2. Create/Move MongoDB Folder
  3. Check MongoDB Folder
  4. Create Data Folder for MongoDB
  5. Create MongoDB Log File
  6. Configure MongoDB Configuration File " mongod.cfg"
  7. Install MongoDB as a Service
  8. Start MongoDB Service
  9. Check MongoDB on Services Window
  10. Connect to MongoDB
  11. Stop MongoDB Service
  12. Delete MongoDB Service
Note :- If you got "Access is denied" to run any command , You must run command line as "Administrator"
 

1. Download MongoDB

First of all you need to download MongoDB from MongoDB website.

https://www.mongodb.org/downloads

MongoDB Support on

Windows
Linux
Mac OS X
Solaris

For windows we have two options download MSI file or compressed (tgz/zip) file.




2. Create/Move MongoDB Folder

MSI File  - If you downloaded .msi file then MongoDB will be install MongoDB into Program Files Or Program Files (x86) depend upon 32 bits or 64 bits.

After then you need to move MongoDB folder into c:\ drive (Note - I am using C:\ drive you can use any drive ). MongoDB not depend on any other files so it will not create any problem to move .

tgz/zip File - If you download compressed file then create MongoDB folder into c:\ and extract files into MongoDB folder.


3. Check MongoDB Folder

MongoDB's bin Folder contains many executable files. Like mongo , mongoD etc. Every executable have different task.


4. Create Data Folder for MongoDB

MongoDB need (Data Directory) For run MongoDB . For that we need to create Data folder where database files create by MongoDB. I am using "data" folder inside bin folder for Data Directory.




5. Create MongoDB Log File

For run MongoDB as a Services we need first create log file. Because MongoDB services require log file to run MongoDB as a services. In log file we can check log like service started time , services stop time , problem on MongoDB server etc. It's very useful for troubleshoot problem.

For create "mongod.log" file inside log folder in bin folder
"bin\log\mongod.log" then create " mongod.log"  file inside log folder



6. Create MongoDB configuration file

Create configuration file "mongo.config" inside bin folder its plain text file. mongo.config file contains path of Data Directory and Log File. The reason is that MongoDB Services need Data Directory and Log file to read/write. mongo.config only contains absolute path.

mongo.config sample

systemLog:
destination: file
path: C:\MongoDB\Server\3.0\bin\log\mongod.log
storage:
dbPath: C:\MongoDB\Server\3.0\bin\data


7. Install MongoDB as Windows Service

We configure everything need to run MongoDB as a Windows Services. Now create a Window services by using sc.exe.

Run following command

C:\>sc.exe create MongoDB binPath="C:\MongoDB\Server\3.0\bin\mongod --service --
config=\"C:\MongoDB\Server\3.0\bin\mongod.cfg\"" DisplayName="MongoDB" start="au
to"

Note - 

binPath - Path of executable file of mongod (mongod.exe).

config= Path of mongod.cfg file.

DisplayName -  Name which need to show on services window.

start- Its startup Type . You can set it demand/ auto/ delayed-auto etc.


8. Start MongoDB Service

Now start MongoDB Services. Use following command on console to start MongoDB Service.

net start MongoDB
Or
sc start MongoDB


9. Check MongoDB on Services Window



10.  Connect to MongoDB


10. Stop MongoDB Service

net stop MongoDB
Or
sc stop MongoDB



11. Delete MongoDB Service

sc delete MongoDB

Need a help , connect with us on skype



Comments