在 Windows 上安装 MongoDB 社区版

在本页面

Overview

使用本教程,可以使用默认安装向导在 Windows 上安装 MongoDB 3.6 Community Edition。

MongoDB Version

本教程将安装 MongoDB 3.6 Community Edition。要安装其他版本的 MongoDB 社区,请使用此页面左上角的版本下拉菜单选择该版本的文档。

Installation Method

本教程使用默认安装向导在 Windows 上安装 MongoDB。或者,您可以选择在命令行(cmd.exe)中使用msiexec.exe以无人参与的方式在 Windows 上安装 MongoDB。这对于希望使用自动化部署 MongoDB 的系统 Management 员很有用。

➤有关说明,请参见使用 msiexec.exe 在 Windows 上安装 MongoDB 社区

Considerations

Platform Support

MongoDB 3.6 Community Edition 在x86_64体系结构上支持以下 Windows 的 64 位 版本:

  • Windows 10/Windows Server 2016

  • Windows 8.1/Windows Server 2012 R2

  • Windows 8/Windows Server 2012

  • Windows 7/Windows Server 2008 R2

MongoDB 仅支持这些平台的 64 位版本。

有关更多信息,请参见Supported Platforms

Production Notes

在生产环境中部署 MongoDB 之前,请考虑Production Notes文档,该文档提供了生产 MongoDB 部署的性能注意事项和配置建议。

安装 MongoDB 社区版

Prerequisites

Windows 10 之前的 Windows 版本上的用户必须在安装 MongoDB 之前安装以下更新:

Windows 通用 C 运行时更新

Windows 10,Server 2016 和 Server 2019 上的用户不需要此更新。

Procedure

请按照以下步骤使用 MongoDB 安装程序向导安装 MongoDB 社区版。安装过程将同时安装 MongoDB 二进制文件和默认的configuration file <install directory>\bin\mongod.cfg

下载 MongoDB 社区版。

  • 打开网络浏览器,然后访问MongoDB 下载中心

  • 下载中心应显示 MongoDB 社区服务器选项卡。如果没有,请选择“服务器”,然后单击“ MongoDB 社区服务器”选项卡。

  • 在“版本”下拉列表中,选择与 MongoDB Server 3.6 对应的版本。下载中心始终显示最新的可用点版本。对于较旧的发行版,请在右侧导航框中单击“所有版本二进制文件”。

  • 在 os 下拉菜单中,确认已选择Windows 64-bit X64

  • 在“包”下拉列表中,确认已选择MSI

  • Click Download.

打开 Windows 资源 Management 器。

导航至您下载 MongoDB 安装程序的目录。

双击.msi 文件。

遵循 MongoDB 社区版安装向导。

如果选择“自定义”安装选项,则可以指定安装目录。 MongoDB 没有其他系统依赖性。您可以从任何目录安装和运行 MongoDB。

从命令解释器运行 MongoDB 社区版

以 Management 员身份打开Windows 命令提示符/解释器(cmd.exe)。

Important

您必须以 Management 员身份打开命令解释器。

创建数据库目录。

在 MongoDB 存储数据的位置创建data directory。 MongoDB 的默认数据目录路径是启动 MongoDB 的驱动器上的绝对路径\data\db

在命令解释器中,创建数据目录:

cd C:\
md "\data\db"

启动您的 MongoDB 数据库。

要启动 MongoDB,请调用mongod.exe

"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe" --dbpath="c:\data\db"

--dbpath选项指向您的数据库目录。

如果 MongoDB 数据库服务器正常运行,则命令解释器将显示:

[initandlisten] waiting for connections

Important

根据 Windows 主机上的Windows Defender 防火墙设置,Windows 可能会显示一个“安全警报”对话框,用于阻止C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe的“某些功能”在网络上进行通信。要解决此问题:

  • 单击专用网络,例如我的家庭或工作网络。

  • 单击允许访问。

要了解有关安全性和 MongoDB 的更多信息,请参阅Security Documentation

连接到 MongoDB。

要将mongo.exe shell 连接到 MongoDB 实例,请打开另一个具有 Management 权限的命令解释器,然后运行:

"C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"

有关连接mongo.exe shell 的更多信息,例如连接到在其他主机和/或端口上运行的 MongoDB 实例,请参阅Mongo 壳牌。有关 CRUD(创建,读取,更新,删除)操作的信息,请参见MongoDB CRUD 操作

为了帮助您开始使用 MongoDB,MongoDB 提供了各种驱动程序版本的入门指南。有关可用版本,请参见Getting Started

将 MongoDB 社区版作为 Windows 服务运行

将 MongoDB 社区版作为 Windows 服务启动

您可以将 MongoDB 服务器设置为 Windows 服务,该服务在启动时自动启动。

创建数据库和日志目录。

如果尚未为 MongoDB 服务器创建数据和日志目录,则必须在将 MongoDB Community Edition 作为 Windows 服务运行之前创建它们。

在命令解释器中,创建以下目录:

cd C:\
md "\data\db" "\data\log"

创建一个 MongoDB 配置文件。

创建一个 MongoDB 配置文件:

C:\Program Files\MongoDB\Server\3.6\mongod.cfg

Note

MongoDB 配置文件使用YAML文件格式。根据YAML 1.2 规范,所有缩进必须使用空格来保持可移植性。为确保没有解析错误,请将所有值括在双引号(")中,并转义任何反斜杠字符(\)。

设置storage.dbPathsystemLog.path。根据需要添加其他configuration options

systemLog:
  destination: "file"
  path: "c:\\data\\log\\mongod.log"
storage:
  dbPath: "c:\\data\\db"

创建 MongoDB 服务。

在命令解释器中调用以下命令以创建服务:

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe\" ^
  --service --config=\"C:\Program Files\MongoDB\Server\3.6\mongod.cfg\"" DisplayName= "MongoDB" start= auto

Note

sc.exe在“ =”和配置值(即binPath=)之间需要一个空格,并需要使用"\"\""来转义双引号字符串。

如果创建成功,则将以下消息写入日志:

[SC] CreateService SUCCESS

启动 MongoDB 服务。

关闭所有其他命令提示符,然后调用以下命令:

net start MongoDB

确认 MongoDB 已成功启动。

检查您的 MongoDB 日志文件是否存在以下行:

[initandlisten] waiting for connections on port 27017

您可能会在过程输出中看到非严重警告。只要您在 MongoDB 日志中看到此消息,就可以在对 MongoDB 进行初始评估时安全地忽略这些警告。

连接到 MongoDB 服务器。

要通过mongo.exe shell 连接到 MongoDB,请打开另一个命令解释器。

"C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"

将 MongoDB 社区版停止为 Windows 服务

要停止/暂停 MongoDB 服务,请使用服务控制台:

  • 在服务控制台中,找到 MongoDB 服务。

  • 右键单击 MongoDB 服务,然后单击“停止”(或“暂停”)。

您也可以从Windows 命令提示符/解释器(cmd.exe)停止 MongoDB 服务。以 Management 员身份打开Windows 命令提示符/解释器(cmd.exe),然后运行以下命令:

net stop MongoDB

将 MongoDB 社区版作为 Windows 服务删除

要删除 MongoDB 服务,请首先使用服务控制台停止该服务。然后以 Management 员身份打开Windows 命令提示符/解释器(cmd.exe),然后运行以下命令:

sc.exe delete MongoDB

Additional Considerations

默认情况下,localhost 绑定

默认情况下,MongoDB 在bindIp设置为127.0.0.1的情况下启动,该绑定到 localhost 网络接口。这意味着mongod.exe仅可以接受来自同一计算机上运行的 Client 端的连接。远程 Client 端将无法连接到mongod.exe,并且mongod.exe将无法初始化replica set,除非将此值设置为有效的网络接口。

可以配置此值:

  • 在带有bindIp的 MongoDB 配置文件中,或者

  • 通过命令行参数--bind_ip

Warning

绑定到非 localhost(例如可公开访问)的 IP 地址之前,请确保已保护群集免受未经授权的访问。有关安全建议的完整列表,请参见Security Checklist。至少考虑enabling authentication加强网络基础设施

有关配置bindIp的更多信息,请参见MongoDB 配置强化

Point 版本和.msi

如果您使用 Windows 安装程序(.msi)安装了 MongoDB,则.msi会在其release series内自动升级(例如,从 3.6.1 升级到 3.6.2)。

升级完整版本系列(例如 3.6 至 4.0)需要重新安装。

将 MongoDB 二进制文件添加到系统 PATH

本教程中的所有命令行示例均作为 MongoDB 二进制文件的绝对路径提供。您可以将C:\Program Files\MongoDB\Server\3.6\bin添加到系统PATH,然后省略 MongoDB 二进制文件的完整路径。