このトピックでは、SQL Server Management Studio、Transact-SQL、またはレプリケーション管理オブジェクト (RMO) を使用して、SQL Server 2014 で発行と配布を構成する方法について説明します。
開始する前に
安全
詳細については、「 セキュリティで保護されたレプリケーションのデプロイ」を参照してください。
SQL Server Management Studio の使用
パブリケーションの新規作成ウィザードまたは配布の構成ウィザードを使用して配布を構成します。 ディストリビューターを構成したら、[ディストリビューターのプロパティ - <Distributor> ] ダイアログ ボックスでプロパティを表示および変更します。 db_owner固定データベース ロールのメンバーがパブリケーションを作成できるようにディストリビューターを構成する場合、またはパブリッシャーではないリモート ディストリビューターを構成する場合は、ディストリビューションの構成ウィザードを使用します。
ディストリビューションを構成するには
Microsoft SQL Server Management Studio で、ディストリビューターになるサーバーに接続し (多くの場合、パブリッシャーとディストリビューターは同じサーバーです)、サーバー ノードを展開します。
[レプリケーション] フォルダーを右クリックし、[ディストリビューションの構成] をクリックします。
ディストリビューションの構成ウィザードに従って、次の操作を行います。
ディストリビューターを選択します。 ローカル ディストリビューターを使用するには、 '<ServerName>' を選択すると、独自のディストリビューターとして機能します。SQL Server によってディストリビューション データベースとログが作成されます。 リモート ディストリビューターを使用するには、[ 次のサーバーをディストリビューターとして使用する] を選択し、サーバーを選択します。 サーバーは既にディストリビューターとして構成されている必要があり、パブリッシャーがディストリビューターを使用できるようにするには有効にする必要があります。 詳細については、「 ディストリビューターでリモート パブリッシャーを有効にする (SQL Server Management Studio)」を参照してください。
リモート ディストリビューターを選択する場合は、パブリッシャーからディストリビューターへの接続の [管理パスワード ] ページにパスワードを入力する必要があります。 このパスワードは、パブリッシャーがリモート ディストリビューターで有効にされたときに指定されたパスワードと一致する必要があります。
ルート スナップショット フォルダーを指定します (ローカル ディストリビューターの場合)。 スナップショット フォルダーは、単に共有として指定したディレクトリです。このフォルダーの読み取りと書き込みを行うエージェントには、アクセスするための十分なアクセス許可が必要です。 このディストリビューターを使用する各パブリッシャーは、ルート フォルダーの下にフォルダーを作成し、各パブリケーションはスナップショット ファイルを格納する Publisher フォルダーの下にフォルダーを作成します。 フォルダーを適切にセキュリティで保護する方法の詳細については、「 スナップショット フォルダーのセキュリティ保護」を参照してください。
ディストリビューション データベースを指定します (ローカル ディストリビューターの場合)。 ディストリビューション データベースには、トランザクション レプリケーションのすべての種類のメタデータと履歴データが格納されます。
必要に応じて、他のパブリッシャーがディストリビューターを使用できるようにします。 他のパブリッシャーがディストリビューターの使用を有効にしている場合は、これらのパブリッシャーからディストリビューターへの接続の [ディストリビューター パスワード ] ページにパスワードを入力する必要があります。
必要に応じて、構成設定のスクリプトを作成します。 詳細については、「 レプリケーションのスクリプト作成」を参照してください。
Transact-SQL の使用
レプリケーションの発行と配布は、レプリケーション ストアド プロシージャを使用してプログラムで構成できます。
ローカル ディストリビューターを使用して発行を構成するには
sp_get_distributor (Transact-SQL) を実行して、サーバーがディストリビューターとして既に構成されているかどうかを判断します。
結果セットに インストールされた 値が 0 の場合は、マスター データベース のディストリビューターで sp_adddistributor (Transact-SQL) を実行します。
結果セットに インストールされているディストリビューション db の値が 0 の場合は、マスター データベース のディストリビューターで sp_adddistributiondb (Transact-SQL) を実行します。 @databaseのディストリビューション データベースの名前を指定します。 必要に応じて、 @max_distretention のトランザクションの最大保有期間と 、@history_retentionの履歴保持期間を指定できます。 新しいデータベースを作成する場合は、目的のデータベース プロパティ パラメーターを指定します。
パブリッシャーでもあるディストリビューターで、 sp_adddistpublisher (Transact-SQL) を実行し、 @working_directoryの既定のスナップショット フォルダーとして使用される UNC 共有を指定します。
パブリッシャーで、 sp_replicationdboption (Transact-SQL) を実行します。 パブリッシュするデータベース@dbname、レプリケーションの種類@optname、および@valueの
true
値を指定します。
リモート ディストリビューターを使用して発行を構成するには
sp_get_distributor (Transact-SQL) を実行して、サーバーがディストリビューターとして既に構成されているかどうかを判断します。
結果セットに インストールされた 値が 0 の場合は、マスター データベース のディストリビューターで sp_adddistributor (Transact-SQL) を実行します。 @passwordの強力なパスワードを指定します。 distributor_admin アカウントのこのパスワードは、ディストリビューターに接続するときにパブリッシャーによって使用されます。
結果セットに インストールされているディストリビューション db の値が 0 の場合は、マスター データベース のディストリビューターで sp_adddistributiondb (Transact-SQL) を実行します。 @databaseのディストリビューション データベースの名前を指定します。 必要に応じて、 @max_distretention のトランザクションの最大保有期間と 、@history_retentionの履歴保持期間を指定できます。 新しいデータベースを作成する場合は、目的のデータベース プロパティ パラメーターを指定します。
ディストリビューターで、 sp_adddistpublisher (Transact-SQL) を実行し、 @working_directoryの既定のスナップショット フォルダーとして使用される UNC 共有を指定します。 パブリッシャーへの接続時にディストリビューターが SQL Server 認証を使用する場合は、@security_modeには値 0、@loginと@passwordには Microsoft SQL Server ログイン情報も指定する必要があります。
マスター データベースのパブリッシャーで、 sp_adddistributor (Transact-SQL) を実行します。 @passwordの手順 1 で使用する強力なパスワードを指定 します。 このパスワードは、ディストリビューターに接続するときにパブリッシャーによって使用されます。
パブリッシャーで、 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'AdventureWorks2012';
-- 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 AdventureWorks2012 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 クラスを使用して、サーバーへの接続を作成します。
ReplicationServer クラスのインスタンスを作成します。 手順 1 の ServerConnection を渡します。
DistributionDatabase クラスのインスタンスを作成します。
Name プロパティをデータベース名に設定し、ConnectionContext プロパティを手順 1. のServerConnectionに設定します。
InstallDistributor メソッドを呼び出してディストリビューターをインストールします。 手順 3 の DistributionDatabase オブジェクトを渡します。
DistributionPublisher クラスのインスタンスを作成します。
DistributionPublisherの次のプロパティを設定します。
Name - パブリッシャーの名前。
ConnectionContext - 手順 1 の ServerConnection 。
DistributionDatabase - 手順 5 で作成したデータベースの名前。
WorkingDirectory - スナップショット ファイルへのアクセスに使用される共有。
PublisherSecurity - パブリッシャーに接続するときに使用されるセキュリティ モード。 WindowsAuthentication が推奨されます。
Create メソッドを呼び出します。
リモート ディストリビューターを使用して発行と配布を構成するには
ServerConnection クラスを使用して、リモート ディストリビューター サーバーへの接続を作成します。
ReplicationServer クラスのインスタンスを作成します。 手順 1 の ServerConnection を渡します。
DistributionDatabase クラスのインスタンスを作成します。
Name プロパティをデータベース名に設定し、ConnectionContext プロパティを手順 1. のServerConnectionに設定します。
InstallDistributor メソッドを呼び出してディストリビューターをインストールします。 手順 3. で、セキュリティで保護されたパスワード (リモート ディストリビューターに接続するときにパブリッシャーによって使用されます) と DistributionDatabase オブジェクトを指定します。 詳細については、「 ディストリビューターのセキュリティ保護」を参照してください。
重要
可能な場合は、実行時にセキュリティ資格情報の入力をユーザーに求めます。 資格情報を格納する必要がある場合は、Microsoft Windows .NET Framework によって提供される 暗号化サービス を使用します。
DistributionPublisher クラスのインスタンスを作成します。
DistributionPublisherの次のプロパティを設定します。
Name - ローカル パブリッシャー サーバーの名前。
Step 1 の ServerConnection は ConnectionContext です。
DistributionDatabase - 手順 5 で作成したデータベースの名前。
WorkingDirectory - スナップショット ファイルへのアクセスに使用される共有。
PublisherSecurity - パブリッシャーに接続するときに使用されるセキュリティ モード。 WindowsAuthentication が推奨されます。
Create メソッドを呼び出します。
ServerConnection クラスを使用して、ローカルパブリッシャーサーバーへの接続を作成します。
ReplicationServer クラスのインスタンスを作成します。 手順 9 の ServerConnection を渡します。
InstallDistributor メソッドを呼び出します。 リモート ディストリビューターの名前と、手順 5 で指定したリモート ディストリビューターのパスワードを渡します。
重要
可能な場合は、実行時にセキュリティ資格情報の入力をユーザーに求めます。 資格情報を保存する必要がある場合は、Windows .NET Framework に用意されている 暗号化サービス を使用します。
例 (RMO)
レプリケーション管理オブジェクト (RMO) を使用して、レプリケーションの発行と配布をプログラムで構成できます。
// Set the server and database names
string distributionDbName = "distribution";
string publisherName = publisherInstance;
string publicationDbName = "AdventureWorks2012";
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 AdventureWorks2012 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 occured 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 = "AdventureWorks2012"
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 AdventureWorks2012 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 occured when installing distribution and publishing.", ex)
Finally
conn.Disconnect()
End Try
こちらもご覧ください
ディストリビューターとパブリッシャーのプロパティの表示と変更
レプリケーション システム ストアド プロシージャの概念
[ディストリビューションの構成]
レプリケーション管理オブジェクトの概念
AlwaysOn 可用性グループのレプリケーションの構成 (SQL Server)