共用方式為


停用發布和分發

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects(RMO)停用 SQL Server 2014 中的發佈與散發功能。

您可以執行下列動作:

  • 刪除分發伺服器上的所有分發資料庫。

  • 停用所有使用散發者的發佈者,並刪除這些發佈者上的所有發佈內容。

  • 刪除所有出版物的訂閱。 將不會刪除發行集和訂閱資料庫中的數據;不過,它會失去與任何發行集資料庫的同步處理關聯性。 如果您想要刪除訂閱者端的數據,您必須手動刪除它。

本主題內容

開始之前

先決條件

  • 若要停用發佈和發行,所有發行和散發資料庫都必須在線上。 如果存在任何可供發佈或發行集資料庫使用的 資料庫快照,則必須先卸除它們,才能停用發行和發佈。 資料庫快照集是資料庫的唯讀脫機複本,與復寫快照集無關。 如需詳細資訊,請參閱資料庫快照集 (SQL Server)

使用 SQL Server Management Studio

使用 [停用發佈與分發精靈] 來停用發佈和分發。

停用發佈和分發

  1. 在 Microsoft SQL Server Management Studio 中,連線到您想要停用的發行者或配發者,然後展開伺服器節點。

  2. 以滑鼠右鍵按兩下 [ 複寫 ] 資料夾,然後按兩下 [ 停用發佈和散發]。

  3. 完成停用發佈和散發精靈中的步驟。

使用 Transact-SQL

您可以使用複製儲存過程,以程式設計方式停用發行和散發。

停用發行和分發

  1. 停止所有複製相關的作業。 如需作業名稱的清單,請參閱 複寫代理程式安全性模型的一節。

  2. 在訂閱資料庫的每個訂閱者端,執行 sp_removedbreplication ,以從資料庫移除複寫物件。 此預存程式不會移除散發者端的復寫作業。

  3. 在發行資料庫中,執行 sp_removedbreplication,以移除資料庫中的複製物件。

  4. 如果發行者使用遠端散發者,請執行 sp_dropdistributor

  5. 在散發者端,執行 sp_dropdistpublisher。 此預存程式應該針對在散發者端註冊的每個發行者執行一次。

  6. 在散發者端,執行 sp_dropdistributiondb 來刪除散發資料庫。 此預存程序應針對發佈伺服器的每個分佈資料庫各執行一次。 這也會移除與散發資料庫相關聯的任何佇列讀取器代理程序作業。

  7. 在散發者端,執行 sp_dropdistributor ,以從伺服器中移除散發者指定。

    備註

    如果您在執行 sp_dropdistpublishersp_dropdistributor之前,不會卸除所有復寫發行和散發對象,這些程式將會傳回錯誤。 若要在卸除發行者或散發者時卸除所有複寫相關的物件,@no_checks 參數必須設定為 1。 如果發行者或散發者脫機或無法連線, @ignore_distributor 參數可以設定為 1 ,以便卸除它們;不過,任何留下的發佈和散發對象都必須手動移除。

範例 (Transact-SQL)

此範例文本會從訂閱資料庫移除複寫物件。

-- Remove replication objects from the subscription database on MYSUB.
DECLARE @subscriptionDB AS sysname
SET @subscriptionDB = N'AdventureWorks2012Replica'

-- Remove replication objects from a subscription database (if necessary).
USE master
EXEC sp_removedbreplication @subscriptionDB
GO

此範例腳本會在同時為發行者和散發者的伺服器上停用發行和散發功能,並卸除散發資料庫。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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".

-- Disable publishing and distribution.
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB as sysname;
SET @distributionDB = N'distribution';
SET @publisher = $(DistPubServer);
SET @publicationDB = N'AdventureWorks2012';

-- Disable the publication database.
USE [AdventureWorks2012]
EXEC sp_removedbreplication @publicationDB;

-- Remove the registration of the local Publisher at the Distributor.
USE master
EXEC sp_dropdistpublisher @publisher;

-- Delete the distribution database.
EXEC sp_dropdistributiondb @distributionDB;

-- Remove the local server as a Distributor.
EXEC sp_dropdistributor;
GO

使用 Replication Management Objects (RMO)

關閉發佈和傳播

  1. 移除所有使用散發者的出版物訂閱。 如需詳細資訊,請參閱 刪除提取訂閱刪除發送訂閱

  2. 移除所有使用分發者的出版物,如果出版者和分發者位於同一伺服器上,則停用所有資料庫的發布功能。 如需詳細資訊,請 參閱刪除發行集

  3. 使用 ServerConnection 類別建立與散發者的連接。

  4. 建立 DistributionPublisher 類別的執行個體。 指定Name屬性,然後傳遞步驟 3 的ServerConnection物件。

  5. (選擇性) LoadProperties 呼叫 方法以取得 對象的屬性,並確認發行者存在。 如果此方法傳回 false,則步驟 4 中設定的發行者名稱不正確,或者此散發者未使用此發行者。

  6. 呼叫 Remove 方法。 如果發行者和散發者位於不同的伺服器上,且發行者應在散發者上卸載,而不先確認發行集不存在於發行者端,則傳遞true強制值。

  7. 建立 ReplicationServer 類別的執行個體。 傳遞步驟 3 中的 ServerConnection 物件。

  8. 呼叫 UninstallDistributor 方法。 傳遞trueforce,以移除散發者端的所有復寫物件,而不需要先確認是否所有本機發行集資料庫都已停用,且是否散發資料庫已卸載。

範例 (RMO)

這個範例會移除分發者處的發行者註冊、刪除分發資料庫,以及卸載分發者。

// Set the Distributor and publication database names.
// Publisher and Distributor are on the same server instance.
string publisherName = publisherInstance;
string distributorName = publisherInstance;
string distributionDbName = "distribution";
string publicationDbName = "AdventureWorks2012";

// Create connections to the Publisher and Distributor
// using Windows Authentication.
ServerConnection publisherConn = new ServerConnection(publisherName);
ServerConnection distributorConn = new ServerConnection(distributorName);

// Create the objects we need.
ReplicationServer distributor =
    new ReplicationServer(distributorConn);
DistributionPublisher publisher;
DistributionDatabase distributionDb =
    new DistributionDatabase(distributionDbName, distributorConn);
ReplicationDatabase publicationDb;
publicationDb = new ReplicationDatabase(publicationDbName, publisherConn);

try
{
    // Connect to the Publisher and Distributor.
    publisherConn.Connect();
    distributorConn.Connect();

    // Disable all publishing on the AdventureWorks2012 database.
    if (publicationDb.LoadProperties())
    {
        if (publicationDb.EnabledMergePublishing)
        {
            publicationDb.EnabledMergePublishing = false;
        }
        else if (publicationDb.EnabledTransPublishing)
        {
            publicationDb.EnabledTransPublishing = false;
        }
    }
    else
    {
        throw new ApplicationException(
            String.Format("The {0} database does not exist.", publicationDbName));
    }

    // We cannot uninstall the Publisher if there are still Subscribers.
    if (distributor.RegisteredSubscribers.Count == 0)
    {
        // Uninstall the Publisher, if it exists.
        publisher = new DistributionPublisher(publisherName, distributorConn);
        if (publisher.LoadProperties())
        {
            publisher.Remove(false);
        }
        else
        {
            // Do something here if the Publisher does not exist.
            throw new ApplicationException(String.Format(
                "{0} is not a Publisher for {1}.", publisherName, distributorName));
        }

        // Drop the distribution database.
        if (distributionDb.LoadProperties())
        {
            distributionDb.Remove();
        }
        else
        {
            // Do something here if the distribition DB does not exist.
            throw new ApplicationException(String.Format(
                "The distribution database '{0}' does not exist on {1}.",
                distributionDbName, distributorName));
        }

        // Uninstall the Distributor, if it exists.
        if (distributor.LoadProperties())
        {
            // Passing a value of false means that the Publisher 
            // and distribution databases must already be uninstalled,
            // and that no local databases be enabled for publishing.
            distributor.UninstallDistributor(false);
        }
        else
        {
            //Do something here if the distributor does not exist.
            throw new ApplicationException(String.Format(
                "The Distributor '{0}' does not exist.", distributorName));
        }
    }
    else
    {
        throw new ApplicationException("You must first delete all subscriptions.");
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The Publisher and Distributor could not be uninstalled", ex);
}
finally
{
    publisherConn.Disconnect();
    distributorConn.Disconnect();
}
' Set the Distributor and publication database names.
' Publisher and Distributor are on the same server instance.
Dim publisherName As String = publisherInstance
Dim distributorName As String = subscriberInstance
Dim distributionDbName As String = "distribution"
Dim publicationDbName As String = "AdventureWorks2012"

' Create connections to the Publisher and Distributor
' using Windows Authentication.
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)
Dim distributorConn As ServerConnection = New ServerConnection(distributorName)

' Create the objects we need.
Dim distributor As ReplicationServer
distributor = New ReplicationServer(distributorConn)
Dim publisher As DistributionPublisher
Dim distributionDb As DistributionDatabase
distributionDb = New DistributionDatabase(distributionDbName, distributorConn)
Dim publicationDb As ReplicationDatabase
publicationDb = New ReplicationDatabase(publicationDbName, publisherConn)

Try
    ' Connect to the Publisher and Distributor.
    publisherConn.Connect()
    distributorConn.Connect()

    ' Disable all publishing on the AdventureWorks2012 database.
    If publicationDb.LoadProperties() Then
        If publicationDb.EnabledMergePublishing Then
            publicationDb.EnabledMergePublishing = False
        ElseIf publicationDb.EnabledTransPublishing Then
            publicationDb.EnabledTransPublishing = False
        End If
    Else
        Throw New ApplicationException( _
            String.Format("The {0} database does not exist.", publicationDbName))
    End If

    ' We cannot uninstall the Publisher if there are still Subscribers.
    If distributor.RegisteredSubscribers.Count = 0 Then
        ' Uninstall the Publisher, if it exists.
        publisher = New DistributionPublisher(publisherName, distributorConn)
        If publisher.LoadProperties() Then
            publisher.Remove(False)
        Else
            ' Do something here if the Publisher does not exist.
            Throw New ApplicationException(String.Format( _
                "{0} is not a Publisher for {1}.", publisherName, distributorName))
        End If

        ' Drop the distribution database.
        If distributionDb.LoadProperties() Then
            distributionDb.Remove()
        Else
            ' Do something here if the distribition DB does not exist.
            Throw New ApplicationException(String.Format( _
             "The distribution database '{0}' does not exist on {1}.", _
             distributionDbName, distributorName))
        End If

        ' Uninstall the Distributor, if it exists.
        If distributor.LoadProperties() Then
            ' Passing a value of false means that the Publisher 
            ' and distribution databases must already be uninstalled,
            ' and that no local databases be enabled for publishing.
            distributor.UninstallDistributor(False)
        Else
            'Do something here if the distributor does not exist.
            Throw New ApplicationException(String.Format( _
                "The Distributor '{0}' does not exist.", distributorName))
        End If
    Else
        Throw New ApplicationException("You must first delete all subscriptions.")
    End If

Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The Publisher and Distributor could not be uninstalled", ex)

Finally
    publisherConn.Disconnect()
    distributorConn.Disconnect()

End Try

本範例會卸載散發者,而不先停用本機發行集資料庫或刪除分散資料庫。

// Set the Distributor and publication database names.
// Publisher and Distributor are on the same server instance.
string distributorName = publisherInstance;

// Create connections to the Distributor
// using Windows Authentication.
ServerConnection conn = new ServerConnection(distributorName);
conn.DatabaseName = "master";

// Create the objects we need.
ReplicationServer distributor = new ReplicationServer(conn);

try
{
    // Connect to the Publisher and Distributor.
    conn.Connect();


    // Uninstall the Distributor, if it exists.
    // Use the force parameter to remove everthing.  
    if (distributor.IsDistributor && distributor.LoadProperties())
    {
        // Passing a value of true means that the Distributor 
        // is uninstalled even when publishing objects, subscriptions,
        // and distribution databases exist on the server.
        distributor.UninstallDistributor(true);
    }
    else
    {
        //Do something here if the distributor does not exist.
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The Publisher and Distributor could not be uninstalled", ex);
}
finally
{
    conn.Disconnect();
}
' Set the Distributor and publication database names.
' Publisher and Distributor are on the same server instance.
Dim distributorName As String = publisherInstance

' Create connections to the Distributor
' using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(distributorName)
conn.DatabaseName = "master"

' Create the objects we need.
Dim distributor As ReplicationServer = New ReplicationServer(conn)

Try
    ' Connect to the Publisher and Distributor.
    conn.Connect()


    ' Uninstall the Distributor, if it exists.
    ' Use the force parameter to remove everthing.  
    If distributor.IsDistributor And distributor.LoadProperties() Then
        ' Passing a value of true means that the Distributor 
        ' is uninstalled even when publishing objects, subscriptions,
        ' and distribution databases exist on the server.
        distributor.UninstallDistributor(True)
    Else
        'Do something here if the distributor does not exist.
    End If

Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The Publisher and Distributor could not be uninstalled", ex)

Finally
    conn.Disconnect()

End Try

另請參閱

複寫管理物件概念
復寫系統預存程式概念