Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are registry settings to enable TCP on SQL Server 2005 and 2008?

I want to programatically enable TCP connections on SQL Server. I believe we can achieve this by modifying registry entries and restarting SQL Server service. What registry should I edit?

like image 608
Rajesh Avatar asked Mar 23 '10 12:03

Rajesh


People also ask

How do I enable enable TCP IP in SQL Server configuration?

Enable TCP/IP in the SQL ServerExpand the Configuration Tools subfolder and right-click on SQL Server Configuration Manager. Click on Run as Administrator. Select SQL Server Network Configuration and click Protocols for SMTKINGDOM. In the right pane, right-click TCP/IP and click Enable.

How do I know if TCP IP protocol is enabled in SQL Server?

Go to the following path to check if it is enabled or not. Open the SQL Server Configuration Manager >> SQL Server NetworkConfiguration >> Protocols for MSSQLSERVER >> Check the status afterNamed Pipes & TCP/IP protocol.

How do I enable the TCP IP protocol in the SQL Server Configuration Manager?

On the Start menu, click All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager. Click SQL Server 2012 Services. Expand the SQL Server 2012 Network Configuration node, and then select Protocols for MSSQLServer (SQL Instance Name) . Right-click TCP/IP, and then click Enable.

How do I enable the TCP IP protocol for a database instance?

In SQL Server Configuration Manager, in the console pane, expand SQL Server Network Configuration. In the console pane, click Protocols for <instance name>. In the details pane, right-click the protocol you want to change, and then click Enable or Disable. In the console pane, click SQL Server Services.


2 Answers

Unless you have a good reason for modifying the registry directly, I suggest you consider using WMI. WMI will provide you with a more version agnostic implementation. WMI can be accessed through the System.Management namespace. You could have code that looks something like this.

public void EnableSqlServerTcp(string serverName, string instanceName)
{
    ManagementScope scope =
            new ManagementScope(@"\\" + serverName +
                                @"\root\Microsoft\SqlServer\ComputerManagement");
    ManagementClass sqlService =
            new ManagementClass(scope,
                                new ManagementPath("SqlService"), null);
    ManagementClass serverProtocol =
            new ManagementClass(scope,
                                new ManagementPath("ServerNetworkProtocol"), null);

    sqlService.Get();
    serverProtocol.Get();

    foreach (ManagementObject prot in serverProtocol.GetInstances())
    {
        prot.Get();
        if ((string)prot.GetPropertyValue("ProtocolName") == "Tcp" &&
            (string)prot.GetPropertyValue("InstanceName") == instanceName)
        {
            prot.InvokeMethod("SetEnable", null);
        }
    }

    uint sqlServerService = 1;
    uint sqlServiceStopped = 1;
    foreach (ManagementObject instance in sqlService.GetInstances())
    {
        if ((uint)instance.GetPropertyValue("SqlServiceType") == sqlServerService &&
            (string)instance.GetPropertyValue("ServiceName") == instanceName)
        {
            instance.Get();
            if ((uint)instance.GetPropertyValue("State") != sqlServiceStopped)
            {
                instance.InvokeMethod("StopService", null);
            }
            instance.InvokeMethod("StartService", null);
        }
    }
}

This code assumes a project reference to System.Management.dll and the following using statement:

using System.Management;

The Sql Protocols blog has an article that goes into some detail as to what the above code is doing.

Note: If a firewall is blocking the port(s) you will still be unable to access the server via TCP.

like image 152
VoidDweller Avatar answered Sep 27 '22 23:09

VoidDweller


Take a look at HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp hive. There are keys like Enabled, ListenOnAllIPs and a list of IP addresses to listen on.

like image 33
Anton Gogolev Avatar answered Sep 28 '22 01:09

Anton Gogolev