Post

SQL Replication


SQL replication is the process of copying and distributing data and database objects from one database to another and then synchronizing between databases to maintain consistency.

Publication is a collection of database objects that are to be replicated. Publisher is the server that makes the data available for replication. Subscriber is the server that receives the replicated data. Other key terms include Distributor, which manages the flow of data and ensures synchronization, and Articles, which are the individual database objects (like tables or views) within a publication.


Before setting up SQL Replication, make sure the SQL Server and Agent services are running under the domain service account

x


Here’s the Employee table on WIN1 that will be replicated to WIN2

x


Distribution

On WIN1, configure the Distribution

x


Configure WIN1 as the distributor

x


Next configure for the SQL Server Agent Service to start automatically

x


Because we don’t run a pull method from the subscriber, we can use a local path instead of a network path here for the Snapshot Folder

x


Then configure the distribution database

x


After that select WIN1 as the publisher

x


Then select configure distribution at the end of the wizard

x


Hit finish

x


Publication

Still on WIN1, creata a new publication

x


Select the database to publish

x


Then select the publication type

x


Next select the articles to be published, here we select one table and one stored procedure

x


For the table filters we’ll just skip away

x


Select create snapshot immediately

x


Then on security, enter the domain service user

x


Next select create publication at the end of the wizard

x


Give the publication a name, then hit finish

x


And now the employee publication has been created

x


Subscription

Now on WIN2, create a new subscription

x


Select WIN1 as publisher and select the publication

x


For the agent location, we will use push type where the agent runs on the publisher

x


Then select the WIN2 as the subscriber and select the subcription database

x


Specify the domain service user

x


For the sync schedule, select run continuously

x


Then select initialize subcription

x


Select create subscription at the end of the wizard

x


Then hit finish

x

x


Monitoring Replication

On WIN1, on Replication Monitor we can see we have WIN2 in the subscription running the replication

x


We can also see the detailed logs for this particular replication

x


Now going back to WIN2, we can see the table has been replicated and available on WIN2

x


Modifying Data

On WIN1, we modify the table data by inserting 10 new rows

x


We can see on the Synchronization Status that we have 1 transaction with 10 commands being delivered to WIN2

x


And WIN2 now has 20 rows of data

x


Now we try deleting 7 rows of data on WIN1

x


The Synchronization Status also shows 7 commands being delivered to inform WIN2 about the deletion

x


Now WIN2 only has 13 rows of data

x


Now lets try stopping the synchronization

x


If we modify data while sync is off, the WIN2 will not receive the changes until the sync is back on

x


Now lets start the synchronization again

x


And we can see the 9 commands change we made is being delivered right after

x


This post is licensed under CC BY 4.0 by the author.