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
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.
MongoDB Support on
Mac OS X
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
“binlogmongod.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.
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:MongoDBServer3.0binmongod –service —
config=”C:MongoDBServer3.0binmongod.cfg”” DisplayName=”MongoDB” start=”au
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
sc start MongoDB
10. Connect to MongoDB
10. Stop MongoDB Service
net stop MongoDB
sc stop MongoDB
sc delete MongoDB