64

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

4

7 Answers 7

96
  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

7
  • This query can also be executed in sqlcmd. Dec 11, 2014 at 4:58
  • 24
    Didn't return any rows.
    – trilawney
    Aug 3, 2016 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 4, 2016 at 7:30
  • 1
    Thanks Punter. That was helpful. I figured it out using netstat -abn
    – trilawney
    Aug 4, 2016 at 12:40
  • 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, dev.mysql.com/doc/refman/8.0/en/show-variables.html
    – Punter015
    Aug 14, 2018 at 8:39
42

You can also use this query

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

Source : sqlauthority blog

4
  • This should be the answer. Worked for me. Thanks Sep 10, 2021 at 19:01
  • No rows for me SQL Server Express 15.0.2000 Sep 1, 2022 at 6:39
  • This works on SQL Server Express 2019 Dec 1, 2022 at 20:26
  • Worked perfectly for me!
    – Ivo Sturm
    Sep 14 at 9:51
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

2
  • 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. Mar 29, 2015 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? Jan 31, 2022 at 20:55
23

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

SQL Server Configuration

1
  • What does Enabled = No mean? I have values defined from IP1 to IP8, but for all of them Enabled = No is set. Dec 10, 2014 at 12:18
9

This query works for me:

SELECT DISTINCT 
    local_tcp_port 
FROM sys.dm_exec_connections 
WHERE local_tcp_port IS NOT NULL 
1
  • 1
    just blank for me
    – greendino
    Sep 15, 2022 at 7:45
6

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

netstat -ntpl
2
  • 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 9, 2014 at 7: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! May 10, 2017 at 19:27
6

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
} 
1
  • This worked great. Would be nice if it could somehow include the instance name.
    – Sean Gough
    Feb 28 at 2:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.