I'm speaking in the perspective of Windows development, but the concepts are similar with other Operating Systems, such as Linux.
What are services?
Services are application types that run in the system's background. These are applications such as task schedulers and event loggers. If you look at the Task Manager > Processes, you can see that you have a series of Service Hosts which are containers hosting your Windows Services.
What difference does setting MongoDB as a service make?
Running MongoDB as a service gives you some flexibility with how you can run and deploy MongoDB. For example, you can have MongoDB run at startup and restart on failures. If you don't set MongoDB up as a service, you will have to run the MongoDB server every time.
So, what is the difference between a network service and a local service?
Running MongoDB as a network service means that your service will have permission to access the network with the same credentials as the computer you are using. Running MongoDB locally will run the service without network connectivity.(Refer Source here)
Answer from Astolfo on Stack OverflowVideos
What is MongoDB?
Why is MongoDB popular?
Why do I have to pay for Managed MongoDB on Public Cloud when using a service plan with the MongoDB Community licence?
After trying for several hours, I finally did it.
Make sure:
- you added the
<MONGODB_PATH>\bindirectory to the system variablePATH - run command prompt as administrator
Steps:
step 1: execute this command:
D:\mongodb\bin>mongod --remove
Step 2: execute this command after opening command prompt as administrator:
D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install
NOTE: you can also append --serviceName MongoDB after the command above.
That's All!
After that right there in the command prompt execute:
services.msc
// OR
net start MongoDB
And look for MongoDB service and click start.
NOTE: Make sure to run command prompt as administrator.
If you don't do this, your log file (D:\mongodb\log.txt in the above example) will contain lines like these:
2016-11-11T15:24:54.618-0800 I CONTROL [main] Trying to install Windows service 'MongoDB'
2016-11-11T15:24:54.618-0800 I CONTROL [main] Error connecting to the Service Control Manager: Access is denied. (5)
and if you try to start the service from a non-admin console, (i.e. net start MongoDB or Start-Service MongoDB in PowerShell), you'll get a response like this:
System error 5 has occurred.
Access is denied.
or this:
Start-Service : Service 'MongoDB (MongoDB)' cannot be started due to the following error: Cannot open MongoDB service
on computer '.'.
At line:1 char:1
+ Start-Service MongoDB
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman
I think if you run it with the --install command line switch, it installs it as a Windows Service.
mongod --install
It might be worth reading this thread first though. There seems to be some problems with relative/absolute paths when the relevant registry key gets written.
The following works fine for me
mongod --install --serviceName "Mongo DB instance" --serviceDisplayName "Mongo DB instance" --port 27017 --dbpath C:\data --logpath C:\data\1.log --smallfiles
Of course you should change the parameters to fit your needs. Make sure that the appropriate folders exist. After installing the service you start it as follows
net start "Mongo DB instance"
1) Download the 64 bit version of mongodb
2) Double click the downloaded file to run the installer
3) Look for MongoDB in C drive If you can't find it there, then look in C:\"Program Files" folder Cut and paste the "MongoDB 2.6 Standard" folder from C:\Program Files to C:\ Rename "MongoDB 2.6 Standard" to MongoDB
4) Create C:\data folder
5) Now open command shell to run as Administrator
6) In the command shell you just opened:
A) Browse to C:\MongoDB\bin folder
B) Then run this command: (Change name and folder accordingly) mongod --install --serviceName "Mongo DB2.6 instance" --serviceDisplayName "Mongo DB2.6 instance" --port 27017 --dbpath C:\data --logpath C:\data\1.log --smallfiles
C) To start the service, you can either run this command: net start "Mongo DB2.6 instance" OR type services in the Search for programs and files box. Then look for Mongo DB2.6 instance > right click it and choose Start
D) Verify that you can connect and run command Open a command prompt
>cd C:\MongoDB\bin (enter)
>mongo.exe (enter)
>it will show that it is connected
>type command "show dbs" and hit enter
>it will show local and test dbs