T-SQL を使用してレプリケーションを構成する

適用対象:SQL Server - Linux

このチュートリアルでは、Transact-SQL (T-SQL) を使用して、SQL Server の 2 つのインスタンスにより、Linux 上で SQL Server スナップショット レプリケーションを構成します。 パブリッシャーとディストリビューターは同じインスタンスになり、サブスクライバーは別のインスタンスに展開されます。

  • Linux 上で SQL Server レプリケーション エージェントを有効にする
  • サンプル データベースの作成
  • SQL Server エージェントのアクセス用にスナップショット フォルダーを構成する
  • ディストリビューターの構成
  • パブリッシャーを構成する
  • パブリケーションとアーティクルを構成する
  • サブスクライバーを構成する
  • レプリケーション ジョブを実行する

すべてのレプリケーション構成は、レプリケーション ストアド プロシージャを使用して構成できます。

前提条件

このチュートリアルを完了するには、次のものが必要です。

  • 最新バージョンの SQL Server on Linux を使用した SQL Server の 2 つのインスタンス

  • SQLCMD や SSMS などのレプリケーションを設定する T-SQL クエリを発行するツール

    Windows で SQL Server Management Studio を使用して SQL Server on Linux を管理する」を参照してください。

    Note

    SQL Server 2017 (14.x) (CU18) 以降では、SQL Server on Linux のインスタンス用の SQL Server レプリケーションがサポートされています。

詳細な手順

  1. Linux 上で SQL Server レプリケーション エージェントを有効にします。 両方のホスト マシン上のターミナルで次のコマンドを実行します。

    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. レプリケーション エージェント ジョブを実行します。 次のクエリを実行してジョブの一覧を取得します。

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

    スナップショット レプリケーション ジョブを実行してスナップショットを生成します。

    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. サブスクライバーに接続し、レプリケートされたデータを照会します。

    サブスクライバーで、次のクエリを実行してレプリケーションが機能していることを確認します。

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

このチュートリアルでは、T-SQL を使用して、SQL Server の 2 つのインスタンスにより、Linux 上で SQL Server スナップショット レプリケーションを構成しました。

  • Linux 上で SQL Server レプリケーション エージェントを有効にする
  • サンプル データベースの作成
  • SQL Server エージェントのアクセス用にスナップショット フォルダーを構成する
  • ディストリビューターの構成
  • パブリッシャーを構成する
  • パブリケーションとアーティクルを構成する
  • サブスクライバーを構成する
  • レプリケーション ジョブを実行する