適用於:Linux 上的 SQL Server
在本教學課程中,於 Linux 上使用 Transact-SQL (T-SQL) 搭配兩個 SQL Server 執行個體來設定 SQL Server 快照式複寫。 發行者和散發者將會是相同的執行個體,且訂閱者將會位於個別的執行個體上。
- 在 Linux 上啟用 SQL Server 複寫代理程式
- 建立範例資料庫
- 為 SQL Server Agent 存取設定快照資料夾
- 設定分發者
- 設定發行者
- 設定出版品與文章
- 設定訂閱者
- 執行複寫作業
你可以用 複製儲存程序來配置所有複製元件。
先決條件
若要完成本教學課程,您需要:
具有 Linux 上的 SQL Server 最新版本的兩個 SQL Server 執行個體
一個用於發出 T-SQL 查詢以建立複寫的工具,例如 sqlcmd 或 SQL Server Management Studio(SSMS)
請參閲使用 Windows 上的 SQL Server Management Studio 來管理 Linux 上的 SQL Server。
Note
SQL Server 2017(14.x)CU 18 及更新版本支援在 Linux 上使用 SQL Server 複寫。 欲了解更多資訊,請參閱 Linux 上的 SQL Server 版本資訊。
詳細步驟
在 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];
在本教學課程中,您已在 Linux 上使用 T-SQL 搭配兩個 SQL Server 執行個體來設定 SQL Server 快照式複寫。
- 在 Linux 上啟用 SQL Server 複寫代理程式
- 建立範例資料庫
- 為 SQL Server Agent 存取設定快照資料夾
- 設定分發者
- 設定發行者
- 設定出版品與文章
- 設定訂閱者
- 執行複寫作業