使用联机方法迁移到 Azure SQL 数据库
如果需要在整个迁移过程中使数据库与用户保持联机,则可以使用事务复制来移动数据。 事务复制是迁移到 Azure SQL 数据库的唯一可用联机方法。
在自行车制造商场景中,仓库全天候运行,没有非活动时间段。 董事会希望确保库存数据库始终可用,即使在迁移到 Azure SQL 数据库的过程中也是如此。
事务复制是什么?
事务复制是在连续连接的数据库服务器之间移动数据的一种方法。
该过程首先是获取发布数据库对象和数据的快照。 获取初始快照后,对发布服务器上的数据或架构的任何后续更改通常会近乎实时地传送到 Azure SQL 数据库。
Azure SQL 数据库支持将事务复制和快照复制均作为推送订阅服务器。 这意味着 Azure SQL 数据库可以使用事务复制或快照复制来接收和应用来自发布服务器的更改。
发布服务器和/或分发服务器可以是在本地、云中的 Azure 虚拟机上运行的 SQL Server 实例,也可以是作为 Azure SQL 托管实例运行的 SQL Server 实例。
可以通过 SQL Server Management Studio 或通过在发布服务器上执行 Transact-SQL 语句来配置事务复制。 无法从 Azure 门户配置事务复制。
事务复制需要以下组件:
角色 | 定义 |
---|---|
发布者 | 托管要复制的数据的数据库实例(源)。 |
订阅服务器 | 接收发布服务器复制的数据(目标)。 |
分发服务器 | 从发布服务器收集项目中的更改,并将其分发到订阅服务器。 |
文章 | 数据库对象;例如,发布中包含的表。 |
发布 | 来自所复制的数据库的一个或多个项目的集合。 |
订阅 | 订阅服务器对发布的请求。 |
设置事务复制
按照以下步骤可在不停机的情况下,将表 [Person].[Person]
从 AdventureWorks 数据库迁移到 Azure SQL 数据库。 事务复制只能使用 SQL Server 身份验证登录来连接到 Azure SQL 数据库。
参数 | 定义 |
---|---|
@distributor |
源实例名称。 |
@publisher |
源实例名称。 |
@subscriber |
Azure SQL 数据库格式:<server>.database.windows.net 。 在运行脚本之前,Azure SQL 数据库必须存在。 |
@dbname |
源中的数据库名称。 |
@publisher_login |
在源中具有所需权限的 SQL 用户。 |
@publisher_password |
SQL 用户的密码。 |
@destination_db |
目标中的数据库名称。 |
@subscriber_login |
在目标中具有所需权限的 SQL 用户。 |
@subscriber_password |
SQL 用户的密码。 |
@working_directory |
复制工作目录,请根据需要更改此位置。 |
运行脚本时,请根据自己的环境调整上述参数。
创建分发服务器
以下脚本创建分发服务器数据库、分发服务器发布服务器和代理。
USE [master]
GO
EXEC sp_adddistributor @distributor = N'CONTOSO-SRV', @password = N''
GO
EXEC sp_adddistributiondb
@database = N'distribution',
@data_folder = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Data',
@data_file = N'distribution.MDF',
@data_file_size = 13,
@log_folder = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Data',
@log_file = N'distribution.LDF',
@log_file_size = 9,
@min_distretention = 0,
@max_distretention = 72,
@history_retention = 48,
@deletebatchsize_xact = 5000,
@deletebatchsize_cmd = 2000,
@security_mode = 1
GO
-- Adding the distribution publishers
exec sp_adddistpublisher
@publisher = N'CONTOSO-SRV',
@distribution_db = N'distribution',
@security_mode = 1,
@working_directory = N'C:\REPL',
@trusted = N'false',
@thirdparty_flag = 0,
@publisher_type = N'MSSQLSERVER'
GO
exec sp_addsubscriber
@subscriber = N'contoso.database.windows.net',
@type = 0,
@description = N'Azure SQL Database (target)'
GO
-- Enabling the replication database
use master
exec sp_replicationdboption
@dbname = N'AdventureWorks',
@optname = N'publish',
@value = N'true'
GO
--Adds a Log Reader agent for the AdventureWorks database.
exec [AdventureWorks].sys.sp_addlogreader_agent
@publisher_security_mode = 1
GO
--Adds a Queue Reader agent for the distributor.
exec [AdventureWorks].sys.sp_addqreader_agent
@frompublisher = 1
GO
创建事务发布
以下脚本创建从发布服务器对 AdventureWorks
数据库的事务发布。
USE [AdventureWorks]
GO
EXEC sp_addpublication
@publication = N'REPL-AdventureWorks',
@description = N'Transactional publication of database ''AdventureWorks'' from Publisher ''CONTOSO-SRV''.',
@sync_method = N'concurrent',
@retention = 0,
@allow_push = N'true',
@allow_pull = N'true',
@allow_anonymous = N'true',
@enabled_for_internet = N'false',
@snapshot_in_defaultfolder = N'false',
@alt_snapshot_folder = N'C:\REPL',
@compress_snapshot = N'true',
@ftp_port = 21,
@ftp_login = N'anonymous',
@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',
@autogen_sync_procs = N'false',
@allow_queued_tran = N'false',
@allow_dts = N'false',
@replicate_ddl = 1,
@allow_initialize_from_backup = N'false',
@enabled_for_p2p = N'false',
@enabled_for_het_sub = N'false'
GO
exec sp_addpublication_snapshot
@publication = N'REPL-AdventureWorks',
@frequency_type = 1,
@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 = 0,
@active_end_date = 0,
@publisher_security_mode = 0,
@publisher_login = N'sqladmin',
@publisher_password = N'<pwd>'
创建发布项目
以下脚本为 [Person].[Person]
表创建项目。
USE [AdventureWorks]
GO
EXEC sp_addarticle
@publication = N'REPL-AdventureWorks',
@article = N'Person',
@source_owner = N'Person',
@source_object = N'Person',
@type = N'logbased',
@description = N'',
@creation_script = N'',
@pre_creation_cmd = N'drop',
@schema_option = 0x000000000803509F,
@identityrangemanagementoption = N'none',
@destination_table = N'Person',
@destination_owner = N'Person',
@status = 24,
@vertical_partition = N'false',
@ins_cmd = N'CALL [sp_MSins_PersonPerson]',
@del_cmd = N'CALL [sp_MSdel_PersonPerson]',
@upd_cmd = N'SCALL [sp_MSupd_PersonPerson]'
GO
创建订阅和订阅代理
以下脚本创建对 Azure SQL 数据库订阅服务器的推送订阅。
USE [AdventureWorks]
GO
EXEC sp_addsubscription
@publication = N'REPL-AdventureWorks',
@subscriber = N'contoso.database.windows.net',
@destination_db = N'my-db',
@subscription_type = N'Push',
@sync_type = N'automatic',
@article = N'all',
@update_mode = N'read only',
@subscriber_type = 0
exec sp_addpushsubscription_agent
@publication = N'REPL-AdventureWorks',
@subscriber = N'contoso.database.windows.net',
@subscriber_db = N'my-db',
@job_login = null,
@job_password = null,
@subscriber_security_mode = 0,
@subscriber_login = N'sqladmin',
@subscriber_password = '<pwd>',
@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
启动和监视复制
Azure SQL 数据库中不支持复制管理和监视。 相反,请从 SQL Server 执行这些活动。 若要启动复制,请启动快照作业、日志读取器作业和分发服务器作业。
可以通过右键单击发布并选择相应选项来监视快照代理和日志读取器代理。 如果代理未运行,请启动它们。
若要查看同步状态,请右键单击订阅,选择“查看同步状态”,然后启动代理。 如果遇到任何错误消息,请在 SQL Server 代理上检查代理作业历史记录。 如果代理按预期运行,应会看到以下结果。
快照代理:
日志读取器代理:
同步状态:
将数据完全复制到 Azure SQL 数据库后,可以将连接定向到订阅服务器数据库,然后停止并删除复制。
若要详细了解支持的配置,请参阅复制到 Azure SQL 数据库。