Script to start subscribers synchronizing

こ雲淡風輕ζ 提交于 2019-12-01 00:21:18

Assuming the publication already exists (and a valid snapshot is available on a valid folder), here are the T-SQL and command line instructions that you need to initiate and maintain a subscription on both publisher and subscriber. We use this code to start a web replication from scratch (no database on the subscriber's side). Once the subscription declared on both sides through stored procedures, you have to run (on a regular basis) the synchronization command-line instruction on the subscriber's side (see below).

As this code is specific to web replication, please check additional references on organizing such a replication on your server, including publication parameters, etc. You'll find all the stuff related to security in the corresponding docs. Of course, synchronization command-line parameters are strictly related to the specific subscription and its parameters. In case your subscription is not done through web (like in this example), you 'll have to adjust publication, subscription and synchronization parameters consequently. Please be aware that many parameters here reflects our specific situation, where for example the publisher is also the subscriber. You might have to adapt depending on your situation.

PS: SQLEXPRESS server cannot be a publisher. You'll have to get the complete version of SQL to publish your database. SQLEXPRESS servers can be Subscribers.

  1. Publisher code: one sp

    exec sp_addmergesubscription 
        @publication = N'myPublication', 
        @subscriber = N'mySuscriber\SQLEXPRESS', 
        @subscriber_db = N'myDatabaseOnMySubscriber', 
        @subscription_type = N'pull', 
        @subscriber_type = N'local', 
        @subscription_priority = 0, 
        @sync_type = N'Automatic'
    go
    
  2. Subscriber code: two sp's

    exec sp_addmergepullsubscription 
        @publisher = N'myServerName', 
        @publication = N'myPublicationName', 
        @publisher_db = N'myMainDatabase', 
        @subscriber_type = N'Local', 
        @subscription_priority = 0, 
        @description = N'', 
        @sync_type = N'Automatic'
    
    exec sp_addmergepullsubscription_agent
        @publisher = N'myServername', 
        @publisher_db = N'myMainDatabase', 
        @publication = N'myDatabaseOnMySubscriber', 
        @distributor = N'myServerName', 
        @distributor_security_mode = 1, 
        @distributor_login = N'', 
        @distributor_password = N'', 
        @enabled_for_syncmgr = N'True', 
        @frequency_type = 4, 
        @frequency_interval = 1, 
        @frequency_relative_interval = 1, 
        @frequency_recurrence_factor = 0, 
        @frequency_subday = 8, 
        @frequency_subday_interval = 1, 
        @active_start_time_of_day = 0, 
        @active_end_time_of_day = 235959, 
        @active_start_date = 0, 
        @active_end_date = 0, 
        @alt_snapshot_folder = N'', 
        @working_directory = N'', 
        @use_ftp = N'True', 
        @job_login = null, 
        @job_password = null, 
        @publisher_security_mode = 1, 
        @publisher_login = N'', 
        @publisher_password = N'', 
        @use_interactive_resolver = N'False', 
        @dynamic_snapshot_location = N'', 
        @use_web_sync = 1, 
        @internet_url = N'https://mySecuredWebPage:myOpenPort/myPublicationName/replisapi.dll',
        @internet_login = N'myDomain\myUserName', 
        @internet_password = null, 
        @internet_security_mode = 0, 
        @internet_timeout = 300
    go
    
  3. Subscriber BAT file for synch

    "C:\Program Files\Microsoft SQL Server\90\COM\replmerg.exe"
        -Publisher [myServerName]  
        -PublisherDB [myMainDatabase]  
        -Publication [myPublicationName] 
        -Distributor [myServerName] 
        -Subscriber [mySubscriber\SQLEXPRESS] 
        -SubscriptionType 1 
        -SubscriberSecurityMode 1 
        -SubscriberDB [myDatabaseOnMySubscriber] 
        -InternetURL [https://mySecuredWebPage:myOpenPort/myPublicationName/replisapi.dll] 
        -InternetLogin [myDomain\myUserName] 
        -InternetPassword [myPassword]
    

On a test system, why not run SQL Server Profiler from your login - and capture the SQL statements as you click on Replication Monitor?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!