I personally appreciate the fact that MongoDB does not come with a Windows installer. Instead, it is left to the developer to determine where to install MongoDB, and how to configure it. This provides flexibility, and makes MongoDB easier to integrate and distribute with a software application.
But even though there are advantages to the "manual" installation approach, there are also disadvantages. Developers often have to remind themselves of how the installation procedure works, and they often forget one or two configuration settings. Manual installations are all-too-often performed inconsistently, causing unique problems that only occur on the local system. To help mitigate these types of problems, I have created a set of MongoDB batch scripts which make it easier to install, configure, and uninstall MongoDB on a Windows PC.
Relevant background can be found in the link below, from the MongoDB website.
Using the Code
This project consists of the following six Windows Batch scripts:
- MongoDB.bat - This batch script is responsible for installing, configuring, and running MongoDB. This script should never be run directly, but instead is called by other scripts.
- MongoDB-RunCommandLine.bat - This batch script installs and configures MongoDB, and runs it in a command-line window. Terminating the script (or closing the window) causes MongoDB to stop.
- MongoDB-ServiceInstall.bat - This batch script installs and configures MongoDB as a Windows Service.
- MongoDB-ServiceStart.bat - This batch script starts the MongoDB Windows Service.
- MongoDB-ServiceStop.bat - This batch script stops the MongoDB Windows Service.
- MongoDB-ServiceUninstall.bat - This batch script uninstalls the MongoDB Windows Service.
To install MongoDB for the first time, follow these steps:
- Download the MongoDB Installation Scripts presented in this article, unzip them, and place them into a convenient directory.
- Download the MongoDB installation file from the MongoDB website, unzip it, and place it in the same folder as the MongoDB Installation Scripts.
(See the MongoDB downloads page: http://www.mongodb.org/downloads)
- Rename the MongoDB directory to simply "mongodb".
- Install MongoDB using one of the two installation batch scripts:
- For command-line installation: double-click the MongoDB-RunCommandLine.bat script, or run it as Administrator by right-clicking on the MongoDB-RunCommandLine.bat file and choosing "Run as administrator". This action will run MongoDB on the MS-DOS command-line, where it will continue to run for as long as the command-line window stays open!
- For Windows Service installation: double-click the MongoDB-ServiceInstall.bat script, or run it as Administrator by right-clicking on the MongoDB-ServiceInstall.bat file and choosing "Run as administrator". This action will install the MongoDB service into the Windows services panel.
Maintenance and Best Practices
- Uninstalling the MongoDB Windows Service
The MongoDB Windows Service can be uninstalled by double-clicking the
MongoDB-ServiceUninstall.bat script, or by running it as Administrator by right-clicking on the
MongoDB-ServiceUninstall.bat file and choosing "Run as administrator".
- Starting and Stopping the MongoDB Windows Service
Once you have installed MongoDB as a service, you can run the MongoDB-ServiceStart.bat and
MongoDB-ServiceStop.bat scripts to start and stop the MongoDB Windows Service.
- MongoDB Configuration
Whenever you install MongoDB as a service or run it on the command-line, the MongoDB.bat script always checks whether a mongod.conf file exists in the installation directory. If not, a default configuration file is created for you. To customize MongoDB, place all of your configuration settings in this file, and be sure to backup this file periodically!
- Moving the MongoDB Installation Directory
Once you have installed MongoDB as a service or run it on the command-line, moving the MongoDB installation directory itself will cause MongoDB to fail. To move the MongoDB installation directory safely, first stop MongoDB, then move the installation folder, and then update the paths defined in the mongod.conf. Once these paths have been updated, MongoDB should run properly again.
Points of Interest
After you have installed MongoDB for the first time, you will notice a few files and directories appear. A "log" directory will appear, containing the MongoDB log. A "data" directory will also appear, which contains a second directory named "db". This is where the MongoDB database files are stored. A mongod.conf file will also appear in the base directory. This file can be modified to configure MongoDB.
Advantages of Use
- Provides a predictable baseline to work with when installing MongoDB on Windows.
- Helps automate the installation process on customer machines.
- 2014/03/17 - First release
- 2014/03/19 - Article updated with installation instructions and more technical details