Configure Windows netsh Firewall for MongoDB为MongoDB配置Windows netsh防火墙

On this page本页内容

On Windows Server systems, the netsh program provides methods for managing the Windows Firewall. 在Windows Server系统上,netsh程序提供了管理Windows防火墙的方法。These firewall rules make it possible for administrators to control what hosts can connect to the system, and limit risk exposure by limiting the hosts that can connect to a system.这些防火墙规则使管理员能够控制哪些主机可以连接到系统,并通过限制可以连接到系统的主机来限制风险暴露。

This document outlines basic Windows Firewall configurations. 本文档概述了Windows防火墙的基本配置。Use these approaches as a starting point for your larger networking organization. 使用这些方法作为你的大型网络组织的起点。For a detailed over view of security practices and risk management for MongoDB, see Security.有关MongoDB安全实践和风险管理的详细概述,请参阅安全

Tip提示
See also: 参阅:

Windows Firewall documentation from Microsoft.微软的文档。

Overview概述

Windows Firewall processes rules in an ordered determined by rule type, and parsed in the following order:按规则类型确定的顺序处理规则,并按以下顺序进行分析:

  1. Windows Service HardeningWindows服务强化
  2. Connection security rules连接安全规则
  3. Authenticated Bypass Rules认证绕过规则
  4. Block Rules封锁规则
  5. Allow Rules允许规则
  6. Default Rules默认规则

By default, the policy in Windows Firewall allows all outbound connections and blocks all incoming connections.默认情况下,Windows防火墙中的策略允许所有出站连接,并阻止所有入站连接。

Given the default ports of all MongoDB processes, you must configure networking rules that permit onlyrequired communication between your application and the appropriate exe and exe instances.考虑到所有MongoDB进程的默认端口,您必须配置网络规则,只允许应用程序与相应的exeexe实例之间进行必要的通信。

The configuration changes outlined in this document will create rules which explicitly allow traffic from specific addresses and on specific ports, using a default policy that drops all traffic that is not explicitly allowed.本文档中概述的配置更改将创建明确允许来自特定地址和特定端口的流量的规则,使用默认策略删除所有不明确允许的流量。

You can configure the Windows Firewall with using the netsh command line tool or through a windows application. 您可以使用netsh命令行工具或通过Windows应用程序配置Windows防火墙。On Windows Server 2008 this application is Windows Firewall With Advanced Security in Administrative Tools. 在Windows Server 2008上,此应用程序是Windows防火墙,在管理工具中具有高级安全性。On previous versions of Windows Server, access the Windows Firewall application in the System and Security control panel.在以前版本的Windows Server上,访问系统和安全控制面板中的Windows防火墙应用程序。

The procedures in this document use the netsh command line tool.本文档中的过程使用netsh命令行工具。

Patterns模式

This section contains a number of patterns and examples for configuring Windows Firewall for use with MongoDB deployments. 本节包含许多配置Windows防火墙以用于MongoDB部署的模式和示例。If you have configured different ports using the port configuration setting, you will need to modify the rules accordingly.如果使用端口配置设置配置了不同的端口,则需要相应地修改规则。

Traffic to and from mongod.exe Instances往来mongod.exe实例的流量

This pattern is applicable to all exe instances running as standalone instances or as part of a replica set. 此模式适用于作为独立实例或副本集的一部分运行的所有exe实例。The goal of this pattern is to explicitly allow traffic to the exe instance from the application server.此模式的目标是显式地允许从应用服务器到exe实例的通信。

netsh advfirewall firewall add rule name="Open mongod port 27017" dir=in action=allow protocol=TCP localport=27017

This rule allows all incoming traffic to port 27017, which allows the application server to connect to the exe instance.此规则允许将所有传入流量发送到端口27017,从而允许应用程序服务器连接到exe实例。

Windows Firewall also allows enabling network access for an entire application rather than to a specific port, as in the following example:还允许为整个应用程序而不是特定端口启用网络访问,如以下示例所示:

netsh advfirewall firewall add rule name="Allowing mongod" dir=in action=allow program=" C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"

You can allow all access for a exe server, with the following invocation:通过以下调用,可以允许对exe服务器进行所有访问:

netsh advfirewall firewall add rule name="Allowing mongos" dir=in action=allow program=" C:\Program Files\MongoDB\Server\3.4\bin\mongos.exe"

Traffic to and from mongos.exe Instances往来mongos.exe实例的流量

exe instances provide query routing for sharded clusters. 实例为分片集群提供查询路由。Clients connect to exe instances, which behave from the client's perspective as exe instances. 客户端连接到exe实例,从客户端的角度来看,exe实例的行为与exe实例相同。In turn, the exe connects to all exe instances that are components of the sharded cluster.反过来,exe将连接到作为分片集群组件的所有exe实例。

Use the same Windows Firewall command to allow traffic to and from these instances as you would from the exe instances that are members of the replica set.使用相同的Windows Firewall命令来允许与这些实例之间的通信,就像来自复制集成员的exe实例一样。

netsh advfirewall firewall add rule name="Open mongod shard port 27018" dir=in action=allow protocol=TCP localport=27018

Traffic to and from a MongoDB Config Server进出MongoDB配置服务器的流量

Configuration servers, host the config database that stores metadata for sharded clusters. 配置服务器托管存储分片集群元数据的配置数据库。Each production cluster has three configuration servers, initiated using the mongod --configsvr option. 每个生产集群都有三个配置服务器,使用mongod --configsvr选项启动。[1] Configuration servers listen for connections on port 27019. 配置服务器侦听端口27019上的连接。As a result, add the following Windows Firewall rules to the config server to allow incoming and outgoing connection on port 27019, for connection to the other config servers.因此,将以下Windows防火墙规则添加到配置服务器,以允许在端口27019上进行传入和传出连接,以便连接到其他配置服务器。

netsh advfirewall firewall add rule name="Open mongod config svr port 27019" dir=in action=allow protocol=TCP localport=27019

Additionally, config servers need to allow incoming connections from all of the exe instances in the cluster and all exe instances in the cluster. 此外,配置服务器需要允许来自集群中所有exe实例和集群中所有exe实例的传入连接。Add rules that resemble the following:添加类似以下内容的规则:

netsh advfirewall firewall add rule name="Open mongod config svr inbound" dir=in action=allow protocol=TCP remoteip=<ip-address> localport=27019

Replace <ip-address> with the addresses of the exe instances and the shard exe instances.exe实例和分片exe实例的地址替换<ip address>

[1] You also can run a config server by using the configsvr value for the clusterRole setting in a configuration file.还可以通过在配置文件中使用clusterRole设置的configsvr值来运行配置服务器。

Traffic to and from a MongoDB Shard Server与MongoDB分片服务器之间的通信量

For shard servers, running as mongod --shardsvr[2] Because the default port number is 27018 when running with the shardsvr value for the clusterRole setting, you must configure the following Windows Firewall rules to allow traffic to and from each shard:对于以mongod --shardsvr[2]运行的分片服务器,由于在使用clusterRole设置的shardsvr值运行时,默认端口号为27018,因此必须配置以下Windows防火墙规则,以允许每个分片之间的通信:

netsh advfirewall firewall add rule name="Open mongod shardsvr inbound" dir=in action=allow protocol=TCP remoteip=<ip-address> localport=27018
netsh advfirewall firewall add rule name="Open mongod shardsvr outbound" dir=out action=allow protocol=TCP remoteip=<ip-address> localport=27018

Replace the <ip-address> specification with the IP address of all exe instances. 用所有exe实例的ip地址替换<ip address>规范。This allows you to permit incoming and outgoing traffic between all shards including constituent replica set members to:这允许您允许所有分片(包括组成副本集成员)之间的传入和传出流量:

  • all exe instances in the shard's replica sets.分片副本集中的所有exe实例。
  • all exe instances in other shards. 其他分片中的所有exe实例。[3]

Furthermore, shards need to be able make outgoing connections to:此外,分片需要能够与以下设备建立传出连接:

  • all exe instances.所有exe实例。
  • all exe instances in the config servers.配置服务器中的所有exe实例。

Create a rule that resembles the following, and replace the <ip-address> with the address of the config servers and the exe instances:创建类似于以下内容的规则,并用配置服务器和exe实例的地址替换<ip-address>

netsh advfirewall firewall add rule name="Open mongod config svr outbound" dir=out action=allow protocol=TCP remoteip=<ip-address> localport=27018
[2] You can also specify the shard server option with the shardsvr value for the clusterRole setting in the configuration file. 您还可以在配置文件中为clusterRole设置指定带有shardsvr值的shard server选项。Shard members are also often conventional replica sets using the default port.分片成员通常也是使用默认端口的传统副本集。
[3] All shards in a cluster need to be able to communicate with all other shards to facilitate chunk and balancing operations.集群中的所有分片都需要能够与所有其他分片通信,以促进分块和平衡操作。

Provide Access For Monitoring Systems为监控系统提供访问权限

The mongostat diagnostic tool, when running with the --discover needs to be able to reach all components of a cluster, including the config servers, the shard servers, and the exe instances.mongostat诊断工具在使用--discover运行时,需要能够访问集群的所有组件,包括配置服务器、分片服务器和exe实例。

Changed in version 3.6.在版本3.6中更改

MongoDB 3.6 removes the deprecated HTTP interface and REST API to MongoDB.MongoDB 3.6将不推荐使用的HTTP接口和REST API删除到MongoDB中。

Manage and Maintain Windows Firewall Configurations管理和维护Windows防火墙配置

This section contains a number of basic operations for managing and using netsh. 本节包含管理和使用netsh的一些基本操作。While you can use the GUI front ends to manage the Windows Firewall, all core functionality is accessible is accessible from netsh.虽然可以使用GUI前端管理Windows防火墙,但所有核心功能都可以从netsh访问。

Delete all Windows Firewall Rules删除所有Windows防火墙规则

To delete the firewall rule allowing exe traffic:要删除允许exe通信的防火墙规则,请执行以下操作:

netsh advfirewall firewall delete rule name="Open mongod port 27017" protocol=tcp localport=27017
netsh advfirewall firewall delete rule name="Open mongod shard port 27018" protocol=tcp localport=27018

List All Windows Firewall Rules列出所有Windows防火墙规则

To return a list of all Windows Firewall rules:要返回所有Windows防火墙规则的列表,请执行以下操作:

netsh advfirewall firewall show rule name=all

Reset Windows Firewall重置Windows防火墙

To reset the Windows Firewall rules:要重置Windows防火墙规则,请执行以下操作:

netsh advfirewall reset

Backup and Restore Windows Firewall Rules备份和还原Windows防火墙规则

To simplify administration of larger collection of systems, you can export or import firewall systems from different servers) rules very easily on Windows:为了简化更大系统集合的管理,您可以在Windows上非常轻松地从不同服务器导出或导入防火墙系统)规则:

Export all firewall rules with the following command:使用以下命令导出所有防火墙规则:

netsh advfirewall export "C:\temp\MongoDBfw.wfw"

Replace "C:\temp\MongoDBfw.wfw" with a path of your choosing. "C:\temp\MongoDBfw.wfw"替换为您选择的路径。You can use a command in the following form to import a file created using this operation:可以使用以下形式的命令导入使用此操作创建的文件:

netsh advfirewall import "C:\temp\MongoDBfw.wfw"
←  Configure Linux iptables Firewall for MongoDBImplement Field Level Redaction →