방법: 게시 및 배포 구성(복제 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에 기록 보존 기간을 지정할 수 있습니다. 새 데이터베이스를 만드는 경우 원하는 데이터베이스 속성 매개 변수를 지정합니다.

  2. 게시자이기도 한 배포자에서 sp_adddistpublisher(Transact-SQL)를 실행하고 @working_directory에 기본 스냅숏 폴더로 사용할 UNC 공유를 지정합니다.

  3. 게시자에서 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_mode에 값 0을 지정하고 @login@password에 Microsoft SQL Server 로그인 정보를 지정해야 합니다.

  3. master 데이터베이스의 게시자에서 sp_adddistributor(Transact-SQL)를 실행합니다. 이때 @password에 1단계에서 사용한 강력한 암호를 지정합니다. 이 암호는 배포자에 연결할 때 게시자에서 사용됩니다.

  4. 게시자에서 sp_replicationdboption(Transact-SQL)을 실행합니다. 이때 @dbname에 게시할 데이터베이스를 지정하고 @optname에 복제 유형을 지정하며 @value에 true 값을 지정합니다.

다음 예제에서는 게시 및 배포를 프로그래밍 방식으로 구성하는 방법을 보여 줍니다. 이 예제에서 게시자 및 로컬 배포자로 구성할 서버의 이름은 스크립팅 변수를 사용하여 제공됩니다.

-- 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'AdventureWorks2008R2'; 

-- 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 AdventureWorks2008R2 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