Skip to content

Run MongoDB as a Window Service AQSA Spark By {Examples}

  • by

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.

1. Check the MongoDB as a window service

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

# Verify MongoDB running as window service
net start MongoDB

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 MongoDB. Here, we have downloaded the version of MongoDB for windows which is 6.0. The image below shows the options for downloading MongoDB.

3. Run the MongoDB installer

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

4. Navigate to the bin directory

Access the Command Prompt in administrator mode after the download is finished and go to the bin directory to access MongoDB. Here, MongoDB is installed in the C:Program FilesMongoDBServer6.0bin directory, we have navigated to this directory by running the following command.

# Navigate to the bin directory
cd C:Program FilesMongoDBServer6.0bin

5. 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 FilesMongoDBServer6.0binmongod.cfg” –serviceName “MongoDB”

6. Start the MongoDB 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

7. 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.

8. 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.

9. 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.

10. Conclusion

In this article, first, we checked if MongoDB is running as a Windows service, downloaded, installed, started it as a service, and stopped it. 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.

 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  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *