啟用或停用伺服器網路通訊協定

所有網路通訊協定都是由 SQL Server 安裝程式所安裝,但不一定啟用。 本主題描述如何使用 SQL Server 組態管理員 或 PowerShell,在 SQL Server 2014 中啟用或停用伺服器網路協定。 您必須停止並重新啟動資料庫引擎,變更才會生效。

重要

SQL Server Express 安裝期間會在 BUILTIN\Users 群組中新增一個登入。 這可讓電腦上所有經過驗證的使用者以 public 角色成員的身分存取 SQL Server Express 執行個體。 您可以安全移除 BUILTIN\Users 登入,藉此限制擁有個別登入或擁有登入之其他 Windows 群組成員的電腦使用者存取資料庫引擎。

警告

適用於 SQL Server 的 SQL Server 和 Microsoft 資料提供者支援 TLS 1.0 和 SSL 3.0。 如果您以在作業系統 SChannel 層級中進行變更的方式,強制執行不同的通訊協定 (例如 TLS 1.1 或 TLS 1.2),您與 SQL Server 的連線可能會失敗。

本主題內容

使用 SQL Server 組態管理員

若要啟用伺服器網路通訊協定

  1. 在 SQL Server 組態管理員的主控台窗格中,展開 [SQL Server 網路組態]。

  2. 在控制檯窗格中,按兩下 [實例名稱>的<通訊協定]。

  3. 在詳細資料窗格中,以滑鼠右鍵按一下要變更的通訊協定,然後按一下 [啟用] 或 [停用]。

  4. 在主控台窗格中,按一下 [SQL Server 服務]。

  5. 在 [詳細資料] 窗格中,以滑鼠右鍵按一下 [SQL Server (<執行個體名稱>)],然後按一下 [重新啟動],停止並重新啟動 SQL Server 服務。

使用 SQL Server PowerShell

若要使用 PowerShell 來啟用伺服器網路通訊協定

  1. 使用管理員權限來開啟命令提示字元。

  2. 從任務欄啟動 Windows PowerShell 2.0,或依序按兩下 [開始]、[所有程式]、[附屬程式]、[Windows PowerShell] 和 [Windows PowerShell]。

  3. 輸入 以匯入 sqlps 模組 Import-Module "sqlps"

  4. 執行下列陳述式,即可同時啟用 TCP 和具名管道通訊協定。 請將 <computer_name> 更換為執行 SQL Server 的電腦名稱。 如果您要設定具名執行個體,請將 MSSQLSERVER 取代成執行個體名稱。

    若要停用通訊協定,請將 IsEnabled 屬性設定為 $false

    $smo = 'Microsoft.SqlServer.Management.Smo.'  
    $wmi = new-object ($smo + 'Wmi.ManagedComputer').  
    
    # List the object properties, including the instance names.  
    $Wmi  
    
    # Enable the TCP protocol on the default instance.  
    $uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"  
    $Tcp = $wmi.GetSmoObject($uri)  
    $Tcp.IsEnabled = $true  
    $Tcp.Alter()  
    $Tcp  
    
    # Enable the named pipes protocol for the default instance.  
    $uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"  
    $Np = $wmi.GetSmoObject($uri)  
    $Np.IsEnabled = $true  
    $Np.Alter()  
    $Np  
    

若要設定本機電腦的通訊協定

  • 在本機執行此指令碼並設定本機電腦時,SQL Server PowerShell 可以動態判定本機電腦名稱,讓指令碼更具彈性。 若要擷取本機電腦名稱,請將設定 $uri 變數的程式碼行取代成下列程式碼行:

    $uri = "ManagedComputer[@Name='" + (Get-Item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"  
    

若要使用 SQL Server PowerShell 來重新啟動 Database Engine

  • 啟用或停用通訊協定之後,您必須停止並重新啟動資料庫引擎,變更才會生效。 您可以執行下列陳述式,使用 SQL Server PowerShell 停止並啟動預設執行個體。 若要停止並啟動具名執行個體,請將 'MSSQLSERVER' 取代成 'MSSQL$<instance_name>'

    # Get a reference to the ManagedComputer class.  
    CD SQLSERVER:\SQL\<computer_name>  
    $Wmi = (Get-Item .).ManagedComputer  
    # Get a reference to the default instance of the Database Engine.  
    $DfltInstance = $Wmi.Services['MSSQLSERVER']  
    # Display the state of the service.  
    $DfltInstance  
    # Stop the service.  
    $DfltInstance.Stop();  
    # Wait until the service has time to stop.  
    # Refresh the cache.  
    $DfltInstance.Refresh();   
    # Display the state of the service.  
    $DfltInstance  
    # Start the service again.  
    $DfltInstance.Start();  
    # Wait until the service has time to start.  
    # Refresh the cache and display the state of the service.  
    $DfltInstance.Refresh(); $DfltInstance