檢視及修改散發者和發行者屬性

適用於:SQL ServerAzure SQL 受控執行個體

此主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO),檢視和修改 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

使用 Replication Management Objects (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 方法。 針對 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 System Stored Procedures Concepts
檢視發行者的資訊並執行工作 (複寫監視器)