在 Azure 中的 Windows VM 上安装和配置 MongoDBInstall and configure MongoDB on a Windows VM in Azure

MongoDB 是一个流行的开源、高性能 NoSQL 数据库。MongoDB is a popular open-source, high-performance NoSQL database. 本文逐步讲解如何在 Azure 中的 Windows Server 2016 虚拟机 (VM) 上安装和配置 MongoDB。This article guides you through installing and configuring MongoDB on a Windows Server 2016 virtual machine (VM) in Azure. 也可以在 Azure 中的 Linux VM 上安装 MongoDBYou can also install MongoDB on a Linux VM in Azure.

必备条件Prerequisites

在安装和配置 MongoDB 之前,需要创建一个 VM,并且最好将一个磁盘添加到该 VM。Before you install and configure MongoDB, you need to create a VM and, ideally, add a data disk to it. 请参阅以下文章创建 VM 并向其添加数据磁盘:See the following articles to create a VM and add a data disk:

使用远程桌面登录到 Windows Server VM,开始安装和配置 MongoDB。To begin installing and configuring MongoDB, log on to your Windows Server VM by using Remote Desktop.

安装 MongoDBInstall MongoDB

Important

默认情况下,不启用 MongoDB 安全功能,例如身份验证和 IP 地址绑定。MongoDB security features, such as authentication and IP address binding, are not enabled by default. 在将 MongoDB 部署到生产环境之前,应启用安全功能。Security features should be enabled before deploying MongoDB to a production environment. 有关详细信息,请参阅 MongoDB Security and Authentication(MongoDB 安全性和身份验证)For more information, see MongoDB Security and Authentication.

  1. 使用远程桌面连接到 VM 后,请从任务栏打开 Internet Explorer。After you've connected to your VM using Remote Desktop, open Internet Explorer from the taskbar.

  2. 当 Internet Explorer 首次打开时,请选择“使用推荐的安全性、隐私和兼容性设置” ,并单击“确定” 。Select Use recommended security, privacy, and compatibility settings when Internet Explorer first opens, and click OK.

  3. 默认情况下已启用 Internet Explorer 增强的安全性配置。Internet Explorer enhanced security configuration is enabled by default. 将 MongoDB 网站添加到允许的站点列表:Add the MongoDB website to the list of allowed sites:

    • 选择右上角的“工具” 图标。Select the Tools icon in the upper-right corner.

    • 在“Internet 选项”中,选择“安全”选项卡,并选择“可信站点”图标。 In Internet Options, select the Security tab, and then select the Trusted Sites icon.

    • 单击“站点”按钮。 Click the Sites button. https://*.mongodb.com 添加到受信任的站点列表,然后关闭对话框。Add https://*.mongodb.com to the list of trusted sites, and then close the dialog box.

      配置 Internet Explorer 安全性设置

  4. 请转到 MongoDB - 下载页 (https://www.mongodb.com/downloads) )。Browse to the MongoDB - Downloads page (https://www.mongodb.com/downloads).

  5. 如有需要,选择“社区服务器” 版本,并选择 Windows Server 2008 R2 64 位及更高版本的最新稳定版本。If needed, select the Community Server edition and then select the latest current stable release forWindows Server 2008 R2 64-bit and later. 若要下载安装程序,请单击“DOWNLOAD (msi)” 。To download the installer, click DOWNLOAD (msi).

    下载 MongoDB 安装程序

    下载完成后,请运行安装程序。Run the installer after the download is complete.

  6. 阅读并接受许可协议。Read and accept the license agreement. 出现提示时,请选择“完整” 安装。When you're prompted, select Complete install.

  7. 如果需要,可以选择还安装“指南针”(MongoDB 的图形界面)。If desired, you can choose to also install Compass, a graphical interface for MongoDB.

  8. 在最后一个屏幕上,单击“安装”。 On the final screen, click Install.

配置 VM 和 MongoDBConfigure the VM and MongoDB

  1. MongoDB 安装程序不会更新路径变量。The path variables are not updated by the MongoDB installer. 如果路径变量中不包含 MongoDB bin 位置,则每次使用 MongoDB 可执行文件时都需要指定完整路径。Without the MongoDB bin location in your path variable, you need to specify the full path each time you use a MongoDB executable. 要将位置添加到路径变量,请执行以下操作:To add the location to your path variable:

    • 右键单击“开始” 菜单,并选择“系统” 。Right-click the Start menu, and select System.

    • 依次单击“高级系统设置” 和“环境变量” 。Click Advanced system settings, and then click Environment Variables.

    • 在“系统变量” 下,选择“路径” ,并单击“编辑” 。Under System variables, select Path, and then click Edit.

      配置 PATH 变量

      将路径添加到 MongoDB bin 文件夹。Add the path to your MongoDB bin folder. MongoDB 通常安装在 C:\Program Files\MongoDB 下。 MongoDB is typically installed in C:\Program Files\MongoDB. 检查 VM 上的安装路径。Verify the installation path on your VM. 以下示例将默认的 MongoDB 安装位置添加到 PATH 变量:The following example adds the default MongoDB install location to the PATH variable:

      ;C:\Program Files\MongoDB\Server\3.6\bin
      

      Note

      请务必添加前置分号 (;),指明要将一个位置添加到 PATH 变量。Be sure to add the leading semicolon (;) to indicate that you are adding a location to your PATH variable.

  2. 在数据磁盘上创建 MongoDB 数据和日志目录。Create MongoDB data and log directories on your data disk. 在“开始”菜单中,选择“命令提示符”。 From the Start menu, select Command Prompt. 以下示例在 F: 驱动器中创建目录:The following examples create the directories on drive F:

    mkdir F:\MongoData
    mkdir F:\MongoLogs
    
  3. 使用以下命令启动 MongoDB 实例,并相应地调整数据和日志目录的路径:Start a MongoDB instance with the following command, adjusting the path to your data and log directories accordingly:

    mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log
    

    MongoDB 可能需要几分钟来分配日志文件和开始侦听连接。It may take several minutes for MongoDB to allocate the journal files and start listening for connections. mongod.exe 服务器启动和分配日志文件时,所有日志消息将定向到 F:\MongoLogs\mongolog.log 文件。All log messages are directed to the F:\MongoLogs\mongolog.log file as mongod.exe server starts and allocates journal files.

    Note

    运行 MongoDB 实例时,命令提示符的焦点放在此任务上。The command prompt stays focused on this task while your MongoDB instance is running. 将命令提示符窗口保持打开状态,以便继续运行 MongoDB。Leave the command prompt window open to continue running MongoDB. 或者,如下一步骤的详述,将 MongoDB 作为服务安装。Or, install MongoDB as service, as detailed in the next step.

  4. 为获得更健全的 MongoDB 体验,请将 mongod.exe 作为服务安装。For a more robust MongoDB experience, install the mongod.exe as a service. 创建服务意味着不需要每次使用 MongoDB 时都要运行命令提示符。Creating a service means you don't need to leave a command prompt running each time you want to use MongoDB. 按如下所示创建服务,并相应地调整数据和日志目录的路径:Create the service as follows, adjusting the path to your data and log directories accordingly:

    mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log --logappend  --install
    

    上述命令创建一个名为“MongoDB”的服务,其描述为“Mongo DB”。The preceding command creates a service named MongoDB, with a description of "Mongo DB". 也可以指定以下参数:The following parameters are also specified:

    • --dbpath 选项指定数据目录的位置。The --dbpath option specifies the location of the data directory.
    • 必须使用 --logpath 选项指定日志文件,因为正在运行的服务不会在命令窗口中显示输出。The --logpath option must be used to specify a log file, because the running service does not have a command window to display output.
    • --logappend 选项指定重新启动服务时,会将输出追加到现有日志文件。The --logappend option specifies that a restart of the service causes output to append to the existing log file.

    若要启动 MongoDB 服务,请运行以下命令:To start the MongoDB service, run the following command:

    net start MongoDB
    

    有关创建 MongoDB 服务的详细信息,请参阅 Configure a Windows Service for MongoDB(为 MongoDB 配置 Windows 服务)。For more information about creating the MongoDB service, see Configure a Windows Service for MongoDB.

测试 MongoDB 实例Test the MongoDB instance

以单一实例的形式运行 MongoDB 或将它安装为服务后,可以开始创建并使用数据库。With MongoDB running as a single instance or installed as a service, you can now start creating and using your databases. 若要启动 MongoDB 管理 shell,请从“开始” 菜单打开另一个命令提示符窗口并输入以下命令:To start the MongoDB administrative shell, open another command prompt window from the Start menu, and enter the following command:

mongo  

可以使用 db 命令列出数据库。You can list the databases with the db command. 按如下所示插入一些数据:Insert some data as follows:

db.foo.insert( { a : 1 } )

按如下所示搜索数据:Search for data as follows:

db.foo.find()

输出类似于以下示例:The output is similar to the following example:

{ "_id" : "ObjectId("57f6a86cee873a6232d74842"), "a" : 1 }

按如下所示退出 mongo 控制台:Exit the mongo console as follows:

exit

配置防火墙和网络安全组规则Configure firewall and Network Security Group rules

安装并运行 MongoDB 后,请在 Windows 防火墙中打开一个端口,以便能够远程连接到 MongoDB。Now that MongoDB is installed and running, open a port in Windows Firewall so you can remotely connect to MongoDB. 若要创建一个新的入站规则以允许 TCP 端口 27017,请打开管理 PowerShell 提示符并输入以下命令:To create a new inbound rule to allow TCP port 27017, open an administrative PowerShell prompt and enter the following command:

New-NetFirewallRule `
    -DisplayName "Allow MongoDB" `
    -Direction Inbound `
    -Protocol TCP `
    -LocalPort 27017 `
    -Action Allow

也可以使用“具有高级安全性的 Windows 防火墙” 图形管理工具创建该规则。You can also create the rule by using the Windows Firewall with Advanced Security graphical management tool. 创建一个新的入站规则以允许 TCP 端口 27017。Create a new inbound rule to allow TCP port 27017.

如有需要,创建一个网络安全组规则,以允许从现有 Azure 虚拟网络子网外部访问 MongoDB。If needed, create a Network Security Group rule to allow access to MongoDB from outside of the existing Azure virtual network subnet. 可以使用 Azure 门户Azure PowerShell 创建网络安全组规则。You can create the Network Security Group rules by using the Azure portal or Azure PowerShell. 与创建 Windows 防火墙规则时一样,允许通过 TCP 端口 27017 连接到 MongoDB VM 的虚拟网络接口。As with the Windows Firewall rules, allow TCP port 27017 to the virtual network interface of your MongoDB VM.

Note

TCP 端口 27017 是 MongoDB 使用的默认端口。TCP port 27017 is the default port used by MongoDB. 可以在启动时 mongod.exe 使用 --port 参数手动更改此端口,或者通过某个服务更改此端口。You can change this port by using the --port parameter when starting mongod.exe manually or from a service. 如果更改了端口,请确保在前面的步骤中更新 Windows 防火墙和网络安全组规则。If you change the port, make sure to update the Windows Firewall and Network Security Group rules in the preceding steps.

后续步骤Next steps

本教程已介绍如何在 Windows VM 上安装和配置 MongoDB。In this tutorial, you learned how to install and configure MongoDB on your Windows VM. 现在,可以遵循 MongoDB 文档中的高级主题访问 Windows VM 上的 MongoDB。You can now access MongoDB on your Windows VM, by following the advanced topics in the MongoDB documentation.