How to find SQL Server running port? - sql-server

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

Related

Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open server 'azurserver' requested by the login

I am connecting to Azure from SQL Server.
Task: Exporting Azure SQL Database data using bcp from SQL Server 2008 R2.
Step 1: Added IP Address in firewall settings in Azure.
Step 2: Running following script from SQL Server 2008 R2:
EXEC sp_configure N'show advanced options', 1
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure N'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE;
DECLARE #SQL NVARCHAR(MAX) = ''
SET #SQL = N'Execute xp_cmdshell ''bcp "SELECT * FROM Table;" queryout "D:\AzureTableData.txt" -c -t#_# -S azure.database.windows.net -d DatabaseName -U user1#azure.database.windows.net -P *****"''';
NOTE: IP address is not static at my system and because of this reason, I keep getting an error:
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open server 'azurserver' requested by the login. Client with IP address 'XXX.XXX.XXX.XX' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect.
I can't configure firewall settings every time at Azure Portal.
You mean that Add client IP doesn't work for you, do you?
If you want to connect to the Azure SQL database for one time, you can set the firewall IP range from 0.0.0.0 to 255.255.255.255.
But for security reasons, we don't suggest to do that.
Azure SQL database could not set the dynamic IP firewall for now.
Hope this helps.

DB2 can't connect to the server

I am tring to set up a db2 server on a Windows 7 machine and I cant connect to database from remote, only from local. I think I might miss some configuration.
1) I turned off the firewall
2) my database information is:
Database 2 entry:
Database alias = SAMPLE
Database name = SAMPLE
Local database directory = C:
Database release level = f.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Part of the output from netstat -a command is:
TCP [::]:49155 WIN-whatever:0 LISTENING
TCP [::]:49164 WIN-whatever:0 LISTENING
TCP [::]:49165 WIN-whatever:0 LISTENING
TCP [::]:50000 WIN-whatever:0 LISTENING
From the client if I ping my server it does not respond
Also if I scan it with nmap -sV <server_ip_address> it doesn't return me open ports and services which use it
From the client, to connect, I run the following commands:
db2 list node directory
SQL1027N The node directory cannot be found.
catalog tcpip node <i am not sure what does here> remote <server_ip_address> server <port_number>
no output
this implies that something is wrong. I tried couple of things for node and nothing works
I am have no idea what is going on and how to fix it.
Thank you for help
First of all, the fact that you cannot ping the server and nmap doesn't see the open ports means your problem, at least at this point, lies outside DB2. Once you fix your network issues, here's the procedure you can follow to set up remote connectivity to a DB2 database via TCP/IP.
1) Optional. You can use either a service name or a port number to configure the DB2 instance. If you want to use the service name, on the server add a line to /etc/services similar to db2c_db2inst1 50000/tcp, which assigns the service name "db2c_db2inst1" to the port 50000.
2) On the server update the DB2 instance configuration: db2 update dbm cfg using svcename db2c_db2inst1 (or db2 update dbm cfg using svcename 50000 if not using the service name). To verify: db2 get dbm cfg | grep SVCENAME.
3) On the server update the DB2 registry variable: db2set DB2COMM=TCPIP. To verify: db2set DB2COMM.
4) On the server restart the instance: issue db2stop force, then db2start.
5) On the remote client, assuming the DB2 client software is installed, issue db2 catalog tcpip node whatever remote <your server IP address> service <your DB2 port number>. Note that the node name ("whatever" in this example) is limited to 8 ASCII characters.
6) On the remote client issue db2 catalog database <your database name> at node whatever, referring to the node name ("whatever") that you defined in step 5.
7) On the remote client verify connectivity: db2 connect to <your database name> user <user name> using <password>
Try to do a telnet on the instance port to check that everything is right from the client to the server
telnet DB2-Server-name 50000
Where DB2-Server-name is the server name or IP address. Make sure you can get the server.
50000 is the most often DB2 port, but you have to check the configuration.
Once you get connection to the server, you can configure the node with the previous answer.

Unable to access an instance of SQL Server 2008 R2 remotely

A very mysterious problem coming up :P
I have a server configured with a static IP. I have installed SQL Server 2008 R2 with additional instance (ITAPP). Now When I use the IP to access the SQL Server following things happens:
Client Machine:
sqlcmd -S XXX.XXX.XXX.XXX -U sa -P mypass
Connected Successfully....
But when I use:
sqlcmd -S XXX.XXX.XXX.XXX\ITAPP -U sa -P mypass
HResult 0xFFFFFFFF, Level 16, State 1 SQL Network Interfaces: Error
Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: 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..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
Even on the same machine (where the SQL Server is installed) using SQL Server Management Studio. I have put off my firewall on both machines, even allow all the protocols for ITAPP (Shared Memory, Named Pipes, TCP/IP), also set Allow Remote connection to true.
One thing more when I use 127.0.0.1\SQLITRAX to connect on server machine it connects Immediately.
Please help me out from this mess :)
maybe it's because it's a named instance on a remote server.
Named instance don't use the SQL Server standard Tcp-port 1433, only the default (unnamed) instance use the 1433 port.
Any other "named" instance simply listen on another port.
So you should check in the SQL-Server configurator, on which TCP port is listening, and then tell the client to connect to this port.
Say that your named instance is listening to 12345 port, then the client should connect using the following command
sqlcmd -S XXX.XXX.XXX.XXX,12345 -U sa -P mypass
when you specify the TCP port, then you don't need the name of the instance.
In my understanding the name of the instance is a way for SQL Server services to find the TCP port that this named instance is listening on.
But for this to work your client need to be able to access those other services that resolve the instance name of SQL Server (maybe it's the Sql server Agent / SQL Server browser, but I'm not sure)
Update
Here is a screenshot that show where to set the TPC port for a SQL Server named instance.
so, on which TCP port is your named instance of SQL Server listening?
The SQL Server Browser service needs to be accessible to be able to connect to named instances.
See this article for more info on what it does and which ports to open on your firewall
This technet article provides a handy script to open up all required ports

How to find the port for MS SQL Server 2008?

I am running MS SQL Server 2008 on my local machine. I know that the default port is 1433 but some how it is not listening at this port. The SQL is an Express edition.
I have already tried the log, SQL Server Management Studio, registry, and extended stored procedure for finding the port. But, I could not find it. Please help me. Thanks.
Click on Start button in Windows.
Go to All Programs -> Microsoft SQL Server 2008 -> Configuration Tools -> SQL Server Configuration Manager
Click on SQL Native Client 10.0 Configuration -> Client Protocols -> TCP/IP
double click ( Right click select Properties ) on TCP/IP.
You will find Default Port 1433.
Depending on connection, the port number may vary.
You could also look with a
netstat -abn
It gives the ports with the corresponding application that keeps them open.
Edit: or TCPView.
Here are 5 methodes i found:
Method 1: SQL Server Configuration Manager
Method 2: Windows Event Viewer
Method 3: SQL Server Error Logs
Method 4: sys.dm_exec_connections DMV
Method 5: Reading registry using xp_instance_regread
Method 4: sys.dm_exec_connections DMV
I think this is almost the easiest way...
DMVs return server state that can be used to monitor SQL Server Instance. We can use sys.dm_exec_connections DMV to identify the port number SQL Server Instance is listening on using below T-SQL code:
SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = ##SPID
GO
Result Set:
local_tcp_port
61499
(1 row(s) affected)
Method 1: SQL Server Configuration Manager
Step 1. Click Start > All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager
Step 2. Go to SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for
Step 3. Right Click on TCP/IP and select Properties
Step 4. In TCP/IP Properties dialog box, go to IP Addresses tab and scroll down to IPAll group.
If SQL Server if configured to run on a static port it will be available in TCP Port textbox, and if it is configured on dynamic port then current port will be available in TCP Dynamic Ports textbox. Here my instance is listening on port number 61499.
The other methods you can find here: http://sqlandme.com/2013/05/01/sql-server-finding-tcp-port-number-sql-instance-is-listening-on/
I came across this because I just had problems creating a remote connection and couldn't understand why setting up 1433 port in firewall is not doing the job. I finally have the full picture now, so I thought I should share.
First of all is a must to enable "TCP/IP" using the SQL Server Configuration Manager under Protocols for SQLEXPRESS!
When a named instance is used ("SQLExpress" in this case), this will listen on a dynamic port. To find this dynamic port you have couple of options; to name a few:
checking ERRORLOG of SQL Server located in '{MS SQL Server Path}\{MS SQL Server instance name}\MSSQL\Log' (inside you'll find a line similar to this: "2013-07-25 10:30:36.83 Server Server is listening on [ 'any' <ipv4> 51118]" --> so 51118 is the dynamic port in this case.
checking registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\{MSSQL instance name}\MSSQLServer\SuperSocketNetLib\Tcp\IPAll, for my case TcpDynamicPorts=51118.
Edit: {MSSQL instance name} is something like: MSSQL10_50.SQLEXPRESS, not only SQLEXPRESS
Of course, allowing this TCP port in firewall and creating a remote connection by passing in: "x.x.x.x,51118" (where x.x.x.x is the server ip) already solves it at this point.
But then I wanted to connect remotely by passing in the instance name (e.g: x.x.x.x\SQLExpress). This is when SQL Browser service comes into play. This is the unit which resolves the instance name into the 51118 port. SQL Browser service listens on UDP port 1434 (standard & static), so I had to allow this also in server's firewall.
To extend a bit the actual answer: if someone else doesn't like dynamic ports and wants a static port for his SQL Server instance, should try this link.
In the ERROLOG log for a line like below. If you don't see it the SQL Server isn't enabled for remote access, or it is just not via TCP. You can change this via the SQL Server Configuration Manager.
Server is listening on [ 192.128.3.2 <ipv4> 1433].
Try this (requires access to sys.dm_exec_connections):
SELECT DISTINCT
local_tcp_port
FROM sys.dm_exec_connections
WHERE local_tcp_port IS NOT NULL
I solved the problem by enabling the TCP/IP using the SQL Server Configuration Manager under Protocols for SQLEXPRESS2008, i restarted the service and now the "Server is listening on" shows up in the ERRORLOG file
I use the following script in SSMS
SELECT
s.host_name
,c.local_net_address
,c.local_tcp_port
,s.login_name
,s.program_name
,c.session_id
,c.connect_time
,c.net_transport
,c.protocol_type
,c.encrypt_option
,c.client_net_address
,c.client_tcp_port
,s.client_interface_name
,s.host_process_id
,c.num_reads as num_reads_connection
,c.num_writes as num_writes_connection
,s.cpu_time
,s.reads as num_reads_sessions
,s.logical_reads as num_logical_reads_sessions
,s.writes as num_writes_sessions
,c.most_recent_sql_handle
FROM sys.dm_exec_connections AS c
INNER JOIN sys.dm_exec_sessions AS s
ON c.session_id = s.session_id
--filter port number
--WHERE c.local_tcp_port <> 1433
USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on', 'any', NULL, NULL, N'asc'
GO
[Identify Port used by Named Instance of SQL Server Database Engine by Reading SQL Server Error Logs]
You can use this two commands: tasklist and netstat -oan
Tasklist.exe is like taskmgr.exe but in text mode.
With tasklist.exe or taskmgr.exe you can obtain a PID of sqlservr.exe
With netstat -oan, it shows a connection PID, and you can filter it.
Example:
C:\>tasklist | find /i "sqlservr.exe"
sqlservr.exe 1184 Services 0 3.181.800 KB
C:\>netstat -oan | find /i "1184"
TCP 0.0.0.0:1280 0.0.0.0:0 LISTENING 1184
In this example, the SQLServer port is 1280
Extracted from: http://www.sysadmit.com/2016/03/mssql-ver-puerto-de-una-instancia.html
This may also be done via a port scan, which is the only possible method if you don't have admin access to a remote server.
Using Nmap (http://nmap.org/zenmap/) to do an "Intense TCP scan" will give you results like this for all instances on the server:
[10.0.0.1\DATABASE]
Instance name: DATABASE
Version: Microsoft SQL Server 2008 R2 RTM
Product: Microsoft SQL Server 2008 R2
Service pack level: RTM
TCP port: 49843
Named pipe: \\10.0.0.1\pipe\MSSQL$DATABASE\sql\query
Important note: To test with query analyzer or MS SQL Server Management Studio you must form your server name and port differently than you would normally connect to a port, over HTTP for instance, using a comma instead of a colon.
Management Studio Server Name: 10.0.0.1,49843
Connection String: Data Source=10.0.0.1,49843
however
JDBC Connection String: jdbc:microsoft:sqlserver://10.0.0.1:49843;DatabaseName=DATABASE
This works for SQL Server 2005 - 2012. Look for event id = 26022 in the error log under applications. That will show the port number of sql server as well as what ip addresses are allowed to access.
In addition to what is listed above, I had to enable both TCP and UDP ports for SQLExpress to connect remotely. Because I have three different instances on my development machine, I enable 1430-1435 for both TCP and UDP.

SQL connection string

website needs to go live which i have placed on client server.i have gven all permissions for IIS.in web.config file i have also given SQL connection string as :
<appSettings>
<add key="ConnectionString" value="Data Source=USER\SQLEXPRESS;Initial Catalog=people_metrix;User ID=MyUser;Password=abc123"/> </appSettings>
In sql server 2005 have allowed remote connections(TCP/IP and named pipes).
but am not able to login to webasite.Username and password exist in database.
In server,firewall is on and there is no IP address(Obtains IP adress automatically).Is Login not happening due to these reasons ?
Please don't EVER use 'sa' in a connection string. EVER!
First try connecting using SQL Server Management studio with your connection string credentials (and not sa!).
Does the firewall on SQl sServer machine allow traffic on port 1433?
To allow connections to SQL Server by TCP port, perform the following steps.
Click Start | Control Panel | Network Connections
Right click on the network adapter that services inbound connections to SQL Server and select Properties
Select Advanced | Windows Firewall | Settings
Select Exceptions | Add Port
Under Name put "SQL Server" and under Port Number select 1433. Use TCP as the protocol.
Click OK to close out all dialogs.
If you're using anything other than standard TCP/IP to access SQL Server, you'll need to enable different ports. To allow RPC over Named Pipes instead of TCP, open port 445 in the same way that I described above.
See this MS support article for configuring remote connections: How to configure SQL Server 2005 to allow remote connections
You might check out connectionstrings.com
It has several connection string examples.

Resources