Tuesday, March 22, 2011

SQL Server 2005 – 2008 Transactional Replication

Transactional replication is the method to replicate the data from one database table to another database table immediately which might be on the same server or some other server. Initially a snapshot is applied from the source database to the destination database and then all the transactions on the source database tables are applied immediately to the destination database tables.

There are 2 databases that I will be using in this replication:

· AdventureWorks (Source Database)

· Books (Destination Database)

Setting Up the Publisher and Distributor:

Right click on the local publication under the replication section in the source server and select New Publication:

clip_image002

clip_image004

Select the source database:

clip_image006

Select the replication type as Transactional Replication. I have shown how to perform snaphot replication in my previous post: http://masoodhashim.blogspot.com/2011/03/sql-server-2005-2008-snapshot_21.html

clip_image008

Select the database article tables which you want to replicate:

clip_image010

clip_image012

clip_image014

Setup the security context under which the transactional replication agent should run:

clip_image016

clip_image018

clip_image020

Click Next to create the initial snapshot of the source tables which will be applied for the first time to the destination database tables when the subscription is setup:

clip_image022

Click finish:

clip_image024

clip_image026

clip_image028

The job for the publisher is automatically created:

clip_image030

Setting Up the Subscriber:

Right click on the local subscription under the replication section in the destination server and select New Subscriptions:

clip_image032

clip_image034

Select the publisher that we had created earlier:

clip_image036

clip_image038

Select the destination database where the source tables are to be replicated:

clip_image040

Define the security context under which the replication agent should run in the destination server:

clip_image042

clip_image044

clip_image046

clip_image048

Select immediately to apply the snapshot to the destination database that was created earlier while setting up the publisher:

clip_image050

clip_image052

Click on finish to subscribe to the publication that was created earlier:

clip_image054

clip_image056

clip_image058

The subscription jobs are created automatically under the SQL server agent:

clip_image060

Publishers source table:

clip_image062

Initial snapshot created at the subscription database:clip_image064

To check if the transactional replication is working:

Update a row at the publisher database:

use AdventureWorks

update Person.Address

set AddressLine2 = 'This is my new address'

where AddressID = 1

select * from Person.Address

where AddressID = 1

clip_image066

The above transaction is replicated at the destination database (Books) immediately:

use Books

select * from Person.Address

where AddressID = 1

clip_image068


Replication Monitor

Right click on the replication section and select “Launch Replication Monitor”:

clip_image070

Replication Monitor shows you all the details and data flow that is occurring between the publisher and the subscriber:

clip_image072

clip_image074

You can also check the latency rate in this window:

clip_image076

clip_image078

Transactional replication is very bandwidth intensive as all the frequent transactions at the publisher keeps applying to the subscriber immediately.

You can also opt for option to display a warning if the latency exceeds the threshold:

clip_image080

Monday, March 21, 2011

SQL Server 2005 - 2008 – Snapshot Replication

In this blog I will show you the steps on how to replicate your tables from one database to the other database that might be on the same server or a different server. The same steps can be followed for both 2005 as well as 2008 SQL servers.

There are 2 databases that I will be using in this replication:

· AdventureWorks (Source Database)

· Books (Destination Database)

Setting Up the Publisher and Distributor:

clip_image002

Go to Replications section of the source server and right click on new publication:

clip_image004

clip_image006

Click Next:

clip_image008

clip_image010

You can specify the folder where the snapshot files should be placed:

clip_image012

Select the source database name:

clip_image014

Select the replication type: Snapshot replication in this case.

In my other blogs I will show how to create other types of replications :

clip_image016

Select the tables that you want to replicate:

clip_image018

Click on article properties to set the properties of the destination articles like the schema name for example. If you want the schema name of the destination table to be different from the source then you can specify it here:

clip_image020

clip_image022

Click Next:

You can specify the filter properties by clicking add:

clip_image024

clip_image026

Click Next:

clip_image028

Specify the security setting for the SQL server snapshot agent under what security context it should run:clip_image030

clip_image032

You can specify the windows account or the SQL server agent service account.

clip_image034

clip_image036

clip_image038

Specify a publication name:

clip_image040

clip_image042

The sql server jobs are automatically created for these operations that can be used to create a snapshot in future which you can schedule also.

clip_image044

clip_image046

The snapshot files are created in the folder that you had mentioned earlier:

clip_image048

Setting Up the Subscriber:

Right click on the local subscriptions in the replication pane of the destination server:

clip_image050

clip_image052

Select the publisher that you had created earlier:

clip_image054

clip_image056

Select the destination database where the replicated tables are to be stored:

clip_image058

Specify the security context as before as to how the agent has to run:

clip_image060

clip_image062

clip_image064

clip_image066

clip_image068

clip_image070

clip_image072

clip_image074

Refresh the database to see the replicated tables:

clip_image076

clip_image078

The subscription jobs are also created automatically:clip_image080

Source Table:

clip_image082

Destination table:

Schema name is different from the source if you had noticed as we had specified its property while setting up the publisher:clip_image084