Replikation mit Transact-SQL konfigurieren

Gilt für:SQL Server unter Linux

In diesem Tutorial konfigurieren Sie die Replikation von SQL Server-Momentaufnahmen unter Linux mit zwei Instanzen von SQL Server mithilfe von Transact-SQL (T-SQL). Der Herausgeber und der Verteiler sind auf derselben Instanz, und der Abonnent befindet sich auf einer separaten Instanz.

  • Aktivieren von SQL Server-Replikations-Agents unter Linux
  • Erstellen einer Beispieldatenbank
  • Konfigurieren des Momentaufnahmeordners für den Zugriff durch SQL Server-Agents
  • Konfigurieren des Distributors
  • Konfigurieren des Herausgebers
  • Konfigurieren Sie die Veröffentlichung und die Artikel
  • Konfigurieren Sie den Abonnenten
  • Führen Sie die Replikationsaufträge aus.

Du kannst alle Replikationskomponenten mit gespeicherten Replikationsprozeduren konfigurieren.

Voraussetzungen

Um die Schritte dieses Tutorials abzuschließen, benötigen Sie Folgendes:

Detaillierte Schritte

  1. Aktivieren Sie SQL Server-Replikations-Agents unter Linux. Führen Sie auf beiden Hostcomputern die folgenden Befehle im Terminal aus.

    sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
    sudo systemctl restart mssql-server
    
  2. Erstellen Sie die Beispieldatenbank und -tabelle. Erstellen Sie auf dem Verleger eine Beispieldatenbank und eine Tabelle, die als Artikel für eine Veröffentlichung fungieren.

    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
    

    Erstellen Sie auf der anderen SQL Server-Instanz, dem Abonnenten, die Datenbank, um die Artikel zu empfangen.

    CREATE DATABASE Sales;
    GO
    
  3. Erstelle im Distributor den Snapshot-Ordner für SQL Server-Agenten zum Lesen und Schreiben und gewähre dem mssql Benutzer den Zugriff:

    sudo mkdir /var/opt/mssql/data/ReplData/
    sudo chown mssql /var/opt/mssql/data/ReplData/
    sudo chgrp mssql /var/opt/mssql/data/ReplData/
    
  4. Konfigurieren Sie den Verteiler. In diesem Beispiel ist der Herausgeber gleichzeitig auch der Distributor. Führen Sie die folgenden Befehle auf dem Verleger aus, um die Instanz ebenfalls für die Verteilung zu konfigurieren.

    DECLARE @distributor AS SYSNAME;
    DECLARE @distributorlogin AS SYSNAME;
    DECLARE @distributorpassword AS SYSNAME;
    
    -- Specify the distributor name. Use the 'hostname' command in the 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;
    
    EXECUTE sp_adddistributor
        @distributor = @distributor; -- this should be the hostname
    
    -- Log into the distributor and create the distribution database.
    -- In this example, the publisher and distributor are on the same host.
    EXECUTE sp_adddistributiondb
        @database = N'distribution',
        @log_file_size = 2,
        @deletebatchsize_xact = 5000,
        @deletebatchsize_cmd = 2000,
        @security_mode = 0,
        @login = @distributorlogin,
        @password = @distributorpassword;
    GO
    
    -- Log into the distributor and configure the snapshot directory.
    -- In this example, the publisher and distributor are on the same host.
    USE [distribution];
    GO
    
    DECLARE @snapshotdirectory AS NVARCHAR (500) = N'/var/opt/mssql/data/ReplData/';
    
    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)))
        EXECUTE sp_updateextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties';
    ELSE
        EXECUTE sp_addextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties';
    GO
    
  5. Konfigurieren Sie den Publisher. Führen Sie die folgenden T-SQL-Befehle auf dem Herausgeber aus.

    DECLARE @publisher AS SYSNAME;
    DECLARE @distributorlogin AS SYSNAME;
    DECLARE @distributorpassword AS SYSNAME;
    
    -- Specify the publisher name. Use the 'hostname' command in the 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
    EXECUTE 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. Konfigurieren Sie den Veröffentlichungs- und Snapshot-Agent-Job. Führen Sie die folgenden T-SQL-Befehle auf dem Herausgeber aus.

    USE [Sales];
    GO
    
    DECLARE @publisherlogin AS SYSNAME;
    DECLARE @publisherpassword AS SYSNAME;
    
    SET @publisherlogin = N'<publisher login>';
    SET @publisherpassword = N'<publisher password>';
    
    EXECUTE sp_replicationdboption
        @dbname = N'Sales',
        @optname = N'publish',
        @value = N'true';
    
    -- Add the snapshot publication
    EXECUTE 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';
    
    EXECUTE 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. Erstellen Sie den Customer Artikel aus der Tabelle Customer .

    Führen Sie die folgenden T-SQL-Befehle auf dem Herausgeber aus.

    USE [Sales];
    GO
    
    EXECUTE 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,
        @destination_table = N'Customer',
        @destination_owner = N'dbo',
        @identityrangemanagementoption = N'manual',
        @vertical_partition = N'false';
    
  8. Konfigurieren Sie das Abonnement. Führen Sie die folgenden T-SQL-Befehle auf dem Herausgeber aus.

    USE [Sales];
    GO
    
    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>';
    
    EXECUTE 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;
    
    EXECUTE 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. Führen Sie die Aufgaben des Replikations-Agents aus. Führen Sie die folgende Abfrage aus, um eine Liste der Aufträge zu erhalten:

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

    Starte den Job des Snapshot-Agenten, um den Snapshot zu generieren:

    USE msdb;
    GO
    
    -- Generate the publication snapshot, for example.
    EXECUTE dbo.sp_start_job N'PUBLISHER-PUBLICATION-SnapshotRepl-1';
    GO
    

    Starten Sie die Aufgabe des Vertriebsagenten, um die Veröffentlichung an den Abonnenten zu verteilen:

    USE msdb;
    GO
    
    -- Distribute the publication to the subscriber.
    EXECUTE dbo.sp_start_job N'DISTRIBUTOR-PUBLICATION-SnapshotRepl-SUBSCRIBER';
    GO
    
  10. Verbinden Sie sich mit dem Abonnenten und fragen Sie die replizierten Daten ab.

    Überprüfen Sie auf dem Abonnenten, ob die Replikation funktioniert, indem Sie die folgende Abfrage ausführen:

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

In diesem Tutorial konfigurierten Sie die Replikation von SQL Server-Momentaufnahmen unter Linux mit zwei Instanzen von SQL Server mithilfe von T-SQL.

  • Aktivieren von SQL Server-Replikations-Agents unter Linux
  • Erstellen einer Beispieldatenbank
  • Konfigurieren des Momentaufnahmeordners für den Zugriff durch SQL Server-Agents
  • Konfigurieren des Distributors
  • Konfigurieren des Herausgebers
  • Konfigurieren Sie die Veröffentlichung und die Artikel
  • Konfigurieren Sie den Abonnenten
  • Führen Sie die Replikationsaufträge aus.