63

I Install SQL server in my system and I have to check on which port number SQL is working in my system

Phil Murray
  • 6,396
  • 9
  • 45
  • 95
Ankit jain
  • 4,198
  • 4
  • 19
  • 24

7 Answers7

95
  1. Open SQL Server Management Studio
  2. Connect to the database engine for which you need the port number
  3. Run the below query against the database

    select distinct local_net_address, local_tcp_port from sys.dm_exec_connections where local_net_address is not null

The above query shows the local IP as well as the listening Port number

Punter015
  • 1,718
  • 10
  • 13
  • This query can also be executed in `sqlcmd`. – Ozair Kafray Dec 11 '14 at 04:58
  • 24
    Didn't return any rows. – trilawney Aug 03 '16 at 13:07
  • 3
    @trilawney If you are connecting to the database locally it won't return any rows. You will have to connect to the database remotely. When you connect locally it uses shared memory to connect, when you connect remotely it connects through TCP. – Punter015 Aug 04 '16 at 07:30
  • 1
    Thanks Punter. That was helpful. I figured it out using netstat -abn – trilawney Aug 04 '16 at 12:40
  • @Punter015.. my sys is not having this table. Does it have any other name as well. M getting error with the query: "mysql> select distinct local_net_address, local_tcp_port from sys.dm_exec_connections where local_net_address is not null; ERROR 1146 (42S02): Table 'sys.dm_exec_connections' doesn't exist" – Yashi Srivastava Aug 14 '18 at 06:19
  • 1
    @YashiSrivastava, This query is for Microsoft SQL Server. You can use "SHOW VARIABLES WHERE Variable_name = 'port'" for MySQL. For more information on show variables refer, https://dev.mysql.com/doc/refman/8.0/en/show-variables.html – Punter015 Aug 14 '18 at 08:39
  • This is the chicken! We couldn't work out why we couldn't connect to a co-worker's SQL Server on his laptop. Turned out it's running in shared memory not TCP/IP. doh! – Becky York Jan 08 '21 at 14:20
41
  1. Open Run in your system.

  2. Type %windir%\System32\cliconfg.exe

  3. Click on ok button then check that the "TCP/IP Network Protocol Default Value Setup" pop-up is open.

  4. Highlight TCP/IP under the Enabled protocols window.

  5. Click the Properties button.

  6. Enter the new port number, then click OK.

enter image description here

plr108
  • 1,201
  • 11
  • 16
Ankit jain
  • 4,198
  • 4
  • 19
  • 24
  • This caused my SQL Server Management Studio to malfunction (due to a null number bug in Visual Studio 2010). It was able to connect and list tables, as well as open an edit 200 rows view, but failed to open a new query window. – Thor Hovden Mar 29 '15 at 20:42
  • @Ankit Jain: Can you explain what this does? If this is changing the server config, then what are the side-effects of the said change? – Chintan Pathak Jan 31 '22 at 20:55
40

You can also use this query

USE MASTER GO xp_readerrorlog 0, 1, N'Server is listening on' GO

Source : sqlauthority blog

Sunil Agarwal
  • 4,097
  • 5
  • 44
  • 80
22

Visually you can open "SQL Server Configuration Manager" and check properties of "Network Configuration":

SQL Server Configuration

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
8

This query works for me:

SELECT DISTINCT 
    local_tcp_port 
FROM sys.dm_exec_connections 
WHERE local_tcp_port IS NOT NULL 
Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62
Unsal Aslan
  • 101
  • 1
  • 5
5

To check all the applications listening on all ports, there is command:

netstat -ntpl
Paco
  • 4,520
  • 3
  • 29
  • 53
  • 5
    On Windows Server 2008 and Windows 7 I need to use `netstat -ap TCP` to display listening (and connected) TCP ports – Paul B. Apr 09 '14 at 07:37
  • This is the only way I could find to get the port number from the client side, without the ability to RDP into the server, or run administrative stored procs. Thanks so much! – EverPresent May 10 '17 at 19:27
5

PowerShell solution that shows all of the instances on the host as well as their incoming traffic addresses. The second bit might be helpful if all you know is the DNS:

ForEach ($SQL_Proc in Get-Process | Select-Object -Property ProcessName, Id | Where-Object {$_.ProcessName -like "*SQL*"})
{
    Get-NetTCPConnection | `
     Where-Object {$_.OwningProcess -eq $SQL_Proc.id} | `
      Select-Object -Property `
                                @{Label ="Process_Name";e={$SQL_Proc.ProcessName}}, `
                                @{Label ="Local_Address";e={$_.LocalAddress + ":" + $_.LocalPort }},  `
                                @{Label ="Remote_Address";e={$_.RemoteAddress + ":" + $_.RemotePort}}, State | `
      Format-Table
} 
Bartosz X
  • 2,620
  • 24
  • 36