24

I was having a discussion today about Dynamic Ports with one of my co-workers and could use some help clarifying how they work.

First question: If the IPALL TCP Dynmaic Ports setting is a specific number (say 1971) does that signify that you have a static port of 1971 or a dynamic port that is currently 1971 and may change at some point in the future.

enter image description here

Second question: This is the one I'm most curious about. We have an instance that has had the same port (the value in the IPALL TCP Dynmaic Ports setting) for several years through dozens of instance restarts. What actually causes the dynamic port to actually change after an instance restart?

1 Answer 1

22

This KB article : How to configure SQL Server to listen on a specific port will clarify things for you :

Dynamic Port Allocation

If you configure an instance of SQL Server to use dynamic port allocation, and you have not yet restarted the instance of SQL Server, the registry values are set as follows:

TCPDynamicPorts = Blank

TCPPort = 0

However, if you configure an instance of SQL Server to use dynamic port allocation, and you restart the instance of SQL Server, the registry values are set as follows:

TCPDynamicPorts = Current port that is used

TCPPort = Current port that is used

Static Port Allocation :

If you configure an instance of SQL Server to use a static port, and you have not yet restarted the instance of SQL Server, the registry values are set as follows:

TCPDynamicPorts = Last port that is used

TCPPort = New static port to be used after the next restart; new static port that you set by using the Server Network Utility

However, if you configure an instance of SQL Server to use a static port, and you restart the instance of SQL Server, the registry values are set as follows:

TCPDynamicPorts = Blank

TCPPort = New static port that you set by using the Server Network Utility

for your second question -

Every time you start the named SQLServer, it uses the port that is allocated. In case if the port is used by another program, then SQL Server chooses another port at the time of restart i.e. The dynamic port is chosen at first startup, and will generally remain the same through future restarts (stored in the Registry) - but if its used by another program then SQL server will choose a new port. note: For Prod Servers, I use only static ports - security and ease of manageability reasons.

Note: More cools thing to find out :

Check if Dynamic port is being used or not using T-SQL :

SELECT NAME
    ,protocol_desc
    ,type_desc
    ,state_desc
    ,is_admin_endpoint
    ,port
    ,is_dynamic_port
    ,ip_address
FROM sys.tcp_endpoints

enter image description here

You can use netstat -ano to check using cmdline.

Additionally, you can check what got cached earlier in the registry on the client side as to what port it is using to connect to sql server :

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SNI11.0\LastConnect

enter image description here

3
  • Thanks and that is a very useful link! Do you know what causes a dynamic port to change ports? See Second question in my question above. Commented Aug 6, 2013 at 18:38
  • @KennethFisher for your second question - Every time you start the named SQLServer, it uses the port that is allocated. In case if the port is used by another program then SQL Server chooses another port at the time of restart. The dynamic port is chosen at first startup, and will generally remain the same through future restarts (stored in the Registry) - but if its used by another program then SQL server will choose a new port. note: For Prod Servers, I use only static ports - security and ease of manageability reasons.
    – Kin Shah
    Commented Aug 6, 2013 at 18:39
  • Would you be so kind to explain why do you think it is worth to implement completely new service ( SQL server browser) which is working on static port so it may not start if this port is used, just because you want to start SQL server in a case where some other services are using ports you are too clumsy to attach to probably the most expensive software you are using?
    – kakaz
    Commented Oct 15, 2018 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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