Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Berlaku untuk:SQL Server di Linux
Dalam tutorial ini, konfigurasikan replikasi rekam jepret SQL Server di Linux dengan dua instans SQL Server menggunakan Transact-SQL (T-SQL). Penerbit dan distributor berada pada instans yang sama, dan pelanggan berada pada instans terpisah.
- Mengaktifkan agen replikasi SQL Server di Linux
- Membuat sebuah basis data contoh
- Mengonfigurasi folder rekam jepret untuk akses agen SQL Server
- Mengonfigurasi distributor
- Mengonfigurasi penerbit
- Konfigurasikan publikasi dan artikel
- Konfigurasi pelanggan
- Jalankan pekerjaan replikasi
Anda dapat mengonfigurasi semua komponen replikasi dengan prosedur tersimpan replikasi.
Prasyarat
Untuk menyelesaikan tutorial ini, Anda memerlukan:
Dua instans SQL Server dengan versi terbaru SQL Server di Linux
Alat untuk mengeluarkan kueri T-SQL untuk menyiapkan replikasi, seperti sqlcmd atau SQL Server Management Studio (SSMS)
Lihat Menggunakan SQL Server Management Studio di Windows untuk mengelola SQL Server di Linux.
Note
SQL Server Replication didukung di Linux dalam SQL Server 2017 (14.x) CU 18 dan versi yang lebih baru. Untuk informasi lebih lanjut, lihat Informasi rilis untuk SQL Server on Linux.
Langkah-langkah terperinci
Aktifkan agen replikasi SQL Server di Linux. Pada kedua komputer host, jalankan perintah berikut di terminal.
sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true sudo systemctl restart mssql-serverBuat database sampel dan tabel. Pada penerbit, buat database sampel dan tabel yang akan bertindak sebagai artikel untuk publikasi.
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); GOPada instans SQL Server lainnya, pelanggan, buat database untuk menerima artikel.
CREATE DATABASE Sales; GOPada distributor, buat folder cuplikan yang digunakan agen SQL Server untuk membaca dan menulis, serta berikan akses kepada pengguna
mssql:sudo mkdir /var/opt/mssql/data/ReplData/ sudo chown mssql /var/opt/mssql/data/ReplData/ sudo chgrp mssql /var/opt/mssql/data/ReplData/Konfigurasikan distributor. Dalam contoh ini, penerbit juga merupakan distributor. Jalankan perintah berikut pada penerbit untuk mengonfigurasi instans untuk distribusi juga.
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'; GOKonfigurasikan penerbit. Jalankan perintah T-SQL berikut pada penerbit.
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'; GOKonfigurasi pekerjaan Agen Publikasi dan Snapshot Agent. Jalankan perintah T-SQL berikut pada penerbit.
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;Buat artikel
Customerdari tabelCustomertersebut.Jalankan perintah T-SQL berikut pada penerbit.
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';Konfigurasikan langganan. Jalankan perintah T-SQL berikut pada penerbit.
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; GOJalankan tugas agen replikasi. Jalankan kueri berikut untuk mendapatkan daftar pekerjaan:
SELECT name, date_modified FROM msdb.dbo.sysjobs ORDER BY date_modified DESC;Mulai pekerjaan agen snapshot untuk menghasilkan snapshot:
USE msdb; GO -- Generate the publication snapshot, for example. EXECUTE dbo.sp_start_job N'PUBLISHER-PUBLICATION-SnapshotRepl-1'; GOMulai pekerjaan agen distribusi untuk mendistribusikan publikasi kepada pelanggan:
USE msdb; GO -- Distribute the publication to the subscriber. EXECUTE dbo.sp_start_job N'DISTRIBUTOR-PUBLICATION-SnapshotRepl-SUBSCRIBER'; GOHubungkan ke subscriber dan lakukan kueri pada data yang direplikasi.
Pada server pelanggan, periksa apakah replikasi berfungsi dengan menjalankan kueri berikut:
SELECT * FROM [Sales].[dbo].[Customer];
Dalam tutorial ini, Anda mengonfigurasi replikasi rekam jepret SQL Server di Linux dengan dua instans SQL Server menggunakan T-SQL.
- Mengaktifkan agen replikasi SQL Server di Linux
- Membuat sebuah basis data contoh
- Mengonfigurasi folder rekam jepret untuk akses agen SQL Server
- Mengonfigurasi distributor
- Mengonfigurasi penerbit
- Konfigurasikan publikasi dan artikel
- Konfigurasi pelanggan
- Jalankan pekerjaan replikasi
Konten terkait
- Replikasi SQL Server
- Replikasi SQL Server di Linux
- prosedur tersimpan replikasi (Transact-SQL)