Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Aplica-se a:SQL Server em Linux
Neste tutorial, configure a replicação de instantâneos do SQL Server no Linux com duas instâncias do SQL Server usando Transact-SQL (T-SQL). O editor e o distribuidor estão na mesma instância, e o assinante está em uma instância separada.
- Habilitar agentes de replicação do SQL Server no Linux
- Criar um banco de dados de exemplo
- Configurar pasta de instantâneo para acesso a agentes do SQL Server
- Configurar o distribuidor
- Configurar o editor
- Configurar a publicação e os artigos
- Configurar o assinante
- Executar os trabalhos de replicação
Pode configurar todos os componentes de replicação com procedimentos armazenados de replicação.
Pré-requisitos
Para concluir este tutorial, você precisa:
Duas instâncias do SQL Server com a versão mais recente do SQL Server no Linux
Uma ferramenta para emitir consultas T-SQL para configurar a replicação, como sqlcmd ou SQL Server Management Studio (SSMS)
Consulte Usar o SQL Server Management Studio no Windows para gerenciar o SQL Server no Linux.
Note
O SQL Server Replication é suportado no Linux no SQL Server 2017 (14.x) CU 18 e em versões posteriores. Para mais informações, consulte Informações de lançamento para SQL Server em Linux.
Passos detalhados
Habilite agentes de replicação do SQL Server no Linux. Em ambas as máquinas host, execute os seguintes comandos no terminal.
sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true sudo systemctl restart mssql-serverCrie o banco de dados e a tabela de exemplo. Na plataforma do editor, crie um banco de dados de exemplo e uma tabela que atuarão como os artigos de uma publicação.
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); GONa outra instância do SQL Server, o assinante, cria a base de dados para receber os artigos.
CREATE DATABASE Sales; GONo distribuidor, crie a pasta snapshot para os agentes do SQL Server lerem e escreverem, e conceda acesso ao
mssqlutilizador:sudo mkdir /var/opt/mssql/data/ReplData/ sudo chown mssql /var/opt/mssql/data/ReplData/ sudo chgrp mssql /var/opt/mssql/data/ReplData/Configura o distribuidor. Neste exemplo, o editor também é o distribuidor. Execute os seguintes comandos no publicador também para configurar a instância para distribuição.
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'; GOConfigura o editor. Execute os seguintes comandos T-SQL no publicador.
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'; GOConfigurar a tarefa do agente de publicação e de instantâneo. Execute os seguintes comandos T-SQL no publicador.
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;Crie o
Customerartigo a partir daCustomertabela.Execute os seguintes comandos T-SQL no publicador.
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';Configura a subscrição. Execute os seguintes comandos T-SQL no publicador.
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; GOExecute trabalhos do agente de replicação. Execute a seguinte consulta para obter uma lista de trabalhos:
SELECT name, date_modified FROM msdb.dbo.sysjobs ORDER BY date_modified DESC;Inicie o trabalho do agente de snapshot para gerar o snapshot:
USE msdb; GO -- Generate the publication snapshot, for example. EXECUTE dbo.sp_start_job N'PUBLISHER-PUBLICATION-SnapshotRepl-1'; GOIniciar o trabalho do agente de distribuição para distribuir a publicação ao assinante:
USE msdb; GO -- Distribute the publication to the subscriber. EXECUTE dbo.sp_start_job N'DISTRIBUTOR-PUBLICATION-SnapshotRepl-SUBSCRIBER'; GOLigue-se ao subscritor e consulte os dados replicados.
No assinante, verifique se a replicação está funcionando executando a seguinte consulta:
SELECT * FROM [Sales].[dbo].[Customer];
Neste tutorial, você configurou a replicação de instantâneos do SQL Server no Linux com duas instâncias do SQL Server usando T-SQL.
- Habilitar agentes de replicação do SQL Server no Linux
- Criar um banco de dados de exemplo
- Configurar pasta de instantâneo para acesso a agentes do SQL Server
- Configurar o distribuidor
- Configurar o editor
- Configurar a publicação e os artigos
- Configurar o assinante
- Executar os trabalhos de replicação