如需将 SQL Server 实例配置为与 Always On 可用性组搭配使用,您需要启用 SQL Server Agent 以在发生故障切换时捕获日志,然后运行清理作业。在执行此操作之前,您需要修改 CDC 代理作业步骤,以检查当前副本是否实际上是主副本。这可以使用 sys.fn_hadr_is_primary_replica 函数完成。
使用以下命令设置实例:
-- Check if the current replica is a primary for the corresponding database.USE[DATABASE_NAME];DECLARE@DatabaseNameSYSNAME=DB_NAME();IF(SELECTsys.fn_hadr_is_primary_replica(@DatabaseName))=1BEGIN-- If the replica isn't a primary, the code block that follows is skippedEXECUTEsys.sp_cdc_add_job@job_type='capture';EXECUTEsys.sp_cdc_add_job@job_type='cleanup';END
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Work with Always On availability groups\n\nThis page describes what Always-On availability groups in SQL Server are and how\nDatastream supports them for failover and data recovery scenarios.\n\nOverview of Always On availability groups\n-----------------------------------------\n\nIn SQL Server, Always On availability groups are a high-availability solution\nthat lets you prepare your databases for disaster recovery scenarios.\n\nAlways On availability groups maximize database availability for businesses.\nAvailability groups support a replicated environment for a selected set of\ndatabases, known as availability databases. Each group includes one set of\nprimary databases for read and write tasks, and up to eight sets of corresponding\nsecondary databases. The secondary databases can optionally allow read-only\naccess or backup operations.\n\nFor more information about Always On availability groups, see\n[What is an Always On availability group?](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server) in the SQL Server\ndocumentation.\n\nFor information about Always On Availability group prerequisites for a SQL\nServer instance, see the [SQL Server documentation](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/prereqs-restrictions-recommendations-always-on-availability?view=sql-server-ver17&source=recommendations#ServerInstance).\n\nConfigure Datastream for use with Always On availability groups\n---------------------------------------------------------------\n\nDatastream supports *synchronous-commit* availability mode with the\nchange tables CDC method. Under this mode, the secondary database stays\nsynchronised with the corresponding primary until data synchronization stops.\nThe confirmation of a transaction is only sent to the client when the secondary\nreplica writes the incoming transaction log records to a disk.\n\nFor information about availability modes, see\n[Differences between availability modes for an Always On availability group](https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server).\n\nTo configure your SQL Server instance for use with Always On availability groups,\nyou need to enable SQL Server Agent to capture logs when there's a failover, and\nthen run a clean up job. Before you can do this, you need to modify the CDC\nAgent job steps to check if the current replica is actually the primary. This is\ndone using the `sys.fn_hadr_is_primary_replica` function.\n\nUse the following commands to set up your instance: \n\n -- Check if the current replica is a primary for the corresponding database.\n USE [\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDATABASE_NAME\u003c/span\u003e\u003c/var\u003e];\n DECLARE @DatabaseName SYSNAME = DB_NAME();\n IF (SELECT sys.fn_hadr_is_primary_replica(@DatabaseName)) = 1\n BEGIN\n -- If the replica isn't a primary, the code block that follows is skipped\n EXECUTE sys.sp_cdc_add_job @job_type = 'capture';\n EXECUTE sys.sp_cdc_add_job @job_type = 'cleanup';\n END\n\nWhat's next\n-----------\n\n- Learn more about how Datastream works with [SQL Server sources](/datastream/docs/sources-sqlserver)."]]