パブリッシングとディストリビューションの構成

適用対象: SQL ServerAzure SQL Managed Instance

このトピックでは、SQL Server Management Studio、Transact-SQL、またはレプリケーション管理オブジェクト (RMO) を使用して SQL Server でパブリッシングとディストリビューションを構成する方法について説明します。

始める前に

セキュリティ

詳細については、「レプリケーションのセキュリティ設定の表示および変更」を参照してください。

SQL Server Management Studio を使用する

パブリケーションの新規作成ウィザードまたはディストリビューションの構成ウィザードを使用して、ディストリビューションを構成します。 ディストリビューターを構成したら、[ディストリビューターのプロパティ - <ディストリビューター]> ダイアログ ボックスでプロパティを表示および変更します。 db_owner 固定データベース ロールのメンバーがパブリケーションを作成できるようにディストリビューターを構成する場合、またはパブリッシャーではないリモート ディストリビューターを構成する必要がある場合は、ディストリビューションの構成ウィザードを使用します。

ディストリビューションを構成するには

  1. Microsoft SQL Server Management Studio で、ディストリビューターとなるサーバーに接続し (多くの場合、パブリッシャーとディストリビューターは同じサーバーです)、サーバー ノードを展開します。

  2. [レプリケーション] フォルダーを右クリックし、 [ディストリビューションの構成]をクリックします。

  3. ディストリビューションの構成ウィザードに従って、次の操作を実行します。

  • ディストリビューターを選択します。 ローカル ディストリビューターを使用するには、[ServerName を独自のディストリビューターとする (SQL Server はディストリビューション データベースとログを作成します)] を選択します。 リモート ディストリビューターを使用するには、 [以下のサーバーをディストリビューターとして使用する]を選択し、サーバーを選択します。 このサーバーは既にディストリビューターとして構成されている必要があり、パブリッシャーではこのディストリビューターを使用できるようになっている必要があります。 詳細については、「ディストリビューターでのリモート パブリッシャーの有効化 (SQL Server Management Studio)」を参照してください。

    リモート ディストリビューターを選択した場合、パブリッシャーからディストリビューターへの接続に対して、 [管理パスワード] ページでパスワードを入力する必要があります。 このパスワードは、リモート ディストリビューターでパブリッシャーを有効にしたときに指定したパスワードと一致する必要があります。

  • ローカル ディストリビューターに対してルート スナップショット フォルダーを指定します。 スナップショット フォルダーは、共有として指定したディレクトリです。このフォルダーの読み取りと書き込みをするエージェントには、このフォルダーへのアクセスを可能にする十分な権限が必要です。 このディストリビューターを使用する各パブリッシャーによって、ルート フォルダーの下にフォルダーが作成され、各パブリケーションでは、パブリッシャー フォルダーの下にスナップショット ファイルを格納するフォルダーが作成されます。 フォルダーの適切なセキュリティ保護の詳細については、「スナップショット フォルダーのセキュリティ保護」を参照してください。

  • ローカル ディストリビューターに対してディストリビューション データベースを指定します。 ディストリビューション データベースには、すべての種類のレプリケーションのメタデータと履歴データ、およびトランザクション レプリケーションに対するトランザクションが格納されます。

  • 必要に応じて、その他のパブリッシャーでディストリビューターを使用できるようにします。 その他のパブリッシャーがディストリビューターを使用できるようになっている場合は、これらのパブリッシャーからディストリビューターへの接続に対して、 [ディストリビューター パスワード] ページでパスワードを入力する必要があります。

  • 必要に応じて、構成の設定のスクリプトを作成します。 詳細については、「レプリケーションのスクリプト作成」を参照してください。

Transact-SQL の使用

レプリケーションのパブリッシングおよびディストリビューションは、レプリケーションのストアド プロシージャを使用してプログラムから構成できます。

ローカル ディストリビューターを使用してパブリッシングを構成するには

  1. sp_get_distributor (Transact-SQL) を実行して、サーバーが既にディストリビューターとして構成されているかどうかを調べます。
  • 結果セットの installed の値が 0 の場合は、ディストリビューターの master データベースで sp_adddistributor (Transact-SQL) を実行します。

  • 結果セットの distribution db installed の値が 0 の場合は、ディストリビューターの master データベースで sp_adddistributiondb (Transact-SQL) を実行します。 このとき、@database にディストリビューション データベースの名前を指定します。 必要に応じて、トランザクションの最大保有期間 (@max_distretention) および履歴保有期間 (@history_retention) を指定することができます。 新しいデータベースを作成する場合は、必要なデータベース プロパティのパラメーターを指定します。

  1. ディストリビューター (兼パブリッシャー) で、sp_adddistpublisher (Transact-SQL) を実行します。このとき @working_directory には、既定のスナップショット フォルダーとして使用する UNC 共有を指定します。

    SQL Managed Instance 上のディストリビューターの場合は、@working_directory に Azure ストレージ アカウントを、@storage_connection_string にストレージ アクセス キーを使用します。

  2. パブリッシャーで、sp_replicationdboption (Transact-SQL) を実行します。 @dbname にはパブリッシュするデータベースを、@optname にはレプリケーションの種類を、@value には true を指定します。

リモート ディストリビューターを使用してパブリッシングを構成するには

  1. sp_get_distributor (Transact-SQL) を実行して、サーバーが既にディストリビューターとして構成されているかどうかを調べます。

    • 結果セットの installed の値が 0 の場合は、ディストリビューターの master データベースで sp_adddistributor (Transact-SQL) を実行します。 @password には強力なパスワードを指定してください。 distributor_admin アカウント用のこのパスワードは、パブリッシャーがディストリビューターに接続する際に使用されます。

    • 結果セットの distribution db installed の値が 0 の場合は、ディストリビューターの master データベースで sp_adddistributiondb (Transact-SQL) を実行します。 このとき、@database にディストリビューション データベースの名前を指定します。 必要に応じて、トランザクションの最大保有期間 (@max_distretention) および履歴保有期間 (@history_retention) を指定することができます。 新しいデータベースを作成する場合は、必要なデータベース プロパティのパラメーターを指定します。

  2. ディストリビューターで、sp_adddistpublisher (Transact-SQL) を実行します。このとき、@working_directory には、既定のスナップショット フォルダーとして使用する UNC 共有を指定します。 ディストリビューターがパブリッシャーに接続する際に SQL Server 認証を使用する場合は、@security_mode0 の値を指定し、さらに @login@password には Microsoft SQL Server ログイン情報を指定する必要があります。

    SQL Managed Instance 上のディストリビューターの場合は、@working_directory に Azure ストレージ アカウントを、@storage_connection_string にストレージ アクセス キーを使用します。

  3. パブリッシャーの master データベースで sp_adddistributor (Transact-SQL) を実行します。 @password には、手順 1. で使用した強力なパスワードを指定してください。 このパスワードは、パブリッシャーがディストリビューターに接続する際に使用されます。

  4. パブリッシャーで、sp_replicationdboption (Transact-SQL) を実行します。 このとき、@dbname にはパブリッシュするデータベースを、@optname にはレプリケーションの種類を、@value には true 値を指定します。

例 (Transact-SQL)

次の例に、パブリッシングおよびディストリビューションをプログラムから構成する方法を示します。 この例では、パブリッシャーおよびローカル ディストリビューターとして構成するサーバーの名前をスクリプト変数を使って指定しています。 レプリケーションのパブリッシングおよびディストリビューションは、レプリケーションのストアド プロシージャを使用してプログラムから構成できます。

-- 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".

-- Install the Distributor and the distribution database.
DECLARE @distributor AS sysname;
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @directory AS nvarchar(500);
DECLARE @publicationDB AS sysname;
-- Specify the Distributor name.
SET @distributor = $(DistPubServer);
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);
-- Specify the replication working directory.
SET @directory = N'\\' + $(DistPubServer) + '\repldata';
-- Specify the publication database.
SET @publicationDB = N'AdventureWorks2022'; 

-- Install the server MYDISTPUB as a Distributor using the defaults,
-- including autogenerating the distributor password.
USE master
EXEC sp_adddistributor @distributor = @distributor;

-- Create a new distribution database using the defaults, including
-- using Windows Authentication.
USE master
EXEC sp_adddistributiondb @database = @distributionDB, 
    @security_mode = 1;
GO

-- Create a Publisher and enable AdventureWorks2022 for replication.
-- Add MYDISTPUB as a publisher with MYDISTPUB as a local distributor
-- and use Windows Authentication.
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);

USE [distribution]
EXEC sp_adddistpublisher @publisher=@publisher, 
    @distribution_db=@distributionDB, 
    @security_mode = 1;
GO

レプリケーション管理オブジェクト (RMO) の使用

パブリッシングおよびディストリビューションを単一サーバーで構成するには

  1. ServerConnection クラスを使用して、サーバーへの接続を作成します。

  2. ReplicationServer クラスのインスタンスを作成します。 手順 1. の ServerConnection を渡します。

  3. DistributionDatabase クラスのインスタンスを作成します。

  4. Name プロパティにデータベース名を設定し、 ConnectionContext プロパティに手順 1. の ServerConnection を設定します。

  5. InstallDistributor メソッドを呼び出してディストリビューターをインストールします。 手順 3 の DistributionDatabase オブジェクトを渡します。

  6. DistributionPublisher クラスのインスタンスを作成します。

  7. 次の DistributionPublisherのプロパティを設定します。

  1. Create メソッドを呼び出します。

リモート ディストリビューターを使用してパブリッシングおよびディストリビューションを構成するには

  1. ServerConnection クラスを使用して、リモート ディストリビューター サーバーへの接続を作成します。

  2. ReplicationServer クラスのインスタンスを作成します。 手順 1. の ServerConnection を渡します。

  3. DistributionDatabase クラスのインスタンスを作成します。

  4. Name プロパティにデータベース名を設定し、 ConnectionContext プロパティに手順 1. の ServerConnection を設定します。

  5. InstallDistributor メソッドを呼び出してディストリビューターをインストールします。 安全なパスワード (パブリッシャーがリモート ディストリビューターへの接続時に使用) および手順 3. の DistributionDatabase オブジェクトを指定します。 詳細については、「ディストリビューターのセキュリティ保護」を参照してください。

    重要

    可能であれば、実行時、ユーザーに対してセキュリティ資格情報の入力を要求します。 資格情報を保存する必要がある場合は、Microsoft Windows .NET Framework に用意されている 暗号化サービス を使用します。

  6. DistributionPublisher クラスのインスタンスを作成します。

  7. 次の DistributionPublisherのプロパティを設定します。

  1. Create メソッドを呼び出します。

  2. ServerConnection クラスを使用して、ローカル パブリッシャー サーバーへの接続を作成します。

  3. ReplicationServer クラスのインスタンスを作成します。 手順 9. の ServerConnection を渡します。

  4. InstallDistributor メソッドを呼び出します。 リモート ディストリビューターの名前、および手順 5. で指定したリモート ディストリビューターのパスワードを渡します。

重要

可能であれば、実行時、ユーザーに対してセキュリティ資格情報の入力を要求します。 資格情報を保存する必要がある場合は、Windows .NET Framework に用意されている 暗号化サービス を使用します。

例 (RMO)

レプリケーション管理オブジェクト (RMO) を使用することで、プログラムによってレプリケーション パブリッシングおよびディストリビューションを構成できます。

// Set the server and database names
string distributionDbName = "distribution";
string publisherName = publisherInstance;
string publicationDbName = "AdventureWorks2022";

DistributionDatabase distributionDb;
ReplicationServer distributor;
DistributionPublisher publisher;
ReplicationDatabase publicationDb;

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

try
{
    // Connect to the server acting as the Distributor 
    // and local Publisher.
    conn.Connect();

    // Define the distribution database at the Distributor,
    // but do not create it now.
    distributionDb = new DistributionDatabase(distributionDbName, conn);
    distributionDb.MaxDistributionRetention = 96;
    distributionDb.HistoryRetention = 120;

    // Set the Distributor properties and install the Distributor.
    // This also creates the specified distribution database.
    distributor = new ReplicationServer(conn);
    distributor.InstallDistributor((string)null, distributionDb);

    // Set the Publisher properties and install the Publisher.
    publisher = new DistributionPublisher(publisherName, conn);
    publisher.DistributionDatabase = distributionDb.Name;
    publisher.WorkingDirectory = @"\\" + publisherName + @"\repldata";
    publisher.PublisherSecurity.WindowsAuthentication = true;
    publisher.Create();

    // Enable AdventureWorks2022 as a publication database.
    publicationDb = new ReplicationDatabase(publicationDbName, conn);

    publicationDb.EnabledTransPublishing = true;
    publicationDb.EnabledMergePublishing = true;
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("An error occurred when installing distribution and publishing.", ex);
}
finally
{
    conn.Disconnect();
}
' Set the server and database names
Dim distributionDbName As String = "distribution"
Dim publisherName As String = publisherInstance
Dim publicationDbName As String = "AdventureWorks2022"

Dim distributionDb As DistributionDatabase
Dim distributor As ReplicationServer
Dim publisher As DistributionPublisher
Dim publicationDb As ReplicationDatabase

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

Try
    ' Connect to the server acting as the Distributor 
    ' and local Publisher.
    conn.Connect()

    ' Define the distribution database at the Distributor,
    ' but do not create it now.
    distributionDb = New DistributionDatabase(distributionDbName, conn)
    distributionDb.MaxDistributionRetention = 96
    distributionDb.HistoryRetention = 120

    ' Set the Distributor properties and install the Distributor.
    ' This also creates the specified distribution database.
    distributor = New ReplicationServer(conn)
    distributor.InstallDistributor((CType(Nothing, String)), distributionDb)

    ' Set the Publisher properties and install the Publisher.
    publisher = New DistributionPublisher(publisherName, conn)
    publisher.DistributionDatabase = distributionDb.Name
    publisher.WorkingDirectory = "\\" + publisherName + "\repldata"
    publisher.PublisherSecurity.WindowsAuthentication = True
    publisher.Create()

    ' Enable AdventureWorks2022 as a publication database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)

    publicationDb.EnabledTransPublishing = True
    publicationDb.EnabledMergePublishing = True

Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("An error occurred when installing distribution and publishing.", ex)

Finally
    conn.Disconnect()

End Try

参照

View and Modify Distributor and Publisher Properties (ディストリビューターとパブリッシャーのプロパティの表示および変更)
Replication System Stored Procedures Concepts
[ディストリビューションの構成]
Replication Management Objects Concepts
AlwaysOn 可用性グループ用のレプリケーションの構成 (SQL Server)