Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Installing MongoDB on Windows Platform

Installing MongoDB on Windows Platform

MongoDB download

The latest version of MongoDB provides precompiled binary packages for 64-bit systems. You can download and install from the MongoDB official website. MongoDB precompiled binary package download address:https://www.mongodb.com/try/download/community

Note: Windows XP is no longer supported after MongoDB 2.2. The latest version also no longer has installation files for 32-bit systems.

Download the .msi file, double-click it after downloading, and follow the prompts to install.

Click the "Next" button to start the installation process:

Next, an agreement window will appear. Select the agree option, then click the "Next" button:

Now there are two choices: you can run MongoDB as a network service user, or run it as a local or domain user.

If you need simplicity and basic functionality, the network service user option is fine. If you need more control over permissions, or need to access and restrict resources with specific user credentials, choosing the local or domain user option is more appropriate.

。

For our simple test, we choose the first one, then click the "Next" button:

Next, we can choose a complete installation. This installation method installs all MongoDB components and tools with default configuration. Of course, you can also choose a custom installation.

After selecting the installation method, you need to click the "Install" button to start the installation process:

After installation, click the "Finish" button to close the installer:

Set environment variables: System Properties -> Environment Variables -> System Variables -> Path -> Edit Environment Variables:

Create a data directory

MongoDB stores the data directory under the db directory; we can create it after installation.

Please note that the data directory should be placed under the root directory (such as C:\ or D:\, etc.).

In this tutorial, we have installed mongodb on the C drive. Now let's create a data directory and then create a db directory inside the data directory.



cd C:\

md "\data\db"

You can also create these directories through Windows Explorer, not necessarily through the command line.


Run the MongoDB server from the command line

Execute the mongod command:



mongod --dbpath c:\data\db

If executed successfully, the following information will be output:


Connect to MongoDB

MongoDB 6.0 and later made significant changes; MongoDB no longer installs the shell tool by default, and you need to install an additional shell tool:MongoDB Shell

We can run the mongo command in the command window to connect to MongoDB. Execute the following command:



mongo


Configure the MongoDB service

Note:Some newer versions of MongoDB complete most of the configuration during installation. If the following directories already exist, you can skip this section.

Open the command window in administrator mode

Create directories. Execute the following statements to create the directories for 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; it is better to include some additional configuration options.

For example, create a configuration file at C:\mongodb\mongod.cfg that specifies 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 the MongoDB service

Install the service by executing mongod with the --install option, and use the --config option to specify the previously created configuration file.

mongod  --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 through the --dbpath option on the command line.

If needed, you can install services for multiple instances of mongod or mongos. You only need to specify different instance names using --serviceName and --serviceDisplayName. Only do this when there are sufficient system resources and the system design requires it.

Starting the MongoDB service

net start MongoDB

Stop the MongoDB service

net stop MongoDB

Remove MongoDB Service

mongod --remove

Run the MongoDB server from the command line and Configure the MongoDB service Any one method of startup will do.

Just choose any one operation


MongoDB background management Shell

If you need to enter MongoDB backend management, you need to first open the bin directory under the mongodb installation directory, then execute the mongo.exe file. MongoDB Shell is an interactive Javascript shell bundled with MongoDB, an interactive environment for operating and managing MongoDB.

After you enter the mongoDB backend, it connects to the test document (database) by default:



> 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

>

db The command is used to view the document (database) currently in operation:



> db

test

>

Insert some simple records and find them:



> db.runoob.insert({x:10})

WriteResult({ "nInserted" : 1 })

> db.runoob.find()

{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }

>

The first command inserts the number 10 into the x field of the runoob collection.