Run MongoDB as a Service in Windows

Today we will learn How to Setup and run MongoDB windows service continuing our MongoDB Development Tutorials series. MongoDB king of NoSQL databases, check out Introduction to MEAN Stack article to know why. In this article, You will learn to Setup, config, start and run MongoDB As a Service in Windows PC to be precise Windows 10 64bit and 32 bit, Windows 7 64bit and 32 bit.

If you haven’t installed MongoDB on your Windows Pc please Go to the Official website of MongoDB and download the latest setup file 32bit or 64 bit according to your pc architecture.Now let’s start how we can install mongodb windows service.

Note:

When you’re installing MongoDB, please select c:/mongodb as direcotry for easier access and future uses

Before installing mongodb windows service, we will set up MongoDB environment as follows:

  1. MongoDB required Directory to store all database to set that directory Create Data folder inside c:/mongodb or you can specify any folder of your choice and preference or MongoDB will use \data\db inside your MongoDB Installation.
    //here we are using MongoDB installation folder as C:\MongoDB\ and if have used default folder 
    //your folder structure will be like
    //C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe
    "C:\MongoDB\bin\mongod.exe" --dbpath C:\MongoDB\data
    
    //If your path includes spaces, enclose the entire path in double quotes
    "C:/MongoDB\bin\mongod.exe" --dbpath "C:\MongoDB\mongo db data"
    
    
  2. To start MongoDB, run mongod.exe from the Command Prompt navigate to your MongoDB Bin folder and run mongod command, it will start MongoDB main process and The waiting for connections message in the console.
    C:\WINDOWS\system32>cd c:/mongodb/bin
    
    c:\mongodb\bin>mongod
    2017-09-25T12:05:26.410+0530 I CONTROL  [initandlisten] MongoDB starting : pid=12056 port=27017 dbpath=c:\data\ 64-bit host=BlueMachine
    .
    .
    .
    2017-09-25T12:05:31.881+0530 I NETWORK  [thread1] waiting for connections on port 27017
  3. If you want to connect mongodb through shell, use below commands
    C:\WINDOWS\system32>cd c:/mongodb/bin
    
    c:\mongodb\bin>mongo
    MongoDB shell version v3.4.4
    connecting to: mongodb://127.0.0.1:27017
    
    > show dbs
    Ecommerce           0.000GB
    admin               0.000GB
    
See also  MongoDB Drop Database - How to Guide

How to Run MongoDB as Windows Service

  1. Start CMD, Command Prompt as Administrator and create following directories, if you haven’t created already.
    //default db directory
    mkdir c:\mongodb\data\db
    
    //default log direcotry
    mkdir c:\data\log
  2. create the config file in mongodb installation folder, which is c:/mongodb in our case.
     //create a file at C:\MongoDB\mongod.cfg
    systemLog:
        destination: file
        path: c:\mongodb\data\log\mongod.log
    storage:
        dbPath: c:\mongodb\data\db
  3. To install mongodb windows service, use the following command in  Command Prompt as Administrator
    "C:MongoDB\bin\mongod.exe" --config "C:\MongoDB\mongod.cfg" --install
    
  4. To start MongoDB Windows service use the following command
    net start MongoDB
  5. To stop MongoDB windows service
    net stop MongoDB
  6. if want to remove MongoDB service from windows
    "C:\MongoDB\mongod.exe" --remove
    

     

If you’re facing any problem, please run Command Prompt as Administrator and you can also comment below if you’ve any queries.

Leave a Comment