# Kiểm tra port SQL sử dụng

## DÙNG SQL SERVER CONFIGURATION MANAGER

<figure><img src="/files/otl1EQ26GImM5gs2ldy0" alt=""><figcaption><p>Kiểm tra port</p></figcaption></figure>

## DÙNG LỆNH SQL

```sql
------------------------------- Lệnh 1 ------------------------------------------
USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on', N'any', NULL, NULL, N'asc' 
GO

------------------------------- Lệnh 2 ------------------------------------------
Use master

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


--------------------------------- Lệnh 3 --------------------------------------
USE [master]
GO

DECLARE @DynamicportNo NVARCHAR(10);
DECLARE @StaticportNo NVARCHAR(10);
DECLARE @ConnectionportNo INT;

-- Look at the port for the current connection
SELECT @ConnectionportNo = [local_tcp_port]
 FROM sys.dm_exec_connections
    WHERE session_id = @@spid;

-- Look for the port being used in the server's registry
EXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE'
                        ,@key =
                         'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll'
                        ,@value_name = 'TcpDynamicPorts'
                        ,@value = @DynamicportNo OUTPUT

EXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE'
                        ,@key =
                         'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll'
                        ,@value_name = 'TcpPort'
                        ,@value = @StaticportNo OUTPUT

SELECT [PortsUsedByThisConnection] = @ConnectionportNo
      ,[ServerStaticPortNumber] = @StaticportNo
      ,[ServerDynamicPortNumber] = @DynamicportNo
GO

```

Tham khảo tài liệu:

```
// Link tài liệu:
https://stackoverflow.com/questions/12297475/how-to-find-sql-server-running-port
https://www.mssqltips.com/sqlservertip/2495/identify-sql-server-tcp-ip-port-being-used/
https://www.sysadmit.com/2016/03/mssql-ver-puerto-de-una-instancia.html
https://blog.devart.com/sql-server-ports.html
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kythuat.dtechvn.com/sql-server/kiem-tra-port-sql-su-dung.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
