简体   繁体   English

事务复制或快照复制适合我的情况

[英]Transactional Replication or Snapshot Replication For My Situation

I'm currently working on a project for a company, the company has 8 branches, each with their own server.我目前正在为一家公司做一个项目,该公司有8个分支机构,每个分支机构都有自己的服务器。 They task me to combine the db into one to be used at the headquarter.他们让我把数据库合并成一个在总部使用。 Maybe I'm stupid but the only way that I can come up with is to replicate each database to HQ.也许我很愚蠢,但我能想出的唯一方法是将每个数据库复制到总部。 So then HQ will have 8 database of each branches.那么HQ将有每个分支机构的8个数据库。 Problem is they want data changes to be real time, so I figured snapshot isn't going to work.问题是他们希望数据更改是实时的,所以我认为快照是行不通的。

So I opted to transaction, which works but from my testing, I tried deleting some record at HQ database first, then I deleted the same record from the publisher/branches.所以我选择了交易,这可行,但从我的测试来看,我尝试先删除总部数据库中的一些记录,然后从发布者/分支机构中删除相同的记录。 And I got this error when it tries to replicate the database,当它尝试复制数据库时出现此错误,

"The row was not found at the Subscriber when applying the replicated DELETE command for Table '[dbo].[repl_tbl]' with Primary Key(s): [id] = 1 (Source: MSSQLServer, Error number: 20598)" “当使用主键对表 '[dbo].[repl_tbl]' 应用复制的 DELETE 命令时,在订阅服务器上找不到该行:[id] = 1(来源:MSSQLServer,错误号:20598)”

Another problem that I found is that, If I were to add new data with let say primary key '15' to the branch database at my HQ which currently doesn't exist yet, and then I add data with primary key '15' to the branch/publisher database, now this result in error of我发现的另一个问题是,如果我将主键为“15”的新数据添加到当前尚不存在的总部的分支数据库中,然后我将主键为“15”的数据添加到分支/发布者数据库,现在这导致错误

"Violation of PRIMARY KEY constraint 'PK_repl_tbl'. Cannot insert duplicate key in object 'dbo.repl_tbl'. The duplicate key value is (13). (Source: MSSQLServer, Error number: 2627)" “违反 PRIMARY KEY 约束 'PK_repl_tbl'。无法在对象 'dbo.repl_tbl' 中插入重复键。重复键值为 (13)。(来源:MSSQLServer,错误号:2627)”

I'm expecting the data at HQ to changed according to branch.我希望总部的数据根据​​分支机构而改变。

Any suggestion is greatly appreciated.任何建议都非常感谢。 Thanks in advance.提前致谢。

If changes must be reflected at each side in real time and are to be made at both publisher and subscriber then you may want to look at Merge Replication, not Transaction or Snapshot one:如果更改必须实时反映在每一方,并且要在发布者和订阅者处进行,那么您可能需要查看合并复制,而不是事务或快照:

https://docs.microsoft.com/en-us/sql/relational-databases/replication/merge/merge-replication?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/relational-databases/replication/merge/merge-replication?view=sql-server-2017

Merge replication is a bit more complex and if any issues happen to it you can't simply reinitialize subscription at Publisher because you will loose non-synchronized data at subscriber like the issue described in this question .合并复制有点复杂,如果发生任何问题,您不能简单地在发布者处重新初始化订阅,因为您将在订阅者处丢失非同步数据,就像这个问题中描述的问题一样 But from your description it may be a way to go.但从你的描述来看,这可能是一种方法。

However, merge, snapshot or transaction replication mostly deals with individual published tables then whole databases.但是,合并、快照或事务复制主要处理单独的已发布表,然后是整个数据库。 If you need to make available at HQ complete databases from branches then you may want to look at another technology called Always On Availability Groups .如果您需要在总部从分支机构提供完整的数据库,那么您可能需要查看另一种称为Always On Availability Groups 的技术 With AGs you can easily make remote DBs available for HQ but it requires Windows Failover Cluster setup between branches and HQ first.使用 AG,您可以轻松地为总部提供远程数据库,但它首先需要在分支机构和总部之间设置 Windows 故障转移集群 Among limitations of AGs is that the secondary replica databases are read-only. AG 的一个限制是辅助副本数据库是只读的。 Changes can only be done at Primary replica.更改只能在主副本上完成。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM