Yes I read this How to find the port for MS SQL Server 2008?

no luck.

telnet 1433

returns connection failed, so I must specify other port.

I tried to use

netstat -abn

but I don't see sqlservr.exe or something similar on this list.

Why it so difficult to find that port? :/

16

Best Answer


Try this:

USE masterGOxp_readerrorlog 0, 1, N'Server is listening on' GO

http://www.mssqltips.com/sqlservertip/2495/identify-sql-server-tcp-ip-port-being-used/

This is the one that works for me:

SELECT DISTINCT local_tcp_port FROM sys.dm_exec_connections WHERE local_tcp_port IS NOT NULL 

very simple. make a note of the sqlsrvr.exe PID from taskmanagerthen run this command:

netstat -ano | findstr *PID*

it will show TCP and UDP connections of your SQL server (including ports)standard is 1433 for TCP and 1434 for UDP

example :enter image description here

If you can start the Sql Server Configuration Manager > SQL Server Network Configuration > Your instance > TCP/IP > Properties

enter image description here

If you have run "netstat -a -b -n" (from an elevated command prompt) and you don't see "sqlservr.exe" at all then either your SQL Server service is not running or its TCP/IP network library is disabled.

Run SQL Server Configuration Manager (Start | All Programs | Microsoft SQL Server 2008 | Configuration Tools).

Navigate to SQL Server Services.In the right-hand pane look for SQL Server (). Is it stopped? If so, start it.

Navigate to SQL Server Network Configuration (or SQL Server Network Configuration (32-bit) as appropriate) then Protocols for .In the right-hand pane look for "TCP/IP". Is it disabled? If so, enable it, then restart the SQL Server service.

Note that he Instance ID will be MSSQLSERVER for the default instance.

Please also note that you don't have to enable the TCP/IP network library to connect a client to the service. Clients can also connect through the Shared Memory network library (if the client is on the same machine) or the Named Pipes network library.

If you don't want to look in SQL Server Management (sqlservermanager15.msc), then run this query in the database, e.g. from sqlcmd or ssms:

SELECT * FROM [sys].[dm_tcp_listener_states]
listener_idip_addressis_ipv4porttypetype_descstatestate_descstart_time1::1False14330TSQL0ONLINE2021-01-01 00:00:00.0000002127.0.0.1True14330TSQL0ONLINE2021-01-01 00:00:00.000000

Thanks to @vladimir-bashutin for pointing out this one. Here is another one:

SELECT [name],[protocol_desc],[type_desc],[state],[state_desc],[is_admin_endpoint]FROM [master].[sys].[endpoints]
nameprotocol_desctype_descstatestate_descis_admin_endpointTSQL Local MachineSHARED_MEMORYTSQL0STARTEDFalseTSQL Named PipesNAMED_PIPESTSQL0STARTEDFalseTSQL Default TCPTCPTSQL0STARTEDFalseTSQL Default VIAVIATSQL0STARTEDFalse

So now you have the port and protocol. If you don't have access to these system tables, consider using an SSRP client, such as https://github.com/adzm/ssrpc.

Maybe it's not using TCP/IP

Have a look at the SQL Server Configuration Manager to see what protocols it's using.

try once:-

USE masterDECLARE @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 OUTPUTSELECT [Port Number] = @portNumberGO

This is another script that I use:

-- Find Database Port script by Jim Pierce 09/05/2018USE [master]GODECLARE @DynamicportNo NVARCHAR(10);DECLARE @StaticportNo NVARCHAR(10);DECLARE @ConnectionportNo INT;-- Look at the port for the current connectionSELECT @ConnectionportNo = [local_tcp_port]FROM sys.dm_exec_connectionsWHERE session_id = @@spid;-- Look for the port being used in the server's registryEXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE',@key ='Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll',@value_name = 'TcpDynamicPorts',@value = @DynamicportNo OUTPUTEXEC xp_instance_regread @rootkey = 'HKEY_LOCAL_MACHINE',@key ='Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll',@value_name = 'TcpPort',@value = @StaticportNo OUTPUTSELECT [PortsUsedByThisConnection] = @ConnectionportNo,[ServerStaticPortNumber] = @StaticportNo,[ServerDynamicPortNumber] = @DynamicportNoGO
select * from sys.dm_tcp_listener_states 

More there:https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-tcp-listener-states-transact-sql?view=sql-server-2017

In our enterprise I don't have access to MSSQL Server, so I can'r access the system tables.

What works for me is:

  1. capture the network traffic Wireshark (run as Administrator, select Network Interface),while opening connection to server.
  2. Find the ip address with ping
  3. filter with ip.dst == x.x.x.x

The port is shown in the column info in the format src.port -> dst.port

Try to enable the protocol by:Configuration Manger > SQL server Network Configuration > Protocols for MSSQLSERVER > properties of TCP/IP

SQL Server 2000 Programs |MS SQL Server |Client Network Utility |Select TCP_IP then Properties

SQL Server 2005Programs |SQL Server |SQL Server Configuration Manager |Select Protocols for MSSQLSERVER or select Client Protocols and right click on TCP/IP

From PowerShell you can use this to see what port your instance is using:

You can change MSSQLSERVER to your own instance name.

$wmi = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' localhost$tcp = $wmi.ServerInstances['MSSQLSERVER'].ServerProtocols['Tcp']$ipAll = $tcp.IPAddresses | where { $_.Name -eq "IPAll" }write-host ($ipAll.IPAddressProperties.value)

In my case the server was remote and used a named instance. The SQL Browse Service is what will translate that into a port for the client. Just make a connection through SQL Management Studio. Perform an nslookup of the server name to obtain its IP. Then do a:

netstat -ano | findstr {ip}

Should have a few remote connections in the list all using the same port number. If the server is configured to use dynamic ports then this will change.

Perhaps not the best options but just another way is to read the Windows Registry in the host machine, on elevated PowerShell prompt you can do something like this:

#Get SQL instance's Port number using Windows Registry:$instName = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances[0]$tcpPort = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instName\MSSQLServer\SuperSocketNetLib\Tcp").TcpPortWrite-Host The SQL Instance: `"$instName`" is listening on `"$tcpPort`" "TcpPort."

enter image description hereEnsure to run this PowerShell script in the Host Server (that hosts your SQL instance / SQL Server installation), which means you have to first RDP into the SQL Server/Box/VM, then run this code.

HTH