T-SQL을 사용하여 복제 구성

적용 대상:SQL Server - Linux

이 자습서에서는 T-SQL(Transact-SQL)을 사용하여 SQL Server의 두 인스턴스를 사용하여 Linux에서 SQL Server 스냅샷 복제본(replica)tion을 구성합니다. 게시자와 배포자는 동일한 인스턴스이고, 구독자는 별도의 인스턴스에 있습니다.

  • Linux에서 SQL Server 복제본(replica)tion 에이전트 사용
  • 샘플 데이터베이스 만들기
  • SQL Server 에이전트 액세스를 위해 스냅샷 폴더 구성
  • 배포자 구성
  • 게시자 구성
  • 게시 및 아티클 구성
  • 구독자 구성
  • 복제본(replica)tion 작업 실행

모든 복제 구성은 복제 저장 프로시저를 사용하여 구성할 수 있습니다.

필수 조건

이 자습서를 완료하려면 다음이 필요합니다.

자세한 단계

  1. Linux에서 SQL Server 복제본(replica)tion 에이전트를 사용하도록 설정합니다. 두 호스트 컴퓨터에서 터미널에서 다음 명령을 실행합니다.

    sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
    sudo systemctl restart mssql-server
    
  2. 샘플 데이터베이스 및 테이블을 만듭니다. 게시자에서 게시의 아티클 역할을 하는 샘플 데이터베이스와 테이블을 만듭니다.

    CREATE DATABASE Sales;
    GO
    
    USE [Sales];
    GO
    
    CREATE TABLE Customer (
        [CustomerID] [int] NOT NULL,
        [SalesAmount] [decimal] NOT NULL
    );
    GO
    INSERT INTO Customer (CustomerID, SalesAmount) VALUES (1, 100), (2, 200), (3, 300);
    GO
    

    다른 SQL Server 인스턴스인 구독자에서 아티클을 받을 데이터베이스를 만듭니다.

    CREATE DATABASE Sales;
    GO
    
  3. SQL Server 에이전트가 배포자에서 읽고 쓰는 스냅샷 폴더를 만들고, 스냅샷 폴더를 만들고 ‘mssql’ 사용자에게 액세스 권한을 부여합니다.

    sudo mkdir /var/opt/mssql/data/ReplData/
    sudo chown mssql /var/opt/mssql/data/ReplData/
    sudo chgrp mssql /var/opt/mssql/data/ReplData/
    
  4. 배포자를 구성합니다. 이 예에서는 게시자도 배포자가 됩니다. 게시자에서 다음 명령을 실행하여 배포할 인스턴스도 구성합니다.

    DECLARE @distributor AS SYSNAME;
    DECLARE @distributorlogin AS SYSNAME;
    DECLARE @distributorpassword AS SYSNAME;
    
    -- Specify the distributor name. Use 'hostname' command on in terminal to find the hostname
    SET @distributor = N'<distributor instance name>'; --in this example, it will be the name of the publisher
    SET @distributorlogin = N'<distributor login>';
    SET @distributorpassword = N'<distributor password>';
    
    -- Specify the distribution database.
    USE master
    
    EXEC sp_adddistributor @distributor = @distributor -- this should be the hostname
    
    -- Log into distributor and create Distribution Database. In this example, our publisher and distributor is on the same host
    EXEC sp_adddistributiondb @database = N'distribution',
        @log_file_size = 2,
        @deletebatchsize_xact = 5000,
        @deletebatchsize_cmd = 2000,
        @security_mode = 0,
        @login = @distributorlogin,
        @password = @distributorpassword;
    GO
    
    DECLARE @snapshotdirectory AS NVARCHAR(500);
    SET @snapshotdirectory = N'/var/opt/mssql/data/ReplData/';
    
    -- Log into distributor and create Distribution Database. In this example, our publisher and distributor is on the same host
    USE [distribution];
    GO
    
    IF (NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'UIProperties' AND type = 'U'))
        CREATE TABLE UIProperties (id INT);
    
    IF (EXISTS (SELECT * FROM::fn_listextendedproperty('SnapshotFolder', 'user', 'dbo', 'table', 'UIProperties', NULL, NULL)))
        EXEC sp_updateextendedproperty N'SnapshotFolder',
            @snapshotdirectory,
            'user',
            dbo,
            'table',
            'UIProperties';
    ELSE
        EXEC sp_addextendedproperty N'SnapshotFolder',
            @snapshotdirectory,
            'user',
            dbo,
            'table',
            'UIProperties';
    GO
    
  5. 게시자를 구성합니다. 게시자에서 다음 T-SQL 명령을 실행합니다.

    DECLARE @publisher AS SYSNAME;
    DECLARE @distributorlogin AS SYSNAME;
    DECLARE @distributorpassword AS SYSNAME;
    
    -- Specify the distributor name. Use 'hostname' command on in terminal to find the hostname
    SET @publisher = N'<instance name>';
    SET @distributorlogin = N'<distributor login>';
    SET @distributorpassword = N'<distributor password>';
    
    -- Specify the distribution database.
    -- Adding the distribution publishers
    EXEC sp_adddistpublisher @publisher = @publisher,
        @distribution_db = N'distribution',
        @security_mode = 0,
        @login = @distributorlogin,
        @password = @distributorpassword,
        @working_directory = N'/var/opt/mssql/data/ReplData',
        @trusted = N'false',
        @thirdparty_flag = 0,
        @publisher_type = N'MSSQLSERVER';
    GO
    
  6. 게시 작업을 구성합니다. 게시자에서 다음 T-SQL 명령을 실행합니다.

    DECLARE @replicationdb AS SYSNAME;
    DECLARE @publisherlogin AS SYSNAME;
    DECLARE @publisherpassword AS SYSNAME;
    
    SET @replicationdb = N'Sales';
    SET @publisherlogin = N'<Publisher login>';
    SET @publisherpassword = N'<Publisher Password>';
    
    USE [Sales];
    GO
    
    EXEC sp_replicationdboption @dbname = N'Sales',
        @optname = N'publish',
        @value = N'true';
    
    -- Add the snapshot publication
    EXEC sp_addpublication @publication = N'SnapshotRepl',
        @description = N'Snapshot publication of database ''Sales'' from Publisher ''<PUBLISHER HOSTNAME>''.',
        @retention = 0,
        @allow_push = N'true',
        @repl_freq = N'snapshot',
        @status = N'active',
        @independent_agent = N'true';
    
    EXEC sp_addpublication_snapshot @publication = N'SnapshotRepl',
        @frequency_type = 1,
        @frequency_interval = 1,
        @frequency_relative_interval = 1,
        @frequency_recurrence_factor = 0,
        @frequency_subday = 8,
        @frequency_subday_interval = 1,
        @active_start_time_of_day = 0,
        @active_end_time_of_day = 235959,
        @active_start_date = 0,
        @active_end_date = 0,
        @publisher_security_mode = 0,
        @publisher_login = @publisherlogin,
        @publisher_password = @publisherpassword;
    
  7. Sales 테이블에서 아티클을 만듭니다.

    게시자에서 다음 T-SQL 명령을 실행합니다.

    USE [Sales];
    GO
    
    EXEC sp_addarticle @publication = N'SnapshotRepl',
        @article = N'customer',
        @source_owner = N'dbo',
        @source_object = N'customer',
        @type = N'logbased',
        @description = NULL,
        @creation_script = NULL,
        @pre_creation_cmd = N'drop',
        @schema_option = 0x000000000803509D,
        @identityrangemanagementoption = N'manual',
        @destination_table = N'customer',
        @destination_owner = N'dbo',
        @vertical_partition = N'false';
    
  8. 구독을 구성합니다. 게시자에서 다음 T-SQL 명령을 실행합니다.

    DECLARE @subscriber AS SYSNAME;
    DECLARE @subscriber_db AS SYSNAME;
    DECLARE @subscriberLogin AS SYSNAME;
    DECLARE @subscriberPassword AS SYSNAME;
    
    SET @subscriber = N'<Instance Name>'; -- for example, MSSQLSERVER
    SET @subscriber_db = N'Sales';
    SET @subscriberLogin = N'<Subscriber Login>';
    SET @subscriberPassword = N'<Subscriber Password>';
    
    USE [Sales];
    GO
    
    EXEC sp_addsubscription @publication = N'SnapshotRepl',
        @subscriber = @subscriber,
        @destination_db = @subscriber_db,
        @subscription_type = N'Push',
        @sync_type = N'automatic',
        @article = N'all',
        @update_mode = N'read only',
        @subscriber_type = 0;
    
    EXEC sp_addpushsubscription_agent @publication = N'SnapshotRepl',
        @subscriber = @subscriber,
        @subscriber_db = @subscriber_db,
        @subscriber_security_mode = 0,
        @subscriber_login = @subscriberLogin,
        @subscriber_password = @subscriberPassword,
        @frequency_type = 1,
        @frequency_interval = 0,
        @frequency_relative_interval = 0,
        @frequency_recurrence_factor = 0,
        @frequency_subday = 0,
        @frequency_subday_interval = 0,
        @active_start_time_of_day = 0,
        @active_end_time_of_day = 0,
        @active_start_date = 0,
        @active_end_date = 19950101;
    GO
    
  9. 복제본(replica) 에이전트 작업을 실행합니다. 다음 쿼리를 실행하여 작업 목록을 가져옵니다.

    SELECT name, date_modified
    FROM msdb.dbo.sysjobs
    ORDER BY date_modified DESC;
    

    스냅샷 복제본(replica)tion 작업을 실행하여 스냅샷 생성합니다.

    USE msdb;
    GO
    
    --generate snapshot of publications, for example
    EXEC dbo.sp_start_job N'PUBLISHER-PUBLICATION-SnapshotRepl-1';
    GO
    

    스냅샷 복제 작업을 실행하여 스냅샷을 생성합니다.

    USE msdb;
    GO
    --distribute the publication to subscriber, for example
    EXEC dbo.sp_start_job N'DISTRIBUTOR-PUBLICATION-SnapshotRepl-SUBSCRIBER';
    GO
    
  10. 구독자 및 쿼리 복제본(replica) 데이터 커넥트.

    구독자에서 다음 쿼리를 실행하여 복제본(replica)tion이 작동하는지 검사.

    SELECT * from [Sales].[dbo].[Customer];
    

이 자습서에서는 T-SQL을 사용하여 SQL Server의 두 인스턴스를 사용하여 Linux에서 SQL Server 스냅샷 복제본(replica)tion을 구성했습니다.

  • Linux에서 SQL Server 복제본(replica)tion 에이전트 사용
  • 샘플 데이터베이스 만들기
  • SQL Server 에이전트 액세스를 위해 스냅샷 폴더 구성
  • 배포자 구성
  • 게시자 구성
  • 게시 및 아티클 구성
  • 구독자 구성
  • 복제본(replica)tion 작업 실행