Configure A Windows Service For MongoDB

In this article, we will learn how to configure a Windows Service for MongoDB.

In this article, we will learn how to create and configure a Windows Service for MongoDB. In the previous article of this series, we learned How To Set Up And Start With MongoDB
 
During the installation of MongoDB, we didn’t check the "Install MongoDB as a service" option as I wanted to show you how to do the same with the help of command prompt to make it easier to learn the basic steps.
 
Let’s begin.
  1. Run the "Services.msc" command and check if the MongoDB service is installed or not. As we didn’t install that service during the installation, you will not be able to find it in the Services window.


  2. Now, open the command prompt with admin rights as we are going to install or configure a Windows Service for MongoDB.


  3. Create a log folder in the drive so that each log can be captured. We already saw how to store a database at a particular location in a previous article of this series.


  4. Now, let us add MongoDB.cfg file and add the location of the database (DB) along with the path where we want to log all the loggings of the MongoDB.


    Add the below lines to the config file.

  5. After that, we have to install a Windows Service for MongoDB. We can check the options available for MongoDB command with the use of –help.


    Run the below command in order to install the Windows Service for MongoDB.

    mongod --config "C:\MongoFiles\data\mongodb.cfg" –install


  6. You will see that a MongoDB Service is now installed in the Services window.


  7. Run the “net start mongodb” command in order to run the MongoDB Service. You will get a success message if the service has started successfully.


    and the MongoDB Service will be shown running in the Services window.


  8. Let’s hit Mongo Shell command in another terminal in order to check the connection with the database.


  9. You can also stop the service with “net stop mongodb” command.


    Check the Services window and you will find that the service is stopped successfully.


  10. In case you want to delete the service from the Windows Service window, then run the“sc.exe delete MongoDB” command in the command prompt.
In the next article, we will learn about creating a database in MongoDB. I hope this will help you.