Google Custom Search

Thursday, June 26, 2008

Configuring Peer-to-Peer replication Using TSQL:

This article brings you the easiest way to configure P2P replication using TSQL. Also I assume that the database has been initialized in all nodes participating in the replication.

1. Configure Distributor:
This script needs to be executed against all servers that participates in the replication.

/* Check if distributor database already exists*/
declare @svrname as sysname
select @svrname = @@servername
declare @cnt int
Select @cnt=count(*) from sys.databases where name = 'distribution'
If @cnt>0
begin
Print 'distributor already exists'
End
Else
Begin
Exec sp_adddistributor @distributor=@svrname
Exec sp_adddistributiondb @database='distribution',@security_mode=1
End

2. Create Publication and Subscriber:
This script needs to be executed against all the publisher databases involved in the P2P replication instances. There are few parameters that you might need to change in this script which will be noted so edit the parameters before executing.

/*Add the publisher*/
declare @svrname as sysname
select @svrname = @@servername
/* Add the publisher server now*/

exec sp_adddistpublisher @publisher =@svrname,
@distribution_db = N'distribution',
@security_mode = 1,
@publisher_type = N'MSSQLSERVER'

/* Enable the database for publication*/
declare @dbname varchar(30)
declare @dbcheck sql_variant

/* Check if the database is set as publication*/
/*Set you database name*/
select @dbname = '------' /***** Set Your Database Name Here**************/
select @dbcheck = databasepropertyex(@dbname,'ispublished')
select @dbcheck
if @dbcheck = 0
begin
print 'Database not set as Publisher. Setting as Publisher'
exec sp_replicationdboption @dbname=@dbname,
@optname='publish',
@value='true'
end
else
begin
print 'Database already set as Publisher'
end

/* Add the Publication now*/
declare @pubname varchar(300)
select @pubname = '---------' /********Add Publication Name Here***********/
/*Execute this step against the database which you need to set as publication*/
exec sp_addpublication @publication=@pubname,
@restricted='false',
@sync_method='native',
@repl_freq='continuous',
@allow_push='true',
@allow_pull='true',
@immediate_sync='true',
@allow_sync_tran='false',
@autogen_sync_procs='false',
@retention=60,
@independent_agent='true',
@enabled_for_p2p='true',
@status='active',
@allow_initialize_from_backup='true'
Go

/* Add articles for this Publication*/
/**********This step needs to be executed for each article in the publication***************/
declare @artname varchar(300)
declare @artins varchar(300)
declare @artdel varchar(300)
declare @artupd varchar(300)
select @artname = '-------'/**********Add Article Name Here***************/
select @artins = 'CALL [sp_MSins_'+@artname+']'
select @artdel = 'CALL [sp_MSdel_'+@artname+']'
select @artupd = 'CALL [sp_MSupd_'+@artname+']'

/*Execute this step against the database which you need to set as publication*/
declare @pubname varchar(300)
select @pubname = '----------'/***********Add your publication name here*************/
exec sp_addarticle @publication = @pubname,
@article = @artname,
@source_owner = N'dbo',
@source_object = @artname,
@type = N'logbased',
@description = N'',
@creation_script = N'',
@pre_creation_cmd = N'none',
@schema_option = 0x00000000000044F7,
@identityrangemanagementoption = N'manual',
@destination_table = @artname,
@destination_owner = N'dbo',
@status = 16,
@vertical_partition = N'false',
@ins_cmd = @artins,
@del_cmd = @artdel,
@upd_cmd = @artupd
Go

/*Add the Subscriber*/
declare @pubname varchar(300)
select @pubname = '----------'/***********Add your publication name here*************/
declare @dbname varchar(30)
select @dbname = '----------'/***********Add your Subscriber database name here*************/
declare @subname varchar(30)
select @subname = '----------'/***********Add your Subscriber Server name here*************/

exec sp_addsubscription @publication = @pubname,
@subscriber = @subname,
@destination_db = @dbname,
@sync_type = 'replication support only'
Go

/*Add the Subscriber Agents*/
declare @pubname varchar(300)
select @pubname = '----------'/***********Add your publication name here*************/
declare @dbname varchar(30)
select @dbname = '----------'/***********Add your Subscriber database name here*************/
declare @subname varchar(30)
select @subname = '----------'/***********Add your Subscriber Server name here*************/

exec sys.sp_addpushsubscription_agent
@publication = @pubname,
@subscriber = @subname,
@subscriber_db = @dbname,
@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'

No comments: