设为首页 收藏本站
查看: 726|回复: 0

[经验分享] SQL Server 2005/2008 合并复制Step by Step

[复制链接]

尚未签到

发表于 2015-6-30 14:03:31 | 显示全部楼层 |阅读模式
  有时在项目中需要使用到SQL Server Replication(复制)功能。
  有关这项技术的详细信息可以参考MSDN的SQL Server 复制。
  对于开发人员来说,SQL 2005/2008用起来区别不大。
  以下是转自code project 中的一篇SQL Server 2005 Merge Replication Step by Step,图文并茂。
  Replication is the process of sharing data between databases in different locations. Using replication, we can create copies of the database and share the copy with different users so that they can make changes to their local copy of database and later synchronize the changes to the source database
Introduction
  Replication is a set of technologies for copying and distributing data and database objects from one database to another and then synchronizing between databases to maintain consistency. Using replication, you can distribute data to different locations and to remote or mobile users over local and wide area networks, dial-up connections, wireless connections, and the Internet.   
  Replication is the process of sharing data between databases in different locations. Using replication, we can create copies of the database and share the copy with different users so that they can make changes to their local copy of database and later synchronize the changes to the source database.
Terminologies before getting started:
  Microsoft SQL Server 2000 supports the following types of replication
  Publisher is a server that makes the data available for subscription to other servers. In addition to that, publisher also identifies what data has changed at the subscriber during the synchronizing process. Publisher contains publication(s).
  Subscriber is a server that receives and maintains the published data. Modifications to the data at subscriber can be propagated back to the publisher.
  Distributor is the server that manages the flow of data through the replication system. Two types of distributors are present, one is remote distributor and the other one local distributor. Remote distributor is separate from publisher and is configured as distributor for replication. Local distributor is a server that is configured as publisher and distributor.
  Agents are the processes that are responsible for copying and distributing data between publisher and subscriber. There are different types of agents supporting different types of replication.
  Snapshot Agent is an executable file that prepares snapshot files containing schema and data of published tables and database objects, stores the files in the snapshot folder, and records synchronization jobs in the distribution database.
  An article can be any database object, like Tables (Column filtered or Row filtered), Views, Indexed views, Stored Procedures, and User defined functions.
  Publication is a collection of articles.
  Subscription is a request for copy of data or database objects to be replicated.
DSC0000.png
Replication Types
  Microsoft SQL Server 2005 supports the following types of replication:

  • Snapshot Replication
  • Transactional Replication
  • Merge Replication
Snapshot Replication

  • Snapshot replication is also known as static replication. Snapshot replication copies and distributes data and database objects exactly as they appear at the current moment in time.
  • Subscribers are updated with complete modified data and not by individual transactions, and are not continuous in nature.
  • This type is mostly used when the amount of data to be replicated is small and data/DB objects are static or does not change frequently.
Transactional Replication

  • Transactional replication is also known as dynamic replication. In transactional replication, modifications to the publication at the publisher are propagated to the subscriber incrementally.
  • Publisher and the subscriber are always in synchronization and should always be connected.
  • This type is mostly used when subscribers always need the latest data for processing.
Merge replication
  It allows making autonomous changes to replicated data on the Publisher and on the Subscriber. With merge replication, SQL Server captures all incremental data changes in the source and in the target databases, and reconciles conflicts according to rules you configure or using a custom resolver you create. Merge replication is best used when you want to support autonomous changes on the replicated data on the Publisher and on the Subscriber.
  Replication agents involved in merge replication are snapshot agent and merge agent.
  Implement merge replication if, changes are made constantly at the publisher and subscribing servers, and must be merged in the end.
  By default, the publisher wins all conflicts that it has with subscribers because it has the highest priority. Conflict resolver can be customized
Before starting the replication process:
  assume that we have 2 server:

  • EGYPT-AEID: is the publisher server ( contains HRatPublisher )
  • SPS:is the subscriber server ( contains HRatSubscriber ) use SQL server Authentication mode for login
  on the publisher database i created table: Employees with fields of (ID, Name, Salary) to replicate its data to the subscriber server.
  i will use publisher as subscriber also
  Note: Check that SQL Server Agent is running on the publisher and the subscriber
Steps:

  • Open SQL ServerManagement Studio and login with SQL Server Authentication to configure Publishing, Subscribers, and Distribution
DSC0001.jpg
  a- Configure the appropriate server as publisher or distributor.
DSC0002.jpg
  b- Enable the appropriate database for merge replication
DSC0003.png
  2- Create new local publication from DB-Server --> Replication --> Local Publications --> Right Click --> New Pub
DSC0004.png
  then choose the database that contains the data or objects you want to replicate
DSC0005.jpg
  then choose the replication type and then specify the SQL server versions that will be used by subscribers to that publication like SQL Server 2005, SQL mobile Edition, SQL for Win CE ....etc
DSC0006.jpg
  after that manage the replication articles, data and db objects, by choosing objects to be replicated
  Note: you can manage the replication properties for selected objects
DSC0007.png
  Then add filters to published tables to optimize performance and then configure the snapshot agent
DSC0008.jpg
DSC0009.png
  and configure the security for snapshot agent
DSC00010.jpg
  finally rename the publication and click finish
DSC00011.jpg
  3- create a new subscription for the created "MyPublication01" Publication by right click on MyPublication01 --> New Subscription
  that to configure the "Merge Agent" for replication on the subscriber database
DSC00012.png
DSC00013.png
  then choose one or more subscriber databases. you can add new SQL Server subscribers

  then specify the Merge Agent security as mentioned above on "Agent Snapshot"
  and so specify the synchronization schedule for each agent.
  Schedules:

  • Run Continuously: add schedule times to be auto run continuously
  • Run on demand only: manually run the synchronization

  and then next up to final step, then click finish
  you can check the errors from "Replication Monitor" by right click on Local Replication --> Launch Replication Monitor
Advantages in Replication:
  Users can avail the following advantages by using replication process:

  • Users working in different geographic locations can work with their local copy of data thus allowing greater autonomy.
  • Database replication can also supplement your disaster-recovery plans by duplicating the data from a local database server to a remote database server. If the primary server fails, your applications can switch to the replicated copy of the data and continue operations.
  • You can automatically back up a database by keeping a replica on a different computer. Unlike traditional backup methods that prevent users from getting access to a database during backup, replication allows you to continue making changes online.
  • You can replicate a database on additional network servers and reassign users to balance the loads across those servers. You can also give users who need constant access to a database their own replica, thereby reducing the total network traffic.
  • Database-replication logs the selected database transactions to a set of internal replication-management tables, which can then be synchronized to the source database. Database replication is different from file replication, which essentially copies files.
Replication Performance Tuning Tips:

  • By distributing partitions of data to different Subscribers.
  • When running SQL Server replication on a dedicated server, consider setting the minimum memory amount for SQL Server to use from the default value of 0 to a value closer to what SQL Server normally uses.
  • Don’t publish more data than you need. Try to use Row filter and Column filter options wherever possible as explained above.
  • Avoid creating triggers on tables that contain subscribed data.
  • Applications that are updated frequently are not good candidates for database replication.
  • For best performance, avoid replicating columns in your publications that include TEXT, NTEXT or IMAGE data types.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-81968-1-1.html 上篇帖子: 《Microsoft Sql server 2008 Internals》读书笔记--第五章Table(7) 下篇帖子: 在SQL SErver中实现数组功能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表