Events
31 Mar, 23 - 2 Apr, 23
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server 2022 (16.x)
This article provides steps to configure Transactional and Snapshot replication by using authentication with Microsoft Entra ID (formerly Azure Active Directory) for Azure-Arc enabled SQL Server.
Microsoft Entra authentication support for replication was introduced in Cumulative Update 6 for SQL Server 2022, and made generally available in Cumulative Update 12. When you use Microsoft Entra authentication for replication, the only different step is the first step. Specifically, create a Microsoft Entra login, and grant sysadmin permissions.
After that, use the Microsoft Entra login in the replication stored procedures to configure Transactional or Snapshot replication as you normally would.
Note
Starting with SQL Server 2022 CU 6, disable Microsoft Entra authentication for replication by using session trace flag 11561.
To configure replication with Microsoft Entra authentication, you must meet the following prerequisites:
Configuring your replication with Microsoft Entra authentication currently has the following limitations:
Create the Microsoft Entra login, and grant it the sysadmin
role.
To create the Microsoft Entra login and assign it as a sysadmin
, use the following Transact-SQL (T-SQL) command:
USE master
CREATE LOGIN [login_name] FROM EXTERNAL PROVIDER
EXEC sp_addsrvrolemember @loginame='login_name', @rolename='sysadmin'
For example, to add the login name for newuser@tenant.com
, use this command:
USE master
CREATE LOGIN [newuser@tenant.com] FROM EXTERNAL PROVIDER
EXEC sp_addsrvrolemember @loginame='newuser@tenant.com', @rolename='sysadmin'
Use sp_adddistributiondb to create the distribution database.
The following is an example script to create your distribution database on your Distributor:
EXEC sp_adddistributiondb @database = N'distribution_db',
@data_folder = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER22\MSSQL\DATA',
@log_folder = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER22\MSSQL\DATA',
@log_file_size = 2, @min_distretention = 0, @max_distretention = 72,
@history_retention = 48, @deletebatchsize_xact = 5000,
@deletebatchsize_cmd = 2000, @security_mode = 1
The following example creates the table UIProperties
in the Distribution database, and sets the SnapshotFolder
property so the snapshot agent knows where to write replication snapshots:
USE [distribution_db]
IF (not exists (SELECT * FROM sysobjects WHERE NAME = 'UIProperties' and TYPE = 'U '))
CREATE TABLE UIProperties(id int)
IF (exists(SELECT * FROM::fn_listextendedproperty('SnapshotFolder', 'user', 'dbo', 'table', 'UIProperties', null, null)))
EXEC sp_updateextendedproperty N'SnapshotFolder', N' C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER22\MSSQL\DATA',
'user', dbo, 'table', 'UIProperties'
ELSE
EXEC sp_addextendedproperty N'SnapshotFolder', N' C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER22\MSSQL\DATA',
'user', dbo, 'table', 'UIProperties'
The following script configures the Publisher to use the Distributor database, and defines the AD user login, along with a password to be used for replication:
EXEC sp_adddistpublisher @publisher = N'publisher_db', @distribution_db = N'distribution_db',
@security_mode = 0, @login = N'newuser@tenant.com', @password = N'password',
@working_directory = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER22\MSSQL\ReplData',
@trusted = N'false', @thirdparty_flag = 0, @publisher_type = N'MSSQLSERVER'
Use sp_replicationdboption to enable replication on your Publisher database, such as testdb
, as the following example:
EXEC sp_replicationdboption @dbname = N'testdb', @optname = N'publish', @value = N'true'
Use sp_addpublication to add the publication.
You can configure transactional or snapshot replication.
Follow these steps to create a Transactional replication.
First, configure the log reader agent:
USE [AdventureWorksDB]
EXEC [AdventureWorksDB].sys.sp_addlogreader_agent @job_login = null, @job_password = null,
@publisher_security_mode = 2, @publisher_login = N'newuser@tenant.com',
@publisher_password = N'<password>', @job_name = null
GO
Next, create the transactional publication:
use [AdventureWorksDB]
exec sp_addpublication @publication = N'AdvWorksProducTrans',
@description = N'Publication of database ''AdventureWorksDB'' from Publisher 'N'publisher_db''.',
@sync_method = N'concurrent', @retention = 0, @allow_push = N'true', @allow_pull = N'true',
@allow_anonymous = N'false', @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'false', @allow_sync_tran = N'true',
@allow_queued_tran = N'true', @allow_dts = N'false', @replicate_ddl = 1,
@allow_initialize_from_backup = N'false', @enabled_for_p2p = N'false',
@enabled_for_het_sub = N'false', @conflict_policy = N'pub wins'
Then, create the Snapshot Agent and store the snapshot files for the Publisher by using the Microsoft Entra login for the @publisher_login
and defining a password for the Publisher:
use [AdventureWorksDB]
exec sp_addpublication_snapshot @publication = N'AdvWorksProducTrans', @frequency_type = 1,
@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,
@job_login = null, @job_password = null, @publisher_security_mode = 2,
@publisher_login = N'newuser@tenant.com', @publisher_password = N'<password>'
Finally, add the article TestPub
to the publication:
use [AdventureWorksDB]
exec sp_addarticle @publication = N'AdvWorksProducTrans', @article = N'testtable',
@source_owner = N'dbo', @source_object = N'testtable', @type = N'logbased',
@description = null, @creation_script = null, @pre_creation_cmd = N'drop',
@schema_option = 0x000000000803509D, @identityrangemanagementoption = N'manual',
@destination_table = N'testtable', @destination_owner = N'dbo', @vertical_partition = N'false'
Use sp_addsubscription to add your Subscriber, and then use either sp_addpushsubscription_agent on the Publisher to create a push subscription or sp_addpullsubscription_agent on the Subscriber to create a pull subscription. Use the Microsoft Entra login for the @subscriber_login
.
The following sample script adds the subscription:
USE [testdb]
EXEC sp_addsubscription @publication = N'testpub', @subscriber = N'<subscription_server>',
@destination_db = N'testdb', @subscription_type = N'Push', @sync_type = N'automatic',
@article = N'all', @update_mode = N'read only', @subscriber_type = 0
The following sample script adds a push subscription agent at the Publisher:
EXEC sp_addpushsubscription_agent @publication = N'testpub', @subscriber = N'<subscription server.',
@subscriber_db = N'testdb', @job_login = null, @job_password = null, @subscriber_security_mode = 2,
@subscriber_login = N'newuser@tenant.com', @subscriber_password = 'password', @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 = 20220406, @active_end_date = 99991231, @enabled_for_syncmgr = N'False', @dts_package_location = N'Distributor'
The following parameters in these replication stored procedures were modified in CU 6 for SQL Server 2022 to support Microsoft Entra authentication for replication:
@distributor_security_mode
@subscriber_security_mode
@publisher_security_mode
, @distributor_security_mode
@subscriber_security_mode
, @publisher_security_mode
@publisher_security_mode
@publisher_security_mode
@publisher_security_mode
@publisher_security_mode
The following values define the security modes for these stored procedures:
To learn more, review SQL Server Replication and Microsoft Entra authentication for SQL Server
Events
31 Mar, 23 - 2 Apr, 23
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Module
Secure Microsoft Entra users with multifactor authentication - Training
Learn how to use multifactor authentication with Microsoft Entra ID to harden your user accounts.
Certification
Microsoft Certified: Azure Database Administrator Associate - Certifications
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.
Documentation
Configure Publishing and Distribution - SQL Server
Learn how to configure publishing and distribution in SQL Server by using SQL Server Management Studio, Transact-SQL, or Replication Management Objects.
Create a publication - SQL Server
Learn how to create a publication in SQL Server by using SQL Server Management Studio, Transact-SQL, or Replication Management Objects.
Best Practices for Replication Administration - SQL Server
After you configure replication, use these best practices to administer your replication topology in SQL Server.