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

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

安全性注意事項安全性注意事項

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

本主題內容

  • 若要使用下列項目來啟用或停用伺服器網路通訊協定:

    SQL Server 組態管理員

    PowerShell

使用 SQL Server 組態管理員

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

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

  2. 在主控台窗格中,按一下 [<instance name> 的通訊協定]

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

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

  5. 在詳細資料窗格中,以滑鼠右鍵按一下 [SQL Server (<instance name>)],然後按一下 [重新啟動],先停止 SQL Server 服務後再重新啟動。

搭配回到頁首連結使用的箭頭圖示[Top]

使用 SQL Server PowerShell

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

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

  2. 從工作列啟動 Windows PowerShell 2.0,或是依序按一下 [開始]、[所有程式]、[附屬應用程式]、[Windows PowerShell],然後按一下 [Windows PowerShell]。

  3. 輸入 Import-Module "sqlps" 來匯入 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

  • 啟用或停用通訊協定之後,您必須停止並重新啟動 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
    

搭配回到頁首連結使用的箭頭圖示[Top]