查看和修改分发服务器和发布服务器属性

适用于:SQL ServerAzure SQL 托管实例

本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象在 SQL Server 中查看和修改分发服务器和发布服务器属性。

本主题内容

开始之前

建议

  • 对于运行低于 Microsoft SQL Server 2005 (9.x) 版本的发布服务器,sysadmin 固定服务器角色中的用户可以在“订阅服务器”页上注册订阅服务器。 从 SQL Server 2005 (9.x) 开始,不再需要为复制显式注册订阅服务器。

安全性

如果可能,请在运行时提示用户输入安全凭据。

使用 SQL Server Management Studio

查看和修改分发服务器属性

  1. 在 SQL Server Management Studio 中连接到分发服务器,然后展开服务器节点。

  2. 右键单击 “复制” 文件夹,然后单击 “分发服务器属性”

  3. 在“分发服务器属性 - <分发服务器>”对话框中查看和修改属性。

    • 若要查看和修改分发数据库的属性,请在该对话框的“常规”页上单击该数据库的属性按钮 (...)。

    • 若要查看和修改与分发服务器关联的发布服务器属性,请在该对话框的“发布服务器”页面上单击发布服务器的属性按钮 ( ... )。

    • 若要访问复制代理的配置文件,请单击此对话框的 “常规” 页上的 “默认配置文件” 按钮。 有关详细信息,请参阅 Replication Agent Profiles

    • 若要更改管理存储过程在发布服务器上执行以及在分发服务器上更新信息时所用帐户的密码,请在该对话框的 “发布服务器” 页面上的 “密码”“确认密码” 框中输入新密码。 有关详细信息,请参阅保护分发服务器的安全

  4. 根据需要修改属性,然后单击 “确定”

查看和修改发布服务器属性

  1. 在 SQL Server Management Studio 中连接到发布服务器,然后展开服务器节点。

  2. 右键单击 “复制” 文件夹,然后单击 “发布服务器属性”

  3. 在“发布服务器属性 - <发布服务器>”对话框中查看和修改属性。

    • sysadmin 固定服务器角色中的用户可以在 “发布数据库” 页上为复制启用数据库。 启用数据库并不会发布该数据库,而是允许该数据库的 db_owner 固定数据库角色中的任何用户在该数据库中创建一个或多个发布。
  4. 根据需要修改属性,然后单击 “确定”

“使用 Transact-SQL”

可以使用复制存储过程以编程方式查看发布服务器和分发服务器属性。

查看分发服务器和分发数据库属性

  1. 执行 sp_helpdistributor 可返回有关分发服务器、分发数据库和工作目录的信息。

  2. 执行 sp_helpdistributiondb 可返回指定的分发数据库的属性。

更改分发服务器和分发数据库属性

  1. 在分发服务器上,执行 sp_changedistributor_property 可修改分发服务器属性。

  2. 在分发服务器上,执行 sp_changedistributiondb 可修改分发数据库属性。

  3. 在分发服务器上,执行 sp_changedistributor_password 可更改分发服务器密码。

    重要

    如果可能,请在运行时提示用户输入安全凭据。 如果必须将凭据存储在脚本文件中,请确保该文件的安全以防受到未经授权的访问。

  4. 在分发服务器上,执行 sp_changedistpublisher ,以便使用分发服务器更改发布服务器的属性。

示例 (Transact-SQL)

下面的示例 Transact-SQL 脚本返回有关分发服务器和分发数据库的信息。

-- View information about the Distributor, distribution database, 
-- working directory, and SQL Server Agent user account. 
USE master
EXEC sp_helpdistributor;
GO
-- View information about the specified distribution database. 
USE distribution
EXEC sp_helpdistributiondb;
GO

该示例更改分发服务器的保持期、连接到分发服务器时使用的密码以及分发服务器检查各种复制代理的状态时采用的时间间隔(也称为检测信号时间间隔)。

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须将凭据存储在脚本文件中,请确保该文件的安全以防受到未经授权的访问。


-- Change the heartbeat interval at the Distributor to 5 minutes. 
USE master 
exec sp_changedistributor_property 
    @property = N'heartbeat_interval', 
    @value = 5;
GO
DECLARE @distributionDB AS sysname;
SET @distributionDB = N'distribution';

-- Change the history retention period to 24 hours and the
-- maximum retention period to 48 hours.  
USE distribution
EXEC sp_changedistributiondb @distributionDB, N'history_retention', 24
EXEC sp_changedistributiondb @distributionDB, N'max_distretention', 48
GO
-- Change the password on the Distributor. 
-- To avoid storing the password in the script file, the value is passed 
-- into SQLCMD as a scripting variable. For information about how to use 
-- scripting variables on the command line and in SQL Server Management
-- Studio, see the "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".
USE master
EXEC sp_changedistributor_password $(Password)
GO

使用复制管理对象 (RMO)

查看和修改分发服务器属性

  1. 使用 ServerConnection 类创建与分发服务器的连接。

  2. 创建 ReplicationServer 类的一个实例。 传递步骤 1 中的 ServerConnection 对象。

  3. (可选)检查 IsDistributor 属性以验证当前连接到的服务器是否为分发服务器。

  4. 调用 Load 方法获取该服务器的属性。

  5. (可选)若要更改属性,请为一个或多个可在 ReplicationServer 对象上设置的分发服务器属性设置新值。

  6. (可选)如果将 CachePropertyChanges 对象的 ReplicationServer 属性设置为 true,则调用 CommitPropertyChanges 方法来提交对服务器的更改。

查看和修改分发数据库属性

  1. 使用 ServerConnection 类创建与分发服务器的连接。

  2. 创建 DistributionDatabase 类的一个实例。 指定名称属性并传递步骤 1 中的 ServerConnection 对象。

  3. 调用 LoadProperties 方法获取该服务器的属性。 如果此方法返回 false,则该服务器上不存在指定名称的数据库。

  4. (可选)若要更改属性,请为可以设置的 DistributionDatabase 属性中的一个设置新值。

  5. (可选)如果将 CachePropertyChanges 对象的 DistributionDatabase 属性设置为 true,则调用 CommitPropertyChanges 方法来提交对服务器的更改。

查看和修改发布服务器属性

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 DistributionPublisher 类的一个实例。 指定 Name 属性并传递步骤 1 中的 ServerConnection 对象。

  3. (可选)若要更改属性,请为可以设置的 DistributionPublisher 属性中的一个设置新值。

  4. (可选)如果将 CachePropertyChanges 对象的 DistributionPublisher 属性设置为 true,则调用 CommitPropertyChanges 方法来提交对服务器的更改。

更改从发布服务器到分发服务器的管理连接的密码

  1. 使用 ServerConnection 类创建与分发服务器的连接。

  2. 创建 ReplicationServer 类的一个实例。

  3. ConnectionContext 属性设置为步骤 1 中创建的连接。

  4. 调用 Load 方法获取该对象的属性。

  5. 调用 ChangeDistributorPassword 方法。 为 password 参数传递新的密码值。

    重要

    如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的加密服务

  6. (可选)执行下列步骤以更改每个使用该分发服务器的远程发布服务器上的密码:

    1. 使用 ServerConnection 类创建与发布服务器的连接。

    2. 创建 ReplicationServer 类的一个实例。

    3. ConnectionContext 属性设置为步骤 6a 中创建的连接。

    4. 调用 Load 方法获取该对象的属性。

    5. 调用 ChangeDistributorPassword 方法。 为 password 参数传递步骤 5 中的新密码值。

示例 (RMO)

此示例显示如何更改分发和分发数据库属性。

重要

若要避免将凭据存储在代码中,应当在运行时提供新分发服务器密码。

// Set the Distributor and distribution database names.
string distributionDbName = "distribution";
string distributorName = publisherInstance;

ReplicationServer distributor;
DistributionDatabase distributionDb;

// Create a connection to the Distributor using Windows Authentication.
ServerConnection conn = new ServerConnection(distributorName);

try
{
    // Open the connection. 
    conn.Connect();

    distributor = new ReplicationServer(conn);

    // Load Distributor properties, if it is installed.
    if (distributor.LoadProperties())
    {
        // Password supplied at runtime.
        distributor.ChangeDistributorPassword(password);
        distributor.AgentCheckupInterval = 5;

        // Save changes to the Distributor properties.
        distributor.CommitPropertyChanges();
    }
    else
    {
        throw new ApplicationException(
            String.Format("{0} is not a Distributor.", publisherInstance));
    }

    // Create an object for the distribution database 
    // using the open Distributor connection.
    distributionDb = new DistributionDatabase(distributionDbName, conn);

    // Change distribution database properties.
    if (distributionDb.LoadProperties())
    {
        // Change maximum retention period to 48 hours and history retention 
        // period to 24 hours.
        distributionDb.MaxDistributionRetention = 48;
        distributionDb.HistoryRetention = 24;

        // Save changes to the distribution database properties.
        distributionDb.CommitPropertyChanges();
    }
    else
    {
        // Do something here if the distribution database does not exist.
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here. 
    throw new ApplicationException("An error occurred when changing Distributor " +
        " or distribution database properties.", ex);
}
finally
{
    conn.Disconnect();
}
' Set the Distributor and distribution database names.
Dim distributionDbName As String = "distribution"
Dim distributorName As String = publisherInstance

Dim distributor As ReplicationServer
Dim distributionDb As DistributionDatabase

' Create a connection to the Distributor using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(distributorName)

Try
    ' Open the connection. 
    conn.Connect()

    distributor = New ReplicationServer(conn)

    ' Load Distributor properties, if it is installed.
    If distributor.LoadProperties() Then
        ' Password supplied at runtime.
        distributor.ChangeDistributorPassword(password)
        distributor.AgentCheckupInterval = 5

        ' Save changes to the Distributor properties.
        distributor.CommitPropertyChanges()
    Else
        Throw New ApplicationException( _
            String.Format("{0} is not a Distributor.", publisherInstance))
    End If

    ' Create an object for the distribution database 
    ' using the open Distributor connection.
    distributionDb = New DistributionDatabase(distributionDbName, conn)

    ' Change distribution database properties.
    If distributionDb.LoadProperties() Then
        ' Change maximum retention period to 48 hours and history retention 
        ' period to 24 hours.
        distributionDb.MaxDistributionRetention = 48
        distributionDb.HistoryRetention = 24

        ' Save changes to the distribution database properties.
        distributionDb.CommitPropertyChanges()
    Else
        ' Do something here if the distribution database does not exist.
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here. 
    Throw New ApplicationException("An error occurred when changing Distributor " + _
        " or distribution database properties.", ex)
Finally
    conn.Disconnect()
End Try

另请参阅

Replication Management Objects Concepts
禁用发布和分发
配置分发
Replication Management Objects Concepts
分发服务器和发布服务器信息脚本
Replication System Stored Procedures Concepts
查看发布服务器的信息和执行其任务(复制监视器)