Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

在 Windows 上安装 MongoDB Community Edition

在此页面上

  • Overview
  • Considerations
  • 安装 MongoDB Community Edition
  • 将 MongoDB Community 版作为 Windows 服务运行
  • Run MongoDB Community Edition from the Command Interpreter
  • 其他注意事项

注意

MongoDB Atlas

MongoDB Atlas 是在云中托管的 MongoDB 服务选项,无需安装开销,并提供免费套餐,可以立即开始使用。

使用本教程,通过默认安装向导在 Windows 上安装 MongoDB 6.0 社区版。

本教程安装的是 MongoDB 6.0 社区版。要安装不同的 MongoDB Community,使用此页面左上角的版本下拉菜单以选择该版本的文档。

本教程使用默认 MSI 安装向导在 Windows 上安装 MongoDB。要使用 msiexec.exe命令行工具安装 MongoDB,请参阅使用 msiexec.exe 安装 MongoDBmsiexec.exe工具对于希望使用自动化功能以无人值守的方式部署 MongoDB 的系统管理员非常有用。

MongoDB Shell ( mongosh ) 不随 MongoDB Server 一起安装。您需要按照mongosh 安装说明单独下载并安装mongosh

MongoDB 6.0 MongoDB Community Edition支持采用 x86_64 架构的以下 64 位 版本的 Windows:

  • Windows Server 2019

  • Windows 10 / Windows 服务器 2016

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

更多信息,请参阅平台支持

注意

适用于 Linux 的 Windows 子系统 (WSL) 不支持 MongoDB。要在 Linux 上运行 MongoDB,请使用受支持的 Linux 系统。

Oracle 为运行 Hyper-V 的 Windows 主机上的 VirtualBox 提供实验性支持。但是,Microsoft 不支持在 Hyper-V 上使用 VirtualBox。

Disable Hyper-V if you want to install MongoDB on Windows using VirtualBox.

在生产环境中部署 MongoDB 之前,请参阅《自管理部署的生产说明》文档,其中提供了 MongoDB 生产部署的性能注意事项和配置建议。

按照以下步骤使用 MongoDB 安装向导安装 MongoDB Community Edition。安装过程会同时安装 MongoDB 的二进制文件以及默认配置文件 <install directory>\bin\mongod.cfg

1

从以下链接下载 MongoDB 社区版 .msi 安装程序:

MongoDB 下载中心

  1. Version 下拉列表中选择要下载的 MongoDB 版本。

  2. Platform 下拉菜单中,选择 Windows

  3. Package 下拉菜单中,选择 msi

  4. 单击 Download(连接)。

2

例如,在 Windows 资源管理器/文件资源管理器中:

  1. 前往您下载 MongoDB 安装程序(.msi 文件)的目录。默认是您的 Downloads 目录。

  2. 双击 .msi 文件。

3

该向导将引导您完成 MongoDB 和 MongoDB Compass 的安装。

  1. Choose Setup Type
    您可以选择Complete(建议大多数用户使用)或Custom安装类型。Complete 安装选项会将 MongoDB 和 MongoDB 工具安装到默认位置。Custom 安装选项允许指定安装哪些可执行文件以及安装位置。
  2. Service Configuration

    您可以在安装过程中将 MongoDB 设置为 Windows 服务,也可以仅安装二进制文件。

    您可以在安装过程中将 MongoDB 配置为 Windows 服务并启动,MongoDB 服务会在安装成功后启动。

    • 选择 Install MongoD as a Service

    • 选择以下选项之一:

      • Run the service as Network Service user (默认)

        这是 Windows 内置的 Windows 用户帐户。

      • Run the service as a local or domain user

        • 对于现有的本地用户帐户,为Account Domain指定句点 ( . ),并为用户指定Account NameAccount Password

        • 对于现有域用户,请为该用户指定Account DomainAccount NameAccount Password

    • Service Name。指定服务名称。默认名称为MongoDB 。如果您已有指定名称的服务,则必须选择其他名称。

    • Data Directory。指定与--dbpath相对应的数据目录。如果该目录不存在,安装程序将创建目录并为服务用户设置目录访问权限。

    • Log Directory。指定对应于--logpath的日志目录。如果该目录不存在,安装程序将创建目录并为服务用户设置目录访问权限。

    如果选择不将 MongoDB 配置为 Windows 服务,请取消选中Install MongoD as a Service

  3. Install MongoDB Compass
    可选。要让向导安装 MongoDB Compass,请选择 Install MongoDB Compass(默认)。
  4. 准备就绪后,单击 Install(安装)。

.msi安装程序不包含mongosh 。按照mongosh 安装说明单独下载并安装 shell。

MongoDB 服务在安装成功后启动。使用配置文件 <install directory>\bin\mongod.cfg 配置 MongoDB 实例。

如果仅安装可执行文件而未将 MongoDB 安装为 Windows 服务,那么您必须手动启动 MongoDB 实例。

请参阅从命令解释器运行 MongoDB Community Edition,获取启动 MongoDB 实例的说明。

您可以在安装过程中将 MongoDB 安装并配置为Windows Service 。 MongoDB 服务将在安装成功后启动。使用配置文件<install directory>\bin\mongod.cfg配置 MongoDB 实例。

如果尚未下载并安装 MongoDB Shell ( mongosh ),请按照 mongosh 安装说明 进行操作。

请务必在安装过程中将 mongosh.exe 二进制文件的路径添加到 PATH 环境变量中。

打开新的 Command Interpreter,然后输入 mongosh.exe 以连接 MongoDB。

有关使用 mongosh.exe 连接到 mongod 的更多信息,例如连接在其他主机和/或端口上运行的 MongoDB 实例,请参阅连接到部署。

有关 CRUD(创建、读取、更新、删除)操作的信息,请参阅:

To start/restart the MongoDB service, use the Services console:

  1. 在“服务”控制台中,找到 MongoDB 服务。

  2. 右键单击 MongoDB 服务,然后单击Start Start (启动)。

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

  1. 在“服务”控制台中,找到 MongoDB 服务。

  2. 右键单击 MongoDB 服务,然后单击 Stop(停止)(或 Pause(暂停))。

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

sc.exe delete MongoDB

您可以从 Windows 命令提示符/解释器 运行 MongoDB Community Edition (cmd.exe ) 而不是作为服务。

Administrator 身份打开 Windows 命令提示/解释器 (cmd.exe)。

重要

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

1

Create the data directory where MongoDB stores data. MongoDB's default data directory path is the absolute path \data\db on the drive from which you start MongoDB.

Command Interpreter 创建数据目录:

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

要启动 MongoDB,请运行 mongod.exe

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

The --dbpath option points to your database directory.

如果 MongoDB 数据库服务器运行正常,则Command Interpreter显示:

[initandlisten] waiting for connections

重要

根据 Windows 主机上的 Windows Defender 防火墙设置,Windows 可能会显示一个 Security Alert 对话框,提示阻塞 C:\Program Files\MongoDB\Server\6.0\bin\mongod.exe 的“某些功能”在网络上进行通信。要解决此问题,请执行以下操作:

  1. 单击 Private Networks, such as my home or work network(连接)。

  2. 单击 Allow access(连接)。

要了解有关安全和 MongoDB 的更多信息,请参阅安全文档。

3

如果尚未下载并安装 MongoDB Shell ( mongosh ),请按照 mongosh 安装说明 进行操作。

请务必在安装过程中将 mongosh.exe 二进制文件的路径添加到 PATH 环境变量中。

打开新的 Command Interpreter,然后输入 mongosh.exe 以连接 MongoDB。

有关使用 mongosh.exe 连接到 mongod (例如连接在其他主机和/或端口上运行的 MongoDB 实例)的更多信息,请参阅连接到部署。

有关 CRUD(创建、读取、更新、删除)操作的信息,请参阅:

By default, MongoDB launches with bindIp set to 127.0.0.1, which binds to the localhost network interface. This means that the mongod.exe can only accept connections from clients that are running on the same machine. Remote clients will not be able to connect to the mongod.exe, and the mongod.exe will not be able to initialize a replica set unless this value is set to a valid network interface which is accessible from the remote clients.

该值可通过以下任一方式配置:

  • 在 MongoDB 配置文件中使用 bindIp,或

  • 通过命令行参数 --bind_ip

警告

在绑定到非本地主机(例如可公开访问)的 IP 地址之前,请确保您已保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自托管部署的安全检查清单。至少应考虑启用身份验证强化网络基础架构。

有关配置bindIp的详细信息,请参阅自托管部署中的 IP 绑定。

如果您使用 Windows 安装程序 (.msi) 安装了 MongoDB,则 .msi 将在其版本系列中自动升级(例如从 7.2.1 升级至 7.2.2)。

升级完整版本系列(例如6.0 至 7.0)需要全新安装。

如果将C:\Program Files\MongoDB\Server\6.0\bin添加到系统PATH ,则可以省略 MongoDB Server 二进制文件的完整路径。如果还没有这样做,还应该添加mongosh的路径。

后退

使用 .tgz Tarball 进行安装