配置服务器以侦听特定 TCP 端口Configure a Server to Listen on a Specific TCP Port

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

本主题说明如何使用 SQL Server 配置管理器配置 SQL Server 数据库引擎SQL Server Database Engine 实例以便侦听特定的固定端口。This topic describes how to configure an instance of the SQL Server 数据库引擎SQL Server Database Engine to listen on a specific fixed port by using the SQL Server Configuration Manager. 如果启用, SQL Server 数据库引擎SQL Server Database Engine 的默认实例将侦听 TCP 端口 1433。If enabled, the default instance of the SQL Server 数据库引擎SQL Server Database Engine listens on TCP port 1433. 数据库引擎Database EngineSQL Server CompactSQL Server Compact 的命名实例配置为使用 动态端口Named instances of the 数据库引擎Database Engine and SQL Server CompactSQL Server Compact are configured for dynamic ports. 这意味着启动 SQL ServerSQL Server 服务时,它们将选择可用的端口。This means they select an available port when the SQL ServerSQL Server service is started. 在通过防火墙连接到命名实例时,请配置 数据库引擎Database Engine 以侦听特定端口,以便能够在防火墙中打开相应的端口。When you are connecting to a named instance through a firewall, configure the 数据库引擎Database Engine to listen on a specific port, so that the appropriate port can be opened in the firewall.

由于端口 1433 是 SQL ServerSQL Server 的已知标准,某些组织指定应更改 SQL ServerSQL Server 端口号以增强安全性。Because port 1433 is the known standard for SQL ServerSQL Server, some organizations specify that the SQL ServerSQL Server port number should be changed to enhance security. 这在某些环境中可能很有用。This might be helpful in some environments. 但是,TCP/IP 体系结构允许端口扫描仪查询打开的端口,因此更改端口号并不是一种可靠的安全措施。However, the TCP/IP architecture permits a port scanner to query for open ports, so changing the port number is not considered a robust security measure.

有关默认 Windows 防火墙设置的详细信息以及有关影响数据库引擎、Analysis Services、Reporting Services 和 Integration Services 的 TCP 端口的说明,请参阅 配置 Windows 防火墙以允许 SQL Server 访问For more information about the default Windows firewall settings, and a description of the TCP ports that affect the Database Engine, Analysis Services, Reporting Services, and Integration Services, see Configure the Windows Firewall to Allow SQL Server Access.

提示

选择端口号时,请查看 https://www.iana.org/assignments/port-numbers 以了解分配给特定应用程序的端口号列表。When selecting a port number, consult https://www.iana.org/assignments/port-numbers for a list of port numbers that are assigned to specific applications. 选择一个未分配的端口号。Select an unassigned port number. 更多详细信息,请参阅 TCP/IP 的默认动态端口范围在 Windows Vista 和 Windows Server 2008 中已更改For more information, see The default dynamic port range for TCP/IP has changed in Windows Vista and in Windows Server 2008.

警告

重新启动时,数据库引擎开始侦听新端口。The Database Engine begins listening on a new port when restarted. 但是, SQL ServerSQL Server Browser 服务监视注册表并在配置更改时报告新端口号,即使数据库引擎可能未使用该端口。However the SQL ServerSQL Server Browser service monitors the registry and reports the new port number as soon as the configuration is changed, even though the Database Engine might not be using it. 重新启动数据库引擎可确保一致性并避免连接失败。Restart the Database Engine to ensure consistency and avoid connection failures.

使用 SQL Server 配置管理器Using SQL Server Configuration Manager

为 SQL Server 数据库引擎分配 TCP/IP 端口号To assign a TCP/IP port number to the SQL Server Database Engine

  1. 在 SQL Server 配置管理器的控制台窗格中,展开“SQL Server 网络配置”,展开“<instance name> 的协议”,然后双击“TCP/IP”。In SQL Server Configuration Manager, in the console pane, expand SQL Server Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.

    备注

    如果无法打开 SQL ServerSQL Server Configuration Manager,请参阅 SQL Server Configuration ManagerIf you are having trouble opening SQL ServerSQL Server Configuration Manager, see SQL Server Configuration Manager.

  2. 在“TCP/IP 属性”对话框的“IP 地址”选项卡上,将显示若干个 IP 地址,格式为:IP1IP2...,一直到 IPAllIn the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear in the format IP1, IP2, up to IPAll. 这些 IP 地址中有一个是环回适配器的 IP 地址 (127.0.0.1)。One of these is for the IP address of the loopback adapter, 127.0.0.1. 其他 IP 地址是计算机上的各个 IP 地址。Additional IP addresses appear for each IP Address on the computer. (可能会同时看到 IP 版本 4 和 IP 版本 6 地址。)右键单击每个地址,再单击“属性”,标识要配置的 IP 地址。(You will probably see both IP version 4 and IP version 6 addresses.) Right-click each address, and then click Properties to identify the IP address that you want to configure.

  3. 如果 “TCP 动态端口” 对话框中包含 0,则表示 数据库引擎Database Engine 正在侦听动态端口,请删除 0。If the TCP Dynamic Ports dialog box contains 0, indicating the 数据库引擎Database Engine is listening on dynamic ports, delete the 0.

    TCP_portsTCP_ports

  4. 在“IP_n_ 属性”区域框的“TCP 端口”框中,键入希望此 IP 地址侦听的端口号,然后单击“确定” 。In the IPn Properties area box, in the TCP Port box, type the port number you want this IP address to listen on, and then click OK. 通过逗号分隔,可指定多个端口。Multiple ports may be specified by separating them with a comma.

    备注

    如果“协议”选项卡上的“全部侦听”设置为“是”,则仅使用 IPAll 部分下面的“TCP 端口”和“TCP 动态端口”值,并将完全忽略各 IPn 部分 。If the Listen All setting on the Protocol tab is set to "Yes", then only TCP Port and TCP Dynamic Port values under the IPAll section will be used and individual IPn sections will be ignored in their entirety. 如果“全部侦听”设置为“否”,将忽略 IPAll 部分下面的“TCP 端口”和“TCP 动态端口”设置,而改用各 IPn 部分的“TCP 端口”、“TCP 动态端口”和“已启用”设置 。If the Listen All setting is set to "No", then the TCP Port and TCP Dynamic Port settings under the IPAll section will be ignored and the TCP Port, TCP Dynamic Port, and Enabled settings on the individual IPn sections will be used instead. 每个 IPn 部分的“已启用”设置的默认值为“否”,这样会使 SQL ServerSQL Server 忽略此 IP 地址,即使它已定义端口。Each IPn section has an Enabled setting with a default value of "No" which causes SQL ServerSQL Server to ignore this IP address even if it has a port defined.

  5. 在控制台窗格中,单击“SQL Server 服务”。In the console pane, click SQL Server Services.

  6. 在详细信息窗格中,右键单击“SQL Server(<instance name>)”,然后单击“重新启动”以停止并重新启动 SQL ServerSQL ServerIn the details pane, right-click SQL Server (<instance name>) and then click Restart, to stop and restart SQL ServerSQL Server.

ConnectingConnecting

在配置完 SQL ServerSQL Server 以侦听特定端口后,可以通过下列三种方式使用客户端应用程序连接到特定端口:After you have configured SQL ServerSQL Server to listen on a specific port, there are three ways to connect to a specific port with a client application:

  • 运行服务器上的 SQL ServerSQL Server Browser 服务以按名称连接到 数据库引擎Database Engine 实例。Run the SQL ServerSQL Server Browser service on the server to connect to the 数据库引擎Database Engine instance by name.
  • 在客户端上创建一个别名,指定端口号。Create an alias on the client, specifying the port number.
  • 对客户端进行编程,以便使用自定义连接字符串进行连接。Program the client to connect using a custom connection string.

另请参阅See Also

创建或删除供客户端使用的服务器别名(SQL Server 配置管理器) Create or Delete a Server Alias for Use by a Client (SQL Server Configuration Manager)
SQL Server Browser 服务SQL Server Browser Service