Installing MongoDB
on Windows Platform
MongoDB Download
MongoDB offers pre-compiled binary packages for both 32-bit and 64-bit systems. You can download and install them from the MongoDB official website. The download link for the pre-compiled binary packages is: https://www.mongodb.com/download-center/community
Note: MongoDB versions after 2.2 no longer support Windows XP systems. The latest versions also do not include installation files for 32-bit systems.
- MongoDB for Windows 64-bit is suitable for 64-bit Windows Server 2008 R2, Windows 7, and the latest versions of Windows systems.
- MongoDB for Windows 32-bit is suitable for 32-bit Windows systems and the latest Windows Vista. MongoDB on a 32-bit system has a maximum database size of 2GB.
- MongoDB for Windows 64-bit Legacy is suitable for 64-bit Windows Vista, Windows Server 2003, and Windows Server 2008.
Download the .msi file, double-click it after downloading, and follow the prompts to install.
During the installation process, you can click the "Custom" button to set your installation directory.
Next, do not check "install mongoDB compass" (although you can choose to install it, which may take longer). MongoDB Compass is a graphical interface management tool, which we can download and install later from the official website. The download link is: https://www.mongodb.com/download-center/compass.
Creating Data Directories
MongoDB stores data directories in the db directory. However, this data directory is not created automatically, and we need to create it after installation. Note that the data directory should be placed in the root directory (such as C:\ or D:\, etc.).
In this tutorial, we have installed MongoDB on the C drive, so let's create a data directory and then a db directory inside it.
cd C:\
md "\data\db"
You can also create these directories through Windows Explorer instead of using the command line.
Running MongoDB Server from the Command Line
To run the MongoDB server from the command prompt, you must execute the mongod.exe file from the bin directory of the MongoDB installation directory.
C:\mongodb\bin\mongod --dbpath c:\data\db
If successful, the following information will be output:
2015-09-25T15:54:09.212+0800 I CONTROL Hotfix KB2731284 or later update is not
installed, will zero-out data files
2015-09-25T15:54:09.229+0800 I JOURNAL [initandlisten] journal dir=c:\data\db\j
ournal
2015-09-25T15:54:09.237+0800 I JOURNAL [initandlisten] recover : no journal fil
es present, no recovery needed
2015-09-25T15:54:09.290+0800 I JOURNAL [durability] Durability thread started
2015-09-25T15:54:09.294+0800 I CONTROL [initandlisten] MongoDB starting : pid=2
488 port=27017 dbpath=c:\data\db 64-bit host=WIN-1VONBJOCE88
2015-09-25T15:54:09.296+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/W
indows Server 2008 R2
2015-09-25T15:54:09.298+0800 I CONTROL [initandlisten] db version v3.0.6
……
Connecting to MongoDB
We can connect to MongoDB by running the mongo.exe command in the command window. Execute the following command:
C:\mongodb\bin\mongo.exe
Configuring MongoDB Service
Note: Some newer versions of MongoDB have already completed most of the configuration during installation. If the directories below already exist, you can skip this part.
Open Command Prompt as Administrator
Create directories by executing the following statements to create directories for the database and log files:
mkdir c:\data\db
mkdir c:\data\log
Create a Configuration File
Create a configuration file. This file must set the systemLog.path parameter, and including additional configuration options is better.
For example, create a configuration file at C:\mongodb\mongod.cfg, specifying systemLog.path and storage.dbPath. The specific configuration content is as follows:
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
Install MongoDB Service
Install the service by executing mongod.exe with the --install option and specify the configuration file created earlier using the --config option.
C:\mongodb\bin\mongod.exe --config "C:\mongodb\mongod.cfg" --install
To use an alternate dbpath, you can specify it in the configuration file (e.g., C:\mongodb\mongod.cfg) or via the --dbpath option in the command line.
If needed, you can install multiple instances of mongod.exe or mongos.exe as services. Just use the --serviceName and --serviceDisplayName options to specify different instance names. This is only necessary if there are sufficient system resources and the system design requires it.
Start MongoDB Service
net start MongoDB
Stop MongoDB Service
net stop MongoDB
Remove MongoDB Service
C:\mongodb\bin\mongod.exe --remove
Running MongoDB Server from the Command Line and Configuring MongoDB Service are alternative methods to start MongoDB.
MongoDB Background Management Shell
If you need to access the MongoDB background management, you need to open the bin directory under the MongoDB installation directory and then execute the mongo.exe file. The MongoDB Shell is an interactive JavaScript shell provided by MongoDB for operating and managing MongoDB.
When you enter the MongoDB backend, it defaults to connecting to the test database:
> mongo
MongoDB shell version: 3.0.6
connecting to: test
……
Since it is a JavaScript shell, you can run some simple arithmetic operations:
> 2 + 2
4
>
The db command is used to view the current database being operated on:
> db
test
>
Insert some simple records and find them:
> db.tutorialpro.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.tutorialpro.find()
{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }
>
The first command inserts the number 10 into the x field of the tutorialpro collection.