적용 대상:SQL Server on Linux
이 자습서에서는 T-SQL(Transact-SQL)을 사용하여 두 개의 SQL Server 인스턴스로 Linux에서 SQL Server 스냅샷 복제를 구성합니다. 게시자와 배포자는 동일한 인스턴스이고, 구독자는 별도의 인스턴스에 있습니다.
- Linux에서 SQL Server 복제 에이전트를 사용하도록 설정
- 샘플 데이터베이스 만들기
- SQL Server 에이전트 액세스를 위해 스냅샷 폴더 구성
- 배포자 설정
- 게시자 구성
- 출판물과 기사 구성
- 가입자 설정
- 복제 작업 실행
모든 복제 구성 요소는 복제 저장 프로시저로 구성할 수 있습니다.
Prerequisites
이 자습서를 완료하려면 다음이 필요합니다.
최신 버전의 SQL Server on Linux를 사용하는 두 개의 SQL Server 인스턴스
sqlcmd 또는 SSMS(SQL Server Management Studio)와 같은 복제를 설정하기 위해 T-SQL 쿼리를 실행하는 도구
Windows에서 SQL Server Management Studio를 사용하여 SQL Server on Linux 관리를 참조하세요.
메모
Linux에서 SQL Server 복제는 SQL Server 2017(14.x) CU 18 이상 버전에서 지원됩니다.
자세한 단계
Linux에서 SQL Server 복제 에이전트를 사용하도록 설정합니다. 두 호스트 컴퓨터 모두 터미널에서 다음 명령을 실행합니다.
sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true sudo systemctl restart mssql-server샘플 데이터베이스 및 테이블을 만듭니다. 퍼블리셔에서 발행물의 기사 역할을 할 샘플 데이터베이스와 테이블을 만듭니다.
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배포판에서 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/디스트리뷰터를 구성하세요. 이 예에서는 게시자도 배포자가 됩니다. 게시자에서 다음 명령을 실행하여 배포할 인스턴스도 구성합니다.
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퍼블리셔를 설정하세요. 게시자에서 다음 T-SQL 명령을 실행합니다.
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출판 및 스냅샷 에이전트 작업을 설정하세요. 게시자에서 다음 T-SQL 명령을 실행합니다.
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;Customer테이블을 기반으로Customer문서를 작성하세요.게시자에서 다음 T-SQL 명령을 실행합니다.
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';구독을 구성합니다. 게시자에서 다음 T-SQL 명령을 실행합니다.
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복제 에이전트 작업을 실행합니다. 다음 쿼리를 실행하여 작업 목록을 가져옵니다.
SELECT name, date_modified FROM msdb.dbo.sysjobs ORDER BY date_modified DESC;스냅샷 에이전트 작업을 시작하여 스냅샷을 생성하세요:
USE msdb; GO -- Generate the publication snapshot, for example. EXECUTE dbo.sp_start_job N'PUBLISHER-PUBLICATION-SnapshotRepl-1'; GO배포 에이전트 작업을 시작하여 구독자에게 출판물을 배포하세요:
USE msdb; GO -- Distribute the publication to the subscriber. EXECUTE dbo.sp_start_job N'DISTRIBUTOR-PUBLICATION-SnapshotRepl-SUBSCRIBER'; GO가입자에 연결하여 복제된 데이터를 쿼리합니다.
구독자에서 다음 쿼리를 실행하여 복제가 작동하는지 확인합니다.
SELECT * FROM [Sales].[dbo].[Customer];
이 자습서에서는 T-SQL을 사용하여 두 개의 SQL Server 인스턴스로 Linux에서 SQL Server 스냅샷 복제를 구성했습니다.
- Linux에서 SQL Server 복제 에이전트를 사용하도록 설정
- 샘플 데이터베이스 만들기
- SQL Server 에이전트 액세스를 위해 스냅샷 폴더 구성
- 배포자 설정
- 게시자 구성
- 출판물과 기사 구성
- 가입자 설정
- 복제 작업 실행