Sql-Server
從 MSSQL2005 複製到 MYSQL 5.x 的一種方式
我有一個 mysql 數據庫作為我們的 websever 後端。我們有我們的生產 mssql 伺服器,我們希望將數據從中推送到我們的 mysql 伺服器。
我已成功將 mysql 伺服器設為 ODBC 系統數據源。
我可以從我們的 mssql 表中向 mysql 表中執行插入更新語句,所以我知道我確實可以通過系統 ODBC 連接連接到 mysql 伺服器。
我已經使用以下方法設置了本地出版物:
-- Adding the transactional publication use [repl_test] exec sp_addpublication @publication = N'Repl_test' , @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
我添加了一個快照代理:
exec sp_addpublication_snapshot @publication = N'Repl_test' , @frequency_type = 4 , @frequency_interval = 4 , @frequency_relative_interval = 1 , @frequency_recurrence_factor = 0 , @frequency_subday = 4 , @frequency_subday_interval = 1 , @active_start_time_of_day = 0 , @active_end_time_of_day = 235959 , @active_start_date = 0 , @active_end_date = 0 , @job_login = null , @job_password = null , @publisher_security_mode = 1 GO
我為非 mssql 伺服器添加了訂閱:
use [repl_test] exec sp_addsubscription @publication = N'Repl_test' , @subscriber = N'MYSQL' , @destination_db = N'TestTable' , @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'Repl_test' , @subscriber = N'MYSQL' , @subscriber_db = N'TestTable' , @job_login = null , @job_password = null , @subscriber_security_mode = 0 , @subscriber_login = N'root' , @subscriber_password = 'PASSWORD' , @subscriber_provider = N'MSDASQL' , @subscriber_datasrc = N'mysqltest' , @frequency_type = 64 , @frequency_interval = 0 , @frequency_relative_interval = 0 , @frequency_recurrence_factor = 0 , @frequency_subday = 0 , @frequency_subday_interval = 0 , @active_start_time_of_day = 0 , @active_end_time_of_day = 235959 , @active_start_date = 20101202 , @active_end_date = 99991231 , @enabled_for_syncmgr = N'False' , @dts_package_location = N'Distributor' GO
一切都是順利創建的,我確保我至少選擇了一篇文章(其中只有一個名為“TestTable”的表。
執行複制監視器時,我收到以下錯誤:
Error messages: The process could not connect to Subscriber 'MYSQL'. (Source: MSSQL_REPL, Error number: MSSQL_REPL20084) Get help: http://help/MSSQL_REPL20084 Data source name not found and no default driver specified (Source: MSSQLServer, Error number: IM002) Get help: http://help/IM002
我搞砸了,我想在“@subscriber_provider”和“@subscriber_datasrc”。如果不能通過複製完成,我們將不得不設置某種 SQL 代理作業來進行更新/複製。
歡迎任何幫助。
Update1 我已經成功完成了“TestTable”的初始複製。我必須使用 sp_addarticle 的儲存過程,以便複製不會在連接時刪除 mysql 表。
exec sp_addarticle @publication = N'Repl_test' , @article = N'TestTable' , @source_owner = N'dbo' , @source_object = N'TestTable' , @type = N'logbased' , @pre_creation_cmd = N'none' , @ins_cmd = N'SQL' , @del_cmd = N'SQL' , @upd_cmd = N'SQL' , @schema_option = 0x20025081 , @status = 24 GO
複製現在抱怨我的表中有一個重複的唯一鍵。我想我還有更多關於添加文章儲存過程的事情。
Update2 在初始複製期間收到以下錯誤
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"MSREPL7" set xactts = _binary'\0\0\0}\0\04\0\0\0\0', timecol = {ts '2010-12-' at line 1 (Source: MSSQL_REPL, Error number: MSSQL_REPL20046) Get help: http://help/MSSQL_REPL20046
然後隨後的錯誤是關於重複鍵。
解決了。
sp_addrticlecolumn 中的變數 @force_reinit_subscription 必須為 null 或 0。
我評論了那條線,它就像一個魅力。
這是完整的工作程式碼:
--step 1 -- Adding the transactional publication use [repl_test] exec sp_addpublication @publication = N'Repl_test' , @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 --add the article to the publication exec sp_addarticle @publication = N'Repl_test' , @article = N'TestTable' , @source_owner = N'dbo' , @source_object = N'TestTable' , @type = N'logbased' , @pre_creation_cmd = N'none' , @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'Repl_test' , @article = N'TestTable' , @refresh_synctran_procs = 1 GO --end step1 --step2 --add the publication snaphot exec sp_addpublication_snapshot @publication = N'Repl_test' , @frequency_type = 4 , @frequency_interval = 4 , @frequency_relative_interval = 1 , @frequency_recurrence_factor = 0 , @frequency_subday = 4 , @frequency_subday_interval = 1 , @active_start_time_of_day = 0 , @active_end_time_of_day = 235959 , @active_start_date = 0 , @active_end_date = 0 , @job_login = null , @job_password = null , @publisher_security_mode = 1 GO --end step2 --step3 --add the subscriber(s) use [repl_test] exec sp_addsubscription @publication = N'Repl_test' , @subscriber = N'mysqltest' , @destination_db = N'repl_test' , @subscription_type = N'Push' , @sync_type = N'automatic' , @article = N'all' , @update_mode = N'read only' , @subscriber_type = 3 GO --add the pushing subscription agent exec sp_addpushsubscription_agent @publication = N'Repl_test' , @subscriber = N'mysqltest' , @subscriber_db = N'repl_test' , @job_login = null , @job_password = null , @subscriber_security_mode = 0 , @subscriber_login = N'root' , @subscriber_password = 'PASSWORD' , @subscriber_provider = N'MSDASQL' , @subscriber_datasrc = N'mysqltest' , @frequency_type = 64 , @frequency_interval = 1 , @frequency_relative_interval = 0 , @frequency_recurrence_factor = 0 , @frequency_subday = 0 , @frequency_subday_interval = 0 , @active_start_time_of_day = 0 , @active_end_time_of_day = 235959 , @active_start_date = 20101202 , @active_end_date = 99991231 , @enabled_for_syncmgr = N'False' , @dts_package_location = N'Distributor' GO --end step3