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

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 和 Named Pipes 协议。 <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 重新启动数据库引擎

  • 数据库引擎 启用或禁用了协议后,必须停止并重新启动才能使更改生效。 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