This can be illustrated by an example , A "publisher publishes a book that is recieved by distributors and then it dilevers the book to each of the subscribers".
Now that we are up with loads of concepts about replication we can work out for our real problem:
Database Replication from Sql Server 2005 to Mysql 5.x
Recently, I was setting up replication from SQL server 2005
to Mysql 5.x. I spent a lot of hours to research on setting up the task. Some
blogs and articles didnt mention the replication between SQL Server 2005 and
Mysql, as mysql do not officially allow the replication between them, they
always mention Oracle as the possible non-SQL subscriber. As scene in the
following screenshot
So, that is why we need a
way that should other than this wizard. i.e we should set up replication with stored procedure provided by SQL Server 2005.
First task is to make a publisher for a database. Which is achieved by the stored procedure sp_addpublication, in our case we executed this stored procedure as :
exec sp_addpublication
@publication = N'mydb'
, @description = N'Transactional publication of database'
, @sync_method = N'concurrent_c'
, @retention = 0
, @allow_push = N'true'
, @allow_pull = N'false'
, @allow_anonymous = N'true'
, @enabled_for_internet = N'false'
, @snapshot_in_defaultfolder = N'true'
, @compress_snapshot = N'false'
, @ftp_port = 21
, @allow_subscription_copy = N'false'
, @add_to_active_directory = N'false'
, @repl_freq = N'continuous'
, @status = N'active'
, @independent_agent = N'true'
, @immediate_sync = N'true'
, @allow_sync_tran = N'false'
, @allow_queued_tran = N'false'
, @allow_dts = N'false'
, @replicate_ddl = 0
, @allow_initialize_from_backup = N'false'
, @enabled_for_p2p = N'false'
, @enabled_for_het_sub = N'true'
, @autogen_sync_procs = 'false'
GO
We used "@sync
method" to be concurrent_c as because,
[ @sync_method=]
'sync_method'
Is the synchronization mode. sync_method is nvarchar(13), and can be
one of the following values.
Value
|
Description
|
native
|
Produces
native-mode bulk copy program output of all tables. Not
supported for Oracle Publishers.
|
character
|
Produces
character-mode bulk copy program output of all tables. For an
Oracle Publisher, character is
valid only for snapshot replication.
|
concurrent
|
Produces
native-mode bulk copy program output of all tables but does not lock tables
during the snapshot. Only supported for transactional publications. Not
supported for Oracle Publishers.
|
concurrent_c
|
Produces
character-mode bulk copy program output of all tables but does not lock
tables during the snapshot. Only supported for transactional publications.
|
database
snapshot
|
Produces
native-mode bulk copy program output of all tables from a database snapshot.
This option requires SQL Server Enterprise.
|
database
snapshot character
|
Produces
character-mode bulk copy program output of all tables from a database
snapshot. This option requires SQL Server Enterprise.
|
NULL
(default)
|
Defaults
to native for Microsoft SQL Server Publishers. For
non-SQL Server Publishers, defaults to character when the value of
repl_freq is Snapshot and to concurrent_c
for all other cases.
|
[ @repl_freq=]
'repl_freq'
We used replication frequency to be continous as we need to set up
transactional replication.
@enabled_for_het_sub = N'true'
We needed to support heterogenous replication.
Now, What is Heterogeneous Replication?
Heterogeneous replication works basically the same way, but means having
a Non-SQL Server in the role of publisher or subscriber. As our subscriber will be Mysql 5.x.
Now that we have set up publication,
Secondly we need to add articles to the publication,
--add
table 1
exec
sp_addarticle @publication = N'aclc_pub'
, @article = N'SS_SetupTarget' -- your database table
, @source_owner = N'dbo'
, @source_object = N'SS_SetupTarget'
, @type = N'logbased'
, @pre_creation_cmd = N'delete'
, @ins_cmd = N'SQL'
, @del_cmd = N'SQL'
, @upd_cmd = N'SQL'
, @schema_option = 0x8000000
, @status = 24
GO
--add
all of the columns to the article
exec
sp_articlecolumn @publication = N'aclc_pub'
, @article = N'SS_SetupTarget'
, @refresh_synctran_procs = 1
GO
--end
add table 1
So,
now here comes another critical point,
Actually for transactional replication
for non SQL subscriber, SQL server generates the bulk inserts for all the rows
in the table. And what if the object is already set up there. We use @pre_creation_cmd = N'delete', so now the SQL Server will delete
all the rows from the subscriber and then inserts all the rows. If pre creation
cmd is none than it will dump all the rows, which can result in duplication of
records accros your Mysql Database. This case is limited to mysql or other non-sql server subscriber as on homogeneous subscribers it only publishes the changed or inserted rows.
It can be a good idea if you only add the
row inserted all along this period. But I have chosen so that any single row is
not missed, as our data need to be really consistent. There is another replication technique where you move the subscriber to sql server subscriber and then move the replicated rows to mysql and delete the entire rows of the middle database tables.
SO now arise another question ,
whats this “@schema_option = 0x8000000”.
Now,
we should set snapshot agent for specified publication.
exec sp_addpublication_snapshot @publication = N'aclc_pub', @frequency_type = 4, --daily --
@frequency_interval = 1, @frequency_relative_interval
= 1,
@frequency_recurrence_factor
= 0,
@frequency_subday
= 4,
@frequency_subday_interval
= 15, @active_start_time_of_day = 0, @active_end_time_of_day = 235959,
@active_start_date = 20111227,
@active_end_date = 99991231,
@job_login = null, @job_password = null,
@publisher_security_mode = 1GO
Add publication creates a
snapshot agent. We need to set up the time after which it should re initialize
itself by using @frequency variables.
@frequency_type = 4 –
which is daily, as we want the data to be synchronized daily , @frequency_relative_interval
= 1 - unused,
@frequency_recurrence_factor = 0
@frequency_subday
= 4,
@frequency_subday_interval = 15 – these are the minutes after which it
generates a snapshot,
For our case we should set
frequency_subday to be 8(in hours) and @frequency_subday_interval to 24.
In our case, we should set @active_start_time_of_day
half an hour before the
Now that snapshot agent is
set up we should move to set up
subscriber.
Now, you
go to ODBC connector > System DSN > Add.. > Mysql odbc 3.5 > OK.
Now that you have setup DSN on your windows machine
exec sp_addsubscription
@publication = N'mySqlReplication',
@subscriber = N'REPLICATION',
@destination_db = N'employee_info',
@subscription_type = N'Push',
@sync_type = N'automatic',
@article = N'all',
@update_mode = N'read only',
@subscriber_type = 1
exec sp_addpushsubscription_agent
@publication = N'mySqlReplication',
@subscriber = N'REPLICATION',
@subscriber_db = N'employee_info',
@job_login = null, @job_password = null,
@subscriber_security_mode = 0,
@subscriber_login = N'root',
@subscriber_password = null,
@subscriber_provider = N'MSDASQL',
@subscriber_datasrc = N'replication',
@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'
GO
You should mention your DSN name in @subscriber, @subscriber_db should be the mysql database you wish to replicate. @subscriber_login and @subscriber_password should be the username and password for you DSN. For sp_addpushsubscription_agent you should set up the frequency after which the subscriber will be refreshed with the snapshot generated by the snapshot agent.
You might need some extra tools to set up, specially you will need schema of the sql server 2005 to be set up for mysql. You should download Mysql Migration toolkit from mysql site.