Enable tcp\ip remote connections to sql server express already installed database with code or script(query)

前端 未结 2 881
南笙
南笙 2020-11-28 05:53

I am deploying sql express with my application. I will like that database engine to accept remote connections. I know how to configure that manual by launching the sql serve

相关标签:
2条回答
  • 2020-11-28 06:21

    I recommend to use SMO (Enable TCP/IP Network Protocol for SQL Server). However, it was not available in my case.

    I rewrote the WMI commands from Krzysztof Kozielczyk to PowerShell.

    # Enable TCP/IP
    
    Get-CimInstance -Namespace root/Microsoft/SqlServer/ComputerManagement10 -ClassName ServerNetworkProtocol -Filter "InstanceName = 'SQLEXPRESS' and ProtocolName = 'Tcp'" |
    Invoke-CimMethod -Name SetEnable
    
    # Open the right ports in the firewall
    New-NetFirewallRule -DisplayName 'MSSQL$SQLEXPRESS' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 1433
    
    # Modify TCP/IP properties to enable an IP address
    
    $properties = Get-CimInstance -Namespace root/Microsoft/SqlServer/ComputerManagement10 -ClassName ServerNetworkProtocolProperty -Filter "InstanceName='SQLEXPRESS' and ProtocolName = 'Tcp' and IPAddressName='IPAll'"
    $properties | ? { $_.PropertyName -eq 'TcpPort' } | Invoke-CimMethod -Name SetStringValue -Arguments @{ StrValue = '1433' }
    $properties | ? { $_.PropertyName -eq 'TcpPortDynamic' } | Invoke-CimMethod -Name SetStringValue -Arguments @{ StrValue = '' }
    
    # Restart SQL Server
    
    Restart-Service 'MSSQL$SQLEXPRESS'
    
    0 讨论(0)
  • I tested below code with SQL Server 2008 R2 Express and I believe we should have solution for all 6 steps you outlined. Let's take on them one-by-one:

    1 - Enable TCP/IP

    We can enable TCP/IP protocol with WMI:

    set wmiComputer = GetObject( _
        "winmgmts:" _
        & "\\.\root\Microsoft\SqlServer\ComputerManagement10")
    set tcpProtocols = wmiComputer.ExecQuery( _
        "select * from ServerNetworkProtocol " _
        & "where InstanceName = 'SQLEXPRESS' and ProtocolName = 'Tcp'")
    
    if tcpProtocols.Count = 1 then
        ' set tcpProtocol = tcpProtocols(0)
        ' I wish this worked, but unfortunately 
        ' there's no int-indexed Item property in this type
    
        ' Doing this instead
        for each tcpProtocol in tcpProtocols
            dim setEnableResult
                setEnableResult = tcpProtocol.SetEnable()
                if setEnableResult <> 0 then 
                    Wscript.Echo "Failed!"
                end if
        next
    end if
    

    2 - Open the right ports in the firewall

    I believe your solution will work, just make sure you specify the right port. I suggest we pick a different port than 1433 and make it a static port SQL Server Express will be listening on. I will be using 3456 in this post, but please pick a different number in the real implementation (I feel that we will see a lot of applications using 3456 soon :-)

    3 - Modify TCP/IP properties enable a IP address

    We can use WMI again. Since we are using static port 3456, we just need to update two properties in IPAll section: disable dynamic ports and set the listening port to 3456:

    set wmiComputer = GetObject( _
        "winmgmts:" _
        & "\\.\root\Microsoft\SqlServer\ComputerManagement10")
    set tcpProperties = wmiComputer.ExecQuery( _
        "select * from ServerNetworkProtocolProperty " _
        & "where InstanceName='SQLEXPRESS' and " _
        & "ProtocolName='Tcp' and IPAddressName='IPAll'")
    
    for each tcpProperty in tcpProperties
        dim setValueResult, requestedValue
    
        if tcpProperty.PropertyName = "TcpPort" then
            requestedValue = "3456"
        elseif tcpProperty.PropertyName ="TcpDynamicPorts" then
            requestedValue = ""
        end if
    
        setValueResult = tcpProperty.SetStringValue(requestedValue)
        if setValueResult = 0 then 
            Wscript.Echo "" & tcpProperty.PropertyName & " set."
        else
            Wscript.Echo "" & tcpProperty.PropertyName & " failed!"
        end if
    next
    

    Note that I didn't have to enable any of the individual addresses to make it work, but if it is required in your case, you should be able to extend this script easily to do so.

    Just a reminder that when working with WMI, WBEMTest.exe is your best friend!

    4 - Enable mixed mode authentication in sql server

    I wish we could use WMI again, but unfortunately this setting is not exposed through WMI. There are two other options:

    1. Use LoginMode property of Microsoft.SqlServer.Management.Smo.Server class, as described here.

    2. Use LoginMode value in SQL Server registry, as described in this post. Note that by default the SQL Server Express instance is named SQLEXPRESS, so for my SQL Server 2008 R2 Express instance the right registry key was HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQLServer.

    5 - Change user (sa) default password

    You got this one covered.

    6 - Finally (connect to the instance)

    Since we are using a static port assigned to our SQL Server Express instance, there's no need to use instance name in the server address anymore.

    SQLCMD -U sa -P newPassword -S 192.168.0.120,3456
    

    Please let me know if this works for you (fingers crossed!).

    0 讨论(0)
提交回复
热议问题