Find out port of database running in SQL Server 2016 - sql-server

I am wondering how to find out at which port a database running locally in SQL Server 2016 is accepting requests.
The Problem is that the database was set up by a partner, but the port wasn't documented. I tried the default port of 1433, but that did not work.
I also tried using SQL Server Management Studio, but did not find what I was looking for.

You can use this query I have been using:
SELECT DISTINCT local_net_address, local_tcp_port
FROM sys.dm_exec_connections
WHERE local_net_address IS NOT NULL
OR
-- Execute below script if SQL Server is configured with dynamic port number
SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = ##SPID
OR
-- Execute below script if SQL Server is configured with static port number
DECLARE #portNo NVARCHAR(10)
EXEC xp_instance_regread
#rootkey = 'HKEY_LOCAL_MACHINE',
#key =
'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll',
#value_name = 'TcpPort',
#value = #portNo OUTPUT
SELECT [PortNumber] = #portNo
At least one of these will work for you.

Related

Default Instance Missing

I am migrating a server that has SQL Server installed - current server Windows 2012 with SQL Server 2012 and target server windows 2019 with SQL Server 2019. Obviously I am trying to mirror the configuration in current server to ensure as few a changes as possible. In the current server, checking the SQL Server Configuration Manager and checking the registry both visually and using script below there does not appear to be a default "MSSQLSERVER" instance setup.
DECLARE #GetInstances TABLE
( Value nvarchar(100),
InstanceNames nvarchar(100),
Data nvarchar(100))
Insert into #GetInstances
EXECUTE xp_regread
#rootkey = 'HKEY_LOCAL_MACHINE',
#key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
#value_name = 'InstalledInstances'
Select InstanceNames from #GetInstances
However, in the current server I can login to SSMS with SA credentials WITHOUT stipulating an instance - that is just using the database engine name and not database engine name\instance, which, unless I am wrong, usually means it connects to the default MSSQLSERVER instance.
Does this mean there IS a default instance setup on current server somewhere? If so how would I find it?

bcp error with sql server

I have created a stored procedure in sql server 2005 to export data from a query to a network path. The user is a sql user and it is also created in the AD. The server that I am logged in is a remote server, with sysadmin privilege. I am encountering the following error while executing the procedure or even the query:
output
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Native Client]Login timeout expired
NULL
It looks like I have no access to the remote server, while I am executing the query while I am logged in the server. The procedure also executes automatically at 01.30 am. The command that is executed is a bcp command :
DECLARE #SQLCommand [varchar](max)
,#Query [varchar](max)
,#SERVER VARCHAR(1000)
SET #Query = 'SELECT * FROM INFINITY_SYSTEM.[dbo].[CTI_WFM_LILO_View]'
SET #SERVER = '[server_name]'
SET #SQLCommand = 'EXEC xp_cmdshell ''bcp "' + #Query + '" queryout "\\network_path\lilo'+CONVERT(VARCHAR,GETDATE(),112)+'.csv" -Uusername -Ppassword -c -t, -r, -S"'+#SERVER+'"''' --also tried with -T in stand of the -Uusername -Ppassword
EXEC (#SQLCommand)
The command works when I try it from a local DB in my local sqlserver to the same path.
Thanks
Make sure that you can telnet to the remote server. I would also try to connect to the remote server via sqlcmd. You may have a firewall blocking the connection. Also the sql server services will need permissions to the share/directory, in order for you to write a file to it.
Another thing you should consider is using an SSIS package to export the data to the remote share. Generally is not a great practice to use xp_cmdshell to export the data. You can do everything you are trying to do via a simple SSIS package. As an added benefit this also minimizes security risk because you can disable xp_cmdshell.

How to find SQL Server running port?

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? :/
Try this:
USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on'
GO
http://www.mssqltips.com/sqlservertip/2495/identify-sql-server-tcp-ip-port-being-used/
very simple.
make a note of the sqlsrvr.exe PID from taskmanager
then 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 :
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
If you can start the Sql Server Configuration Manager > SQL Server Network Configuration > Your instance > TCP/IP > Properties
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.
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 master
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
This is another script that I use:
-- Find Database Port script by Jim Pierce 09/05/2018
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
In our enterprise I don't have access to MSSQL Server, so I can'r access the system tables.
What works for me is:
capture the network traffic Wireshark (run as Administrator, select Network Interface),while opening connection to server.
Find the ip address with ping
filter with ip.dst == x.x.x.x
The port is shown in the column info in the format src.port -> dst.port
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
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_id
ip_address
is_ipv4
port
type
type_desc
state
state_desc
start_time
1
::1
False
1433
0
TSQL
0
ONLINE
2021-01-01 00:00:00.000000
2
127.0.0.1
True
1433
0
TSQL
0
ONLINE
2021-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]
name
protocol_desc
type_desc
state
state_desc
is_admin_endpoint
TSQL Local Machine
SHARED_MEMORY
TSQL
0
STARTED
False
TSQL Named Pipes
NAMED_PIPES
TSQL
0
STARTED
False
TSQL Default TCP
TCP
TSQL
0
STARTED
False
TSQL Default VIA
VIA
TSQL
0
STARTED
False
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.
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 2005
Programs |
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").TcpPort
Write-Host The SQL Instance: `"$instName`" is listening on `"$tcpPort`" "TcpPort."
Ensure 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

xp_regread SQL Server 2012

I am trying to use xp_regread on a new SQL Server 2012 server I have just installed.
Previously, I have used a query like the following on SQL Server 2008 servers to get the account running the current instance:
declare #regResult varchar(20)
exec master..xp_regread #rootKey = 'HKEY_LOCAL_MACHINE',
#key = 'SYSTEM\CurrentControlSet\Services\MSSQLSERVER',
#value_name = 'ObjectName',
#value = #regResult OUTPUT
select #regResult
However, I now get the following error when using the same query on SQL Server 2012:
Msg 22001, Level 15, State 0, Line 0
Error executing Read extended stored procedure: Invalid Parameter
I am assuming xp_regread has changed in SQL Server 2012. Does anyone know how it changed?
I am also open to a different query that doesn't use an extended stored procedure to get the service account running the instance.
How about
select * from sys.dm_server_services
http://msdn.microsoft.com/en-us/library/hh204542.aspx

Get current connection protocol in SQL 2000

I'm currently using SQL 2000 (it's a vendor server - don't ask...), and I'm wondering if there's a way to detect the connection protocol of connected clients. For SQL 2005+, I use:
select net_transport from sys.dm_exec_connections
where session_id = ##spid
But SQL2000 lacks this dynamic view. Anybody know the equivalent function on SQL2000?
SELECT net_library FROM sysprocesses WHERE spid = ##SPID
You can also sys.sysprocesses in SQL Server 2005/2008. It may be deprecated but there is no equivalent in after SQL 2000 for functionality, says MS Connect 144515

Resources