In this article, we will talk about how to send the SQL Server Transactional Replication on Linux Environment.
在本文中,我们将讨论如何在Linux环境上发送SQL Server事务复制。
Replication is a procedure to deal with various copies of same data at not at all like each other hubs. Microsoft SQL Server underpins a few distinctive sort Data Synchronization procedures like (Merge Replication, Transactional Replication, Peer to Peer Replication and Snapshot Replication).
复制是一个处理彼此完全不同的相同数据的各种副本的过程。 Microsoft SQL Server支持一些独特的排序数据同步过程,例如 (合并复制,事务复制,对等复制和快照复制)。
The Replication is one of the features given by Microsoft SQL Server and it is accessible with SQL Server on Linux as well. A few of the features are not gotten on Linux based SQL Server version, which is firmly coordinated with Windows Operating System. Presently let us perceive how the Transactional Replication functions in SQL Server on Windows and Linux Operation Systems:
复制是Microsoft SQL Server提供的功能之一,并且在Linux上也可以通过SQL Server访问。 在基于LinuxSQL Server版本上没有获得一些功能,该版本与Windows操作系统紧密协作。 当前,让我们了解事务复制在Windows和Linux操作系统上SQL Server中如何起作用:
事务复制如何工作? (How Transactional Replication works?)
The Transactional Replication is a procedure to synchronize information from one database (Publisher) to different databases (Subscriber). The job of information synchronization arbiter is performed by Distributor (Distribution database) which can be arranged on the Publisher site, Subscriber Site or some other occasion too.
事务复制是一种将信息从一个数据库(发布者)同步到不同数据库(订阅者)的过程。 信息同步仲裁器的工作由分发服务器(分发数据库)执行,分发服务器也可以安排在发布者站点,订阅者站点或其他一些场合。
Exchange Replication is methodical overseen by Log Reader Agent and Distributor Agent. The usefulness of Log-Reader Agent is to catch the information changes from Publisher by Transaction log and move it to Distribution database.
Exchange复制由Log Reader代理和Distributor代理有条不紊地进行监督。 Log-Reader代理的作用是通过事务日志捕获来自Publisher的信息更改,并将其移动到分发数据库。
To catch the new information changes the Distributor Agent is required, it catches the new information changes, for example, addition or schema changes in conveyance database and move them to Subscriber database utilizing Primary key reference of the table.
为了捕获新的信息更改,需要Distributor Agent,它会捕获新的信息更改,例如,传输数据库中的添加或架构更改,并使用表的主键引用将其移动到订户数据库。
Presently before setting up the Transactional Replication, initially we need the Distributor to be arranged to utilize SSMS, which can be designed inside a couple of steps. Taking a gander at the means beneath we understand that the other Replication steps and errands are additionally simple to continue in SSMS, for example, Generate Snapshot, Re-introduction, Start/Stop Synchronization, Changes in Agent Profile and Replication Monitor for issue troubleshooting.
目前,在设置事务复制之前,我们首先需要安排分发服务器以利用SSMS,可以在几个步骤中进行设计。 仔细研究一下下面的方法,我们了解到其他复制步骤和步骤在SSMS中也很容易继续进行,例如,生成快照,重新引入,启动/停止同步,代理配置文件中的更改以及用于问题排查的复制监视器。
While working with SQL Server on Linux, the client needs to chip away at SQL Command-Line as it were. The client can associate that Linux SQL Server occurrence on SSMS by the remote association at any rate. This is as of now introduced in Windows Operating System, anyway the best practice with Linux Environment is to do troubleshooting and execution done by core application command as it were.
在Linux上使用SQL Server时,客户端需要按原样使用SQL Command-Line。 客户端可以通过远程关联以任何速率关联Linux SQL Server在SSMS上的出现。 到目前为止,这是Windows操作系统中引入的,无论如何,Linux环境的最佳实践是照原样由核心应用程序命令进行故障排除和执行。
Transactional Replication Process steps:
事务复制过程的步骤:
- Configure Distribution (If not Exists) 配置分发(如果不存在)
- Add Publisher & Articles 添加发布者和文章
- Add Subscriber 添加订户
- Generate Snapshot 产生快照
Configure Distribution
配置分配
Interface SQL Server database engine in Linux utilizing,
在Linux中使用以下接口连接SQL Server数据库引擎:
sqlcmd -S localhost -U SA
execute underneath commands in Query Command line tool to arrange Distributor inside distribution database and characterize the snapshot folder.
在查询命令行工具中的命令下方执行,以在分发数据库中安排分发服务器并表征快照文件夹。
USE master
GO
EXEC sp_adddistributor @distributor = N'sqltranrepl'
GO
EXEC sp_adddistributiondb @database = N'distribution', @data_folder = N'/var/opt/mssql/data', @log_folder = N'/var/opt/mssql/data', @log_file_size = 2, @min_distretention = 0, @max_distretention = 72, @history_retention = 48, @deletebatchsize_xact = 5000, @deletebatchsize_cmd = 2000, @security_mode = 1
GO
SELECT name, create_date FROM sys.databases
GO
@distributor is the value of the name of SQL Server instance, it is the place you will configure the Distribution database. As referenced over, the Distribution database can be designed on different SQL Server instance too. In the above model, we have utilized the same SQL Server Instance for Publisher, Subscriber, and Distributor.
@distributor是SQL Server实例名称的值,它是您将配置分发数据库的位置。 如上所述,分发数据库也可以设计在不同SQL Server实例上。 在上述模型中,我们为发布者,订阅者和分发者使用了相同SQL Server实例。
@data_folder and @log_folder is utilized to define Data file and Log file in referenced index or Directory. As a matter of course, it utilizes /var/opt/mssql/. In case you need to keep the Data in other than default registry than make a point to give expected authorizations and permissions to directory in advance executing the above command. After finishing the execution of above statements the confirmation should be possible in the sys.databases Table.
@data_folder和@log_folder用于在引用的索引或目录中定义数据文件和日志文件。 当然,它利用/ var / opt / mssql / 。 如果您需要将数据保留在默认注册表之外的其他地方,则可以在执行上述命令之前预先给目录指定预期的授权和许可。 完成上述语句的执行后,应该可以在sys.databases表中进行确认。
![List database to track distribution database.](https://www.sqlshack.com/wp-content/uploads/2019/09/list-database-to-track-distribution-database-.jpeg)
We have to ensure getting the right Linux based SQL Server Edition to be introduced. generally, won’t bolster the Replication feature. In that case, SQL Server won’t permit to configure distribution too.
我们必须确保引入正确的基于LinuxSQL Server Edition。 通常,不会支持复制功能。 在这种情况下,SQL Server也将不允许配置分发。
![Error on Distribution Configuration](https://www.sqlshack.com/wp-content/uploads/2019/09/error-on-distribution-configuration.png)
To make a Snapshot folder and incorporate it with Distribution database. We have utilized /var/opt/mssql/data/repl_data in this model. thusly, before executing the following stage, make directory of the repl_data folder and enable users to make it available by giving fitting authorizations or permissions on the folder.
制作一个Snapshot文件夹并将其合并到分发数据库中。 我们在此模型中使用了/ var / opt / mssql / data / repl_data 。 因此,在执行下一阶段之前,请创建repl_data文件夹的目录,并使用户可以通过对该文件夹提供合适的授权或许可来使其可用。
chmod -R u+rX /var/opt/mssql/data/repl_data
chmod a+rwx /var/opt/mssql/data/repl_data
USE [distribution]
IF (NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'UIProperties' AND type = 'U '))
CREATE TABLE UIProperties(id INT)
GO
IF (EXISTS (SELECT * FROM ::fn_listextendedproperty('SnapshotFolder', 'user', 'dbo', 'table', 'UIProperties', null, null)))
EXEC sp_updateextendedproperty N'SnapshotFolder', N'/var/opt/mssql/data/repl_data', 'user', dbo, 'table', 'UIProperties'
ELSE
EXEC sp_addextendedproperty N'SnapshotFolder', N'/var/opt/mssql/data/repl_data', 'user', dbo, 'table', 'UIProperties'
GO
EXEC sp_adddistpublisher @publisher = N'sqltranrepl', @distribution_db = N'distribution', @security_mode = 1, @working_directory = N'/var/opt/mssql/data/repl_data', @trusted = N'false', @thirdparty_flag = 0, @publisher_type = N'MSSQLSERVER'
GO
Following the above steps will complete the configuration of Distribution with prerequisites.
执行上述步骤将完成先决条件的分发配置。
添加发布者和文章(使用Log Reader Agent) (Add Publisher & Articles (With Log Reader Agent))
包括两个新数据库 pub_db(Publisher) and pub_db (Publisher)和 sub_db(Subscriber) with table as underneath, sub_db (Subscriber),表位于下方,CREATE DATABASE pub_db
CREATE DATABASE sub_db
GO
use pub_db
GO
CREATE TABLE [dbo].[data_in](
[id] [bigint] IDENTITY(1,1) PRIMARY KEY,
[is_active] [bit] NULL
)
GO
See here, all Transaction articles must contain a Primary Key. Presently before including the Publisher, the Replication must be enabled on Publisher Database. The underneath command will enable the database to be utilized as a replication at Publisher or Subscriber. It could be utilized in Merge Replication, Transactional Replication, and Snapshot Replication too.
请参阅此处,所有“交易”文章都必须包含主键。 目前,在包括发布服务器之前,必须在发布服务器数据库上启用复制。 underneath命令将使数据库可以用作发布服务器或订阅服务器上的复制。 它也可以用于合并复制,事务复制和快照复制。
USE MASTER
EXEC sp_replicationdboption @dbname = N'pub_db', @optname = N'publish', @value = N'true'
GO
See here, the value of @optname parameter will be diverse as purpose behind database in Replication, for example, publish, merge publish, subscribe, etc.
参见此处, @ optname参数的值将根据复制中数据库的用途而有所不同,例如,发布,合并发布,订阅等。
In the event of including Log-reader agent on Publisher database, using command underneath:
如果在发布者数据库中包括Log-reader代理,请使用下面的命令:
EXEC [pub_db].sys.sp_addlogreader_agent @job_login = null, @job_password = null, @publisher_security_mode = 1
GO
![Configure Log Reader Agent in transaction Replication.](https://www.sqlshack.com/wp-content/uploads/2019/09/configure-log-reader-agent-in-transaction-replicat.jpeg)
Now, we will include Publisher with Publication name as linux_tran_repl. We utilized rest of parameters with default esteems (see the query underneath).
现在,我们将包括发布服务器,其发布名称为linux_tran_repl 。 我们使用了带有默认值的其余参数(请参阅下面的查询)。
添加出版物 (Adding Publication)
-- Adding the Transactional Replication publication
USE [pub_db]
EXEC sp_addpublication @publication = N'linux_tran_repl', @description = N'Transactional publication of database ''pub_db'' from Publisher ''sqltranrepl''.', @sync_method = N'concurrent', @retention = 0, @allow_push = N'true', @allow_pull = N'true', @allow_anonymous = N'true', @enabled_for_internet = N'false', @snapshot_in_defaultfolder = N'true', @compress_snapshot = N'false', @ftp_port = 21, @ftp_login = N'anonymous', @allow_subscription_copy = N'false', @add_to_active_directory = N'false', @repl_freq = N'continuous', @status = N'active', @independent_agent = N'true', @immediate_sync = N'true', @allow_sync_tran = N'false', @autogen_sync_procs = N'false', @allow_queued_tran = N'false', @allow_dts = N'false', @replicate_ddl = 1, @allow_initialize_from_backup = N'false', @enabled_for_p2p = N'false', @enabled_for_het_sub = N'false'
GO
After including the Publication, the Snapshot Agent should be made to have purpose of having specified Publication at Publisher database.
包括发布后,应使快照代理具有在发布者数据库中指定发布的目的。
EXEC sp_addpublication_snapshot @publication = N'linux_tran_repl', @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 = 235959, @active_start_date = 0, @active_end_date = 0, @job_login = null, @job_password = null, @publisher_security_mode = 1
The below command will allow user to retrieve the details of the Publication as defined in @login parameter. We have just added the system administrator as a user however we can add any other users as well, if it is required.
以下命令将允许用户检索@login参数中定义的发布的详细信息。 我们刚刚将系统管理员添加为用户,但是如果需要,我们也可以添加任何其他用户。
EXEC sp_grant_publication_access @publication = N'linux_tran_repl', @login = N'sa'
GO
To get it verified, that publisher has been added or not, use the command below:
要验证它是否已添加发布者,请使用以下命令:
USE distribution
GO
SELECT publisher_db, publication
FROM MSpublications
GO
添加文章 (Adding Article)
USE [pub_db]
EXEC sp_addarticle @publication = N'linux_tran_repl', @article = N'data_in', @source_owner = N'dbo', @source_object = N'data_in', @type = N'logbased', @description = N'', @creation_script = N'', @pre_creation_cmd = N'drop', @schema_option = 0x000000000803509F, @identityrangemanagementoption = N'manual', @destination_table = N'data_in', @destination_owner = N'dbo', @status = 24, @vertical_partition = N'false', @ins_cmd = N'CALL [sp_MSins_dbodata_in]', @del_cmd = N'CALL [sp_MSdel_dbodata_in]', @upd_cmd = N'SCALL [sp_MSupd_dbodata_in]'
GO
Here @publication parameter defines name of the Publication and @article is for the name of Table and the remainder of parameters are characterized with default values.
这里@publication参数定义发布的名称,而@article代表Table的名称,其余参数用默认值来表征。
Now, to get it checked, that the Article has been included or not, utilize the command underneath:
现在,要检查它是否包含了Article,请使用下面的命令:
use pub_db
GO
SELECT p.Name AS publication_name, a.name as article_name
FROM syspublications p
INNER JOIN sysarticles a on p.pubid=a.pubid
GO
![List Articles with Publication in Transaction Replication.](https://www.sqlshack.com/wp-content/uploads/2019/09/list-articles-with-publication-in-transaction-repl.png)
添加订户 (Add Subscriber)
In this model, we have utilized Subscriber database as sub_db. The subscriber can be included with Publication by PUSH or PULL subscription type.
在此模型中,我们将订户数据库用作sub_db 。 订阅服务器可以按PUSH或PULL订阅类型包含在Publication中。
The PUSH distributor Agent will run at the Distributor to the end and the publisher data changes will be pushed to subscribers on demand or Continuously using and defining @sync_type parameter while adding Subscription.
PUSH分发服务器代理将在分发服务器上运行到最后,发布服务器数据更改将按需推送给订阅服务器,或者在添加订阅时连续使用并定义@sync_type参数。
The PULL Distributor Agent will keep running at the Subscriber as far as possible and it solicitation to get the progressions which are made at the Publisher.
PULL分发服务器代理将尽可能在订阅服务器上继续运行,并请求获取在发布服务器上进行的进度。
In case we have used PUSH subscription:
如果我们使用了PUSH订阅:
-- Adding the Transactional Replication Subscriber
USE [pub_db]
EXEC sp_addsubscription @publication = N'linux_tran_repl', @subscriber = N'sqltranrepl', @destination_db = N'sub_db', @subscription_type = N'Push', @sync_type = N'automatic', @article = N'all', @update_mode = N'read only', @subscriber_type = 0
The Distributor Agent should be arranged relying upon @subscription type. In this model, we utilized the Push subscription type, henceforth we will include Distributor Agent at Publication site end utilizing underneath content.
应该根据@subscription type来安排Distributor Agent。 在此模型中,我们使用了Push订阅类型,此后,我们将在发布站点末尾使用内容在其下包括Distributor Agent。
USE pub_db
GO
EXEC sp_addpushsubscription_agent @publication = N'linux_tran_repl', @subscriber = N'sqltranrepl', @subscriber_db = N'sub_db', @job_login = null, @job_password = null, @subscriber_security_mode = 1, @frequency_type = 64, @frequency_interval = 1, @frequency_relative_interval = 1, @frequency_recurrence_factor = 0, @frequency_subday = 4, @frequency_subday_interval = 5, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 0, @active_end_date = 0, @dts_package_location = N'Distributor'
产生快照 (Generate Snapshot)
Essentially, we generally Generate Snapshot utilizing SSMS or Replication Monitor. in Linux. To begin with, we have to find the Snapshot Agent Position from msdb database and afterward get started by SQL Command.
本质上,我们通常使用SSMS或Replication Monitor生成快照。 在Linux中。 首先,我们必须从msdb数据库中找到快照代理位置,然后由SQL Command开始。
In order to get the list of the jobs in SQL Server by executing below T-SQL statement in msdb database.
为了通过在msdb数据库中执行以下T-SQL语句来获取SQL Server中的作业列表。
use msdb
GO
SELECT sj.name, sc.name AS category_name
FROM sysjobs sj
INNER JOIN syscategories sc ON sj.category_id = sc.category_id
![List jobs to track Snapshot Agnet job.](https://www.sqlshack.com/wp-content/uploads/2019/09/list-jobs-to-track-snapshot-agnet-job-.png)
Now, in order to generate a snapshot by starting the SQL job SQLTRANREPL-pub_db-linux_tran_repl-1 using below T-SQL command.
现在,为了通过使用以下T-SQL命令启动SQL作业SQLTRANREPL-pub_db-linux_tran_repl-1来生成快照。
USE msdb
GO
EXEC dbo.sp_start_job N'SQLTRANREPL-pub_db-linux_tran_repl-1'
![Generate Snapshot by executing the snapshot job in msdb in Transaction Replication.](https://www.sqlshack.com/wp-content/uploads/2019/09/generate-snapshot-by-executing-the-snapshot-job-in.jpeg)
There could be a few errors that could happen on beginning the Snapshot Agent, that can be checked utilizing beneath T-SQL query on Distribution database.
启动Snapshot Agent时可能会发生一些错误,可以使用Distribution数据库上的T-SQL查询进行检查。
USE distribution
GO
SELECT time, error_text FROM MSrepl_errors ORDER BY time DESC
GO
By checking Transactional Replication Error log in distribution database again, it looks good now.
通过再次检查分发数据库中的“事务复制错误”日志,现在看起来不错。
The Snapshot successfully generated and inserted a row in the publisher database. Its quickly synchronized at subscriber end as well.
快照成功生成并在发布者数据库中插入了一行。 它也可以在订户端快速同步。
![Data replicated at Subscriber end.](https://www.sqlshack.com/wp-content/uploads/2019/09/data-replicated-at-subscriber-end-.png)
结论 (Conclusion)
We attempted to cover the Deployment procedure of SQL Server Transactional Replication on Linux Operating System in this. I trust you think that it’s helpful, please feel free pose any question or put any recommendation to improve in the comment section below.
我们试图在此介绍Linux操作系统上SQL Server事务复制的部署过程。 我相信您认为这会有所帮助,请在下面的评论部分随意提出任何问题或提出任何建议以进行改进。
翻译自: https://www.sqlshack.com/sql-server-transactional-replication-on-linux/