Introduction
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.
Step 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.
Step 2. Now, open the command prompt with admin rights as we are going to install or configure a Windows Service for MongoDB.
Step 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.
Step 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.
Step 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.
Step 6. Run the below command in order to install the Windows Service for MongoDB.
mongod --config "C:\MongoFiles\data\mongodb.cfg" –install
Step 7. You will see that a MongoDB Service is now installed in the Services window.
Step 8. 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.
Step 9. and the MongoDB Service will be shown running in the Services window.
Step 10. Let’s hit Mongo Shell command in another terminal in order to check the connection with the database.
Step 11. You can also stop the service with “net stop mongodb” command.
Step 12. Check the Services window and you will find that the service is stopped successfully.
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.