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

適用於:SQL Server - 僅限 Windows

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

備註

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

  • 使用最高版本為 SQL Server 2014 (12.x) 的 SQL Server 和 Microsoft SQL Server 資料提供者,預設僅支援 TLS 1.0 和 SSL 3.0。 若在作業系統 SChannel 層進行變更,藉以強制使用不同的通訊協定 (例如 TLS 1.1 或 TLS 1.2),則除非已安裝合適的更新以新增 SQL Server 的 TLS 1.1 及 TLS 1.2 支援,否則與 SQL Server 的連線可能會失敗。 如需詳細資訊,請參閱 KB 3135244。 從 SQL Server 2016 (13.x) 開始,所有 SQL Server 發行版本都包含 TLS 1.2 支援,不需要進一步更新。

使用 SQL Server 組態管理員

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

  2. 在主控台窗格中,選取 執行個體名稱 的< 通訊協定>

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

  4. 在主控台窗格中,選擇 [SQL Server 服務]。

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

注意

如果您有 SQL Server 的具名執行個體,包括 SQL Server Express 版本,您也應該重新啟動 SQL Server Browser 服務。

使用 SQL Server PowerShell

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

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

  2. 從工作列或 [開始] 功能表開啟 Windows PowerShell。

  3. 輸入 Import-Module SqlServer 來匯入 SqlServer 模組。

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

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

    您可以在任何電腦上執行此指令碼,無論是否有安裝 SQL Server。 請確定您已安裝 SqlServer 模組。

    #requires the SqlServer module
    Import-Module SQLServer
    
    $wmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer <#computer_name#>
    
    # 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

注意

如果您有 SQL Server 的具名執行個體,包括 SQL Server Express 版本,您也應該重新啟動 SQL Server Browser 服務。