启用或禁用服务器网络协议

适用于:SQL Server - 仅限 Windows

所有网络协议都是在安装期间由 SQL Server 安装程序安装的,可以启用也可以禁用这些网络协议。 本文介绍如何通过使用 SQL Server 配置管理器或 PowerShell,在 SQL Server 中启用或禁用服务器网络协议。 必须停止并重新启动 数据库引擎 ,更改才能生效。

备注

  • 在安装 SQL Server Express Edition 过程中,为 BUILTIN\Users 组添加一个登录名。 此登录名可以使计算机的所有通过身份验证的用户作为 public 角色成员访问 SQL Server Express 实例。 可以安全地删除 BUILTIN\Users 登录名,以限制 数据库引擎 对具有单独登录名或为使用此登录名的其他 Windows 组成员的计算机用户的访问。

  • SQL Server 至 SQL Server 2014 (12.x) 的 SQL Server 和 Microsoft 数据提供商在默认情况下仅支持 TLS 1.0 和 SSL 3.0。 如果通过更改操作系统 SChannel 层强制使用其他协议(如 TLS 1.1 或 TLS 1.2),与 SQL Server 的连接可能会失败,除非安装相应的更新,将 TLS 1.1 和 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 Edition),则还应重新启动 SQL Server Browser 服务。

使用 SQL Server PowerShell

使用 PowerShell 启用服务器网络协议

  1. 使用管理员权限打开一个命令提示符。

  2. 从任务栏或“开始”菜单启动 Windows PowerShell。

  3. 通过输入 Import-Module SqlServer 导入 SqlServer 模块。

  4. 执行以下语句以启用 TCP 和 Named Pipes 协议。 <computer_name> 将 SQL Server替换为运行 的计算机的名称。 如果你在配置命名实例(包括 SQL Server Express Edition),请将 MSSQLSERVER 替换为该实例的名称。

    IsEnabled 若要禁用协议,请将 $false属性设置为 。

    你可在任何安装了 SQL Server 或安装了 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 重新启动数据库引擎

数据库引擎 启用或禁用了协议后,必须停止并重新启动才能使更改生效。 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 Edition),则还应重新启动 SQL Server Browser 服务。