sql server端口

This article is useful for a beginner in SQL Server administration and gives insights about the SQL Server Ports, the methods to identify currently configured ports.

本文对于SQL Server管理的初学者很有用,并提供有关SQL Server端口(识别当前配置的端口的方法)的见解。

介绍 (Introduction)

We can define the port as an endpoint of service for communication purposes. It might bind to a particular application or service. Once we install SQL Server, it configures default ports for SQL Server services. Each client application uses the combination of IP addresses and port number to connect to SQL Server.

出于通信目的,我们可以将端口定义为服务的端点。 它可能绑定到特定的应用程序或服务。 安装SQL Server后,它将为SQL Server服务配置默认端口。 每个客户端应用程序使用IP地址和端口号的组合来连接到SQL Server。

We can have two kinds of SQL Server Ports in SQL Server.

SQL Server中可以有两种SQL Server端口。

  1. Static Port: A static port is always bound to a service or application. It does not change due to a service or system restart. By default, SQL Server uses static TCP port number 1433 for the default instance MSSQLSERVER. If you configure SQL Server to use a static port other than the default port, you should communicate it to the clients or application owners to specify in the connection string 静态端口:静态端口始终绑定到服务或应用程序。 由于服务或系统重启,它不会更改。 默认情况下,SQL Server使用静态TCP端口号1433作为默认实例MSSQLSERVER。 如果将SQL Server配置为使用默认端口以外的静态端口,则应将其传达给客户端或应用程序所有者,以在连接字符串中指定
  2. Dynamic Port: You can configure SQL Server to use a dynamic port. If you use dynamic port allocation, you specify port number zero in the network configuration. Once SQL Service restarts, it requests a free port number from the operating system and assigns that port to SQL Server. 动态端口:您可以将SQL Server配置为使用动态端口。 如果使用动态端口分配,请在网络配置中将端口号指定为零。 SQL Service重新启动后,它将向操作系统请求一个空闲端口号,并将该端口分配给SQL Server。

    As you know, Application uses a combination of SQL Server IP address and port number, you might think of a question – How will an application know the port number for connecting to SQL Server?

    如您所知,Application使用SQL Server IP地址和端口号的组合,您可能会想到一个问题– 应用程序如何知道用于连接到SQL Server的端口号?

Once the operating system allocates a dynamic SQL Server Port to SQL Server, it writes that port number in the Windows registry. SQL Server Browser service uses UDP static port 1434. It reads the registry for the assigned TCP port. SQL Server client library connects and sends a UDP message using port 1434. SQL Server Browser service gives back the port number of a specific instance. An application can connect to SQL Server using that dynamic SQL Server port. SQL Server default instance uses the static port; therefore, SQL Server Browser does not return port for the default instance.

操作系统将动态SQL Server端口分配给SQL Server之后,它将在Windows注册表中写入该端口号。 SQL Server Browser服务使用UDP静态端口1434。它读取分配的TCP端口的注册表。 SQL Server客户端库使用端口1434连接并发送UDP消息。SQLServer Browser服务返回特定实例的端口号。 应用程序可以使用该动态SQL Server端口连接到SQL Server。 SQL Server默认实例使用静态端口。 因此,SQL Server浏览器不会为默认实例返回端口。

In most of the cases, SQL Server uses the same dynamic the SQL Server Port upon restart of the SQL Service as well. Suppose you stopped SQL Services and operating system allocated the dynamic port number (previously assigned to SQL) to another service, SQL Server gets another dynamic port assigned to it.

在大多数情况下,SQL Server在重新启动SQL Service时也会使用相同的动态SQL Server端口。 假设您停止了SQL Services,并且操作系统将动态端口号(以前已分配给SQL)分配给了另一个服务,则SQL Server将获得另一个动态端口。

检查SQL Server端口号 (Check SQL Server Port Number )

In this section, we will check a different method to check for the SQL Server Port number.

在本节中,我们将检查另一种方法来检查SQL Server端口号。

方法1:SQL Server配置管理器: (Method 1: SQL Server Configuration Manager: )

It is the most common method to find the SQL Server Port number.

查找SQL Server端口号是最常见的方法。

  • Step 1: 第一步

    Open SQL Server Configuration Manager from the start menu. In case you have multiple SQL Server versions you might get an error message while opening SQL Server Configuration Manager:

    从开始菜单中打开SQL Server配置管理器。 如果您有多个SQL Server版本,则在打开SQL Server Configuration Manager时可能会收到错误消息:

    Cannot connect to WMI provider. You do not have permission or the server is unreachable

    无法连接到WMI提供程序。 您没有权限或服务器无法访问

    In order to fix it, open the administrative command prompt and execute the following command

    为了解决此问题,请打开管理命令提示符并执行以下命令

    > mofcomp "%programfiles(x86)%\Microsoft SQL Server\150\Shared\sqlmgmproviderxpsp2up.mof"
    

    In this command, change the SQL Server version

    在此命令中,更改SQL Server版本

    • SQL 2008 – 100

      SQL 2008 – 100
    • SQL 2012 – 110

      SQL 2012 – 110
    • SQL 2014 – 120

      SQL 2014 – 120
    • SQL2016 – 130

      SQL2016 – 130
    • SQL 2017 – 140

      SQL 2017 – 140
    • SQL 2019 – 150

      SQL 2019 – 150

  • Step 2: 第2步:

    Go to Network Configuration, click the SQL instance for which you want to check SQL port

    转到“网络配置”,单击要检查其SQL端口SQL实例。

  • Step 3: 第三步:

    It opens the protocols list. Right click on TCP/IP and properties

    它打开协议列表。 右键单击TCP / IP和属性

  • Step 4:

    Click on IP Addresses and scroll down to IPAll group. You can see TCP dynamic SQL ports and TCP port in the following screenshot.

    TCP dynamic ports value shows that we are using dynamic ports configuration. The Current assigned TCP dynamic SQL port is 51688.

    If we want to use a static port, remove the dynamic port value and specify a static port in the TCP port. This port number should be unique and not being used by other applications. You need to restart SQL Services to make this change effective

    • Note: You should change the SQL Server Port configuration using the SQL Server Configuration Manager only.
  • 第4步:

    单击IP地址,然后向下滚动到IPAll组。 您可以在以下屏幕截图中看到TCP动态SQL端口和TCP端口。

    TCP动态端口值显示我们正在使用动态端口配置。 当前分配的TCP动态SQL端口为51688。

    如果要使用静态端口,请删除动态端口值并在TCP端口中指定一个静态端口。 此端口号应该是唯一的,并且不能被其他应用程序使用。 您需要重新启动SQL Services才能使此更改生效

    • 注意:您应该仅使用SQL Server配置管理器更改SQL Server端口配置。

方法2:SQL Server错误日志: (Method 2: SQL Server Error Logs:)

SQL Server logs an entry in the SQL Server Error logs on each restart of SQL Services. We can use extended stored procedure xp_readerrorlog to filter the error log using a particular keyword.

每次重新启动SQL Services时,SQL Server都会在SQL Server错误日志中记录一个条目。 我们可以使用扩展存储过程xp_readerrorlog来使用特定关键字过滤错误日志。

The following query uses extended stored procedure xp_readerrorlog to check for the Server is listening on a keyword.

以下查询使用扩展的存储过程xp_readerrorlog来检查服务器是否在侦听关键字。

EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'
 
GO

You get the detail of the SQL Server port on which current SQL Server instance is configured.

您将获得配置当前SQL Server实例SQL Server端口的详细信息。

  • Note: If you are recycling error logs regularly, you might not be able to find SQL Server Port using this method.注意:如果要定期回收错误日志,则可能无法使用此方法查找SQL Server端口。

方法3:使用xp_instance_regread从注册表中获取SQL Server端口: (Method 3: Get SQL Server Port from the registry using xp_instance_regread:)

We can use an extended stored procedure to get the SQL Port value. Execute the following code in SSMS to get a dynamic port for the currently connected SQL instance.

我们可以使用扩展存储过程来获取SQL Port值。 在SSMS中执行以下代码,以获取当前连接SQL实例的动态端口。

DECLARE @portNumber NVARCHAR(10);
EXEC xp_instance_regread 
     @rootkey = 'HKEY_LOCAL_MACHINE', 
     @key = 'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll', 
     @value_name = 'TcpDynamicPorts', 
     @value = @portNumber OUTPUT;
SELECT [Port Number] = @portNumber;
GO

We can use @Value_name=’ TcpPort’ to get detail about the static port.

我们可以使用@ Value_name ='TcpPort'来获取有关静态端口的详细信息。

DECLARE @portNumber NVARCHAR(10);
EXEC xp_instance_regread 
     @rootkey = 'HKEY_LOCAL_MACHINE', 
     @key = 'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll', 
     @value_name = 'TcpPorts', 
     @value = @portNumber OUTPUT;
SELECT [Port Number] = @portNumber;
GO

方法4:使用sys.dm_exec_connections DMV获取SQL Server端口: (Method 4: Get SQL Server Port from using sys.dm_exec_connections DMV:)

We can use sys.dm_exec_connections DMV to check for the TCP port of connected SQL Server instance. We use the @@SPID variable to return the current session SP ID.

我们可以使用sys.dm_exec_connections DMV检查连接SQL Server实例的TCP端口。 我们使用@@ SPID变量返回当前会话的SP ID。

SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = @@SPID;
GO

方法5:使用Windows事件查看器获取SQL Server端口: (Method 5: Get SQL Server Port from using the Windows Event Viewer:)

We can use Windows Event Viewer as well to check for the SQL Server Port details. Once we restart SQL Services, Windows Event Viewer also gets an entry for the SQL Port. Windows Event Viewer might have a large number of events. It is better to filter event logs for event id 26022.

我们也可以使用Windows事件查看器来检查SQL Server端口的详细信息。 一旦我们重新启动SQL Services,Windows Event Viewer也将获得SQL Port的条目。 Windows事件查看器可能包含大量事件。 最好为事件ID 26022过滤事件日志

Go to Run and type eventvwr. It opens the windows event viewer console. Click on – Filter Current Log.

转到运行,然后键入eventvwr。 它打开Windows事件查看器控制台。 单击– 过滤当前日志

Specify the event ID 26022 and click OK

指定事件ID 26022,然后单击“确定”。

The Event viewer shows events for all installed SQL Servers in the servers. You can look for a specific instance and check for the port number.

事件查看器显示服务器中所有已安装SQL Server的事件。 您可以查找特定实例并检查端口号。

You can also select a specific SQL instance in the drop-down list of Event Sources.

您还可以在“事件源”下拉列表中选择特定SQL实例。

In the following screenshot, we can see an entry of SQL Server Port in the Windows event viewer.

在下面的屏幕截图中,我们可以在Windows事件查看器中看到SQL Server端口的条目。

结论 (Conclusion)

In this article, we explored and identify methods to check SQL Server Ports and change the static and dynamic port configuration. If you have any comments or questions, feel free to leave them in the comments below.

在本文中,我们探索并确定了检查SQL Server端口并更改静态和动态端口配置的方法。 如果您有任何意见或疑问,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack/overview-of-sql-server-ports/

sql server端口

更多推荐

sql server端口_SQL Server端口概述