如何在运行 Windows XP Service Pack 2 的系统上使用脚本以编程方式打开端口供 SQL Server 使用

Microsoft Windows XP Service Pack 2 (SP2) 包括 Windows 防火墙。Windows 防火墙是 Internet 连接防火墙 (ICF) 的增强版本。默认情况下,运行 Windows XP Service Pack 2 的计算机上启用了 Windows 防火墙。Windows 防火墙将阻止某些使用 TCP/IP、使用命名管道或使用多协议远程过程调用 (RPC) 的网络连接。这种阻止行为可影响 Microsoft Data Engine (MSDE)、Microsoft SQL Server 2000 和 Microsoft SQL Server 2005。

如果您有一个应用程序要求 SQL Server 或 MSDE 通过使用命名管道、使用 TCP/IP 或使用 RPC 来访问网络,则您可以使用“更多信息”一节中提供的脚本以编程方式(而不使用 Windows 防火墙)打开所需的端口。

本文中提供了两个脚本。第一个脚本以编程方式配置 Windows 防火墙,以允许 SQL Server 在所有协议上侦听网络。第二个脚本以编程方式配置 Windows 防火墙,以允许 SQL Server 仅在 TCP/IP 上侦听。

注意 :我们建议您仅在需要时才打开端口。


本文中讨论的脚本仅提供命名管道上的多协议 (RPC) 访问。如果您正在运行 RPC over TCP,请参阅下面的 Microsoft 知识库文章中有关使用 TCP/IP 的信息:

841252   (http://support.microsoft.com/kb/841252/ ) 如何在 Windows XP Service Pack 2 上手动为 SQL Server 2000 启用 TCP/IP

更多信息
可用于打开所有端口的脚本 本节中讨论的脚本以编程方式配置 Windows 防火墙,以允许 SQL Server 在所有协议上侦听网络。关于此脚本的重要说明此脚本...

<script type="text/javascript"> loadTOCNode(1, 'moreinformation'); </script>

可用于打开所有端口的脚本

<script type="text/javascript"> loadTOCNode(3, 'moreinformation'); </script> 本节中讨论的脚本以编程方式配置 Windows 防火墙,以允许 SQL Server 在所有协议上侦听网络。

关于此脚本的重要说明

  • 此脚本应仅在运行 Windows XP Service Pack 2 的计算机上运行。
  • 此脚本具有用于启用命名管道和启用多协议 (RPC) 的其他选项。
  • 此脚本仅在命名管道上启用多协议 (RPC),并且仅打开端口 445。
  • 此脚本不提供指定范围的功能。


要创建脚本,请按照下列步骤操作:

  1. 启动记事本。
  2. 复制如下代码并将其粘贴到记事本中:
    echo off

    if "%1"=="-np" goto HandleNp
    if "%1"=="-rpc" goto HandleRpc
    if "%1"=="-tcp" goto HandleTcp
    if "%1"=="-browser" goto HandleBrowser

    rem Usage
    :Usage

    echo "Usage: setupSqlServerPortAll -[np | rpc | tcp | browser] -port [portnum] -[enable | disable]
    echo "-np : Setup SQLServer to listen on Named Pipe connections for local subnet only"
    echo "-rpc : Setup SQLServer to listen on RPC multiprotocol for local subnet only"
    echo "-tcp : Setup SQLServer to listen on TCP connections for local subnet only"
    echo " Must specify a port if -tcp option is chosen."
    echo "-browser : Setup SQLServer to provide SSRP service to support named instances"
    echo "-port : Applies only for tcp"
    echo " One of the following options MUST be specified"
    echo "-enable: Enables a port"
    echo "-disable: Disables a port"

    goto Exit

    :HandleTcp
    echo %2
    if "%2"=="-port" goto cont
    goto Usage
    :cont
    if "%3"=="" goto Usage
    if "%4"=="-enable" goto EnableTcp
    if "%4"=="-disable" goto DisableTcp
    goto Usage

    :EnableTcp
    echo "Enabling SQLServer tcp access for port %3 local subnet only"
    netsh firewall set portopening tcp %3 SQL%3 ENABLE subnet
    goto Exit

    :DisableTcp
    echo Disabling SQLServer tcp access for port %3 local subnet only"
    netsh firewall set portopening tcp %3 SQL%3 disable subnet
    goto Exit

    :HandleNp
    if "%2"=="-enable" goto EnableNp
    if "%2"=="-disable" goto DisableNp
    goto Usage

    :EnableNp
    echo "Enabling SQLServer named pipe access for local subnet only"
    netsh firewall set portopening tcp 445 SQLNP ENABLE subnet
    goto Exit

    :DisableNp
    echo Disabling SQLServer named pipe access for local subnet only"
    netsh firewall set portopening tcp 445 SQLNP DISABLE subnet
    goto Exit

    :HandleRpc
    if "%2"=="-enable" goto EnableRpc
    if "%2"=="-disable" goto DisableRpc
    goto Usage

    :EnableRpc
    echo "Enabling SQLServer multiprotocol access for local subnet only"
    netsh firewall set portopening tcp 445 SQLNP enable subnet
    goto Exit

    :DisableRpc
    echo Disabling SQLServer multiprotocol access for local subnet only"
    netsh firewall set portopening tcp 445 SQLNP disable subnet
    goto Exit

    :HandleBrowser
    if "%2"=="-enable" goto EnableBrowser
    if "%2"=="-disable" goto DisableBrowser
    goto Usage

    :EnableBrowser
    echo "Enabling SQLServer SSRP service for local subnet only"
    netsh firewall set portopening udp 1434 SQLBrowser enable subnet
    goto Exit

    :DisableBrowser
    echo "Enabling SQLServer SSRP service for local subnet only"
    netsh firewall set portopening udp 1434 SQLBrowser disable subnet
    goto Exit

    :Exit
    endlocal

  3. 将其保存为 .txt 文件,并将文件命名为 ConfigSQLPorts.txt。
  4. 将 ConfigSQLPorts.txt 文件重命名为 ConfigSQLPorts.bat。


当您在 ConfigSQLPorts.bat 文件中运行该脚本时,必须使用在其上保存了该脚本的计算机。要运行脚本,请按照下列步骤操作:

  1. 单击“开始”,单击“运行”,键入 cmd ,然后单击“确定”。
  2. 在 命令窗口中,使用“cd”命令更改文件夹,直到您与 ConfigSQLPorts.bat 文件位于同一个文件夹中。例如,如果 ConfigSQLPorts.bat 文件保存在 C:/Myfiles 中,您应在命令提示符下键入“CD myfiles”,然后按 Enter。此操作将使您的文件夹更改为 C:/Myfiles。
  3. 要运行 ConfigSQLPorts.bat 脚本,请在命令提示符下键入 ConfigSQLPorts.bat ,然后按 Enter。




可用于仅打开 TCP/IP 端口的脚本

<script type="text/javascript"> loadTOCNode(3, 'moreinformation'); </script> 本节中讨论的脚本用于配置 Windows 防火墙以允许 SQL Server 仅在 TCP/IP 上侦听。

要创建脚本,请按照下列步骤操作:

  1. 启动记事本。
  2. 复制如下代码并将其粘贴到记事本中:

    echo off
    setlocal

    if "%1"=="-port" goto HandleTcp

    rem Usage
    :Usage

    echo "Usage: setupSqlServerPort -port [portnum] -[enable | disable] [ALL | SUBNET]"
    echo -port : Specifies the port to be enabled or disabled. Port is not optional.
    echo -enable: Enables a port
    echo -enable ALL: enables access for ALL
    echo -enable SUBNET: enables access for SUBNET
    echo -disable: Disables a port
    echo one of -enable or -disable must be specified
    echo the default scope is SUBNET only

    goto Exit

    :HandleTcp
    if "%2"=="" goto Usage
    if "%3"=="-enable" goto EnableTcp
    if "%3"=="-disable" goto DisableTcp
    goto Usage


    :EnableTcp
    set SCOPE="%4"
    if "%4"=="ALL" echo "Enabling SQLServer tcp access for port %2 ALL access"
    if "%4"=="SUBNET" echo "Enabling SQLServer tcp access for port %2 subnet only access"
    if "%4"=="" set SCOPE="SUBNET"

    netsh firewall set portopening tcp %2 SQL_PORT_%2 ENABLE %SCOPE%
    goto Exit

    :DisableTcp
    echo Disabling SQLServer tcp access for port %2"
    netsh firewall set portopening tcp %2 SQL_PORT_%2 disable
    goto Exit

    :Exit
    endlocal

  3. 将其保存为 .txt 文件,并将文件命名为 SetupSqlServerPort.txt。
  4. 将 SetupSqlServerPort.txt 文件重命名为 SetupSqlServerPort.bat。



当您运行 SetupSqlServerPort.bat 脚本时,必须使用在其上保存了该脚本的计算机。要运行脚本,请按照下列步骤操作:

  1. 单击“开始”,单击“运行”,键入 cmd ,然后单击“确定”。
  2. 在 命令窗口中,使用“cd”命令更改文件夹,直到您与 SetupSqlServerPort.bat 文件位于同一个文件夹中。例如,如果 SetupSqlServerPort.bat 文件保存在 C:/Myfiles 中,您应在命令提示符下键入“CD myfiles”,然后按 Enter。此操作将使您的文件夹更改为 C:/Myfiles。
  3. 要运行 SetupSqlServerPort.bat 脚本,请在命令提示符下键入 setupSqlServerPort.bat ,然后按 Enter。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值