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

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

本主题内容

在您开始之前

建议

  • 对于运行 MICROSOFT SQL Server 2005 之前版本的发布服务器,sysadmin 固定服务器角色中的用户可以在 订阅者 页面上注册订阅者。 从 SQL Server 2005 开始,不再需要显式地注册订阅者以进行复制。

安全

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

使用 SQL Server Management Studio

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

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

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

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

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

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

    • 若要访问复制代理的配置文件,请单击对话框的“常规”页上的“配置文件默认值”按钮。 有关详细信息,请参阅 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 类的一个实例。 指定 name 属性并传递步骤 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 方法。 传递 密码 参数的新密码值。

    重要

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

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

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

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

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

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

    5. 调用 ChangeDistributorPassword 方法。 为密码参数传递步骤 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 occured 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 occured when changing Distributor " + _
        " or distribution database properties.", ex)
Finally
    conn.Disconnect()
End Try

另请参阅

复制管理对象概念
禁用发布和分发
配置分发
复制管理对象概念
发行商和出版商信息脚本
复制系统存储过程概念
使用复制监视器查看信息和执行任务