• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:7 mins read
You are currently viewing Run MongoDB as a Window Service

How to run MongoDB as a Window Service? This article will walk you through the steps required to set up MongoDB as a Windows service. Running MongoDB as a service is a convenient way to ensure that the database is always running, even if the system restarts or shuts down. Additionally, running MongoDB as a service provides better control and security as only authorized users can interact with the service.

Advertisements

1. Check the MongoDB as a window service

As a first step, make sure you verify MongoDB is not running as a window service in your system. You can check this by using the following command from the prompt. The results showed that the service name is invalid which indicates that the MongoDB services are not installed and running on the system.


# Verify MongoDB running as window service
net start MongoDB
MongoDB as window service

2. Download the MongoDB in the system

The next step is to download and install MongoDB Community Edition, download the MongoDB Community Server for Windows, and install it on the computer. The official MongoDB website is where you can download from. Here, we have downloaded version MongoDB version 6.0. The image below shows the options for downloading MongoDB.

MongoDB as window service

3. Run the MongoDB installer

After downloading the msi installer of MongoDB, run the installer and followed all the steps to install MongoDB. Be sure to select the Complete installation type, which includes the MongoDB server, client tools, and other components.

3.1 Navigate to the bin directory

Once the download is completed, open the command prompt in administrator mode and go to the bin directory to access MongoDB. Here, MongoDB is installed in the C:\Program Files\MongoDB\Server\6.0\bin directory, we have navigated to this directory by running the following command.


# Navigate to the bin directory
cd C:\Program Files\MongoDB\Server\6.0\bin

3.2 Install MongoDB as a window service

Now, after following the aforementioned steps, we are in the bin directory. Thus, install MongoDB as a window service. To do this, we must execute the following command to install MongoDB as a service. This will install MongoDB as a Windows service.


# Install MongoDB as Window service
mongod.exe --install --config "C:\Program Files\MongoDB\Server\6.0\bin\mongod.cfg" --serviceName "MongoDB"

3.3 Start the MongoDB as a Service

As we have successfully installed the MongoDB service by running the above command. Now, we can start the MongoDB service in our system by using the following command. This will start the MongoDB service, and we can now use the MongoDB shell to connect to the MongoDB instance.


# Starte MongoDB service
net start MongoDB

4. Connect to MongoDB with shell

Connect to the MongoDB instance with mongosh, simply open a new command prompt and type the following command:


# Connect the MongoDB instance with mongosh
mongosh

By doing this so, we can connect to the MongoDB instance and run the MongoDB shell on our system as shown below.

MongoDB as window service

5. Stop the MongoDB service

However, if we don’t want to run MongoDB as a window service in our system, we can stop it from running. The following command is used to stop the window service.


# Stop MongoDB service
net stop MongoDB

The image indicates that the service is now stopped, and we can’t access the MongoDB instance.

6. Uninstall the MongoDB services

Moreover, if we want to uninstall the MongoDB instances from the system. Then, we have to run the following command which removes the MongoDB as a window service from our system.


# Uninstall MongoDB service
mongod.exe --remove

Now, let’s check whether the services of MongoDB are removed or not by running the below command. This actually stops the MongoDB, if you get the error, it indicates that services have been removed here. We can again install it by following the aforementioned steps.

MongoDB as window service

7. Conclusion

In this article, first, we checked if MongoDB is already running as a window service, If it is not running, downloaded, installed, and started it as a service. Running it as a windows service ensures that it starts automatically when the Windows operating system boots up.

More details about this topic can be found here.