| title | Configure Publishing and Distribution | |||
|---|---|---|---|---|
| description | Learn how to configure publishing and distribution in SQL Server by using SQL Server Management Studio, Transact-SQL, or Replication Management Objects. | |||
| author | MashaMSFT | |||
| ms.author | mathoma | |||
| ms.date | 09/25/2024 | |||
| ms.service | sql | |||
| ms.subservice | replication | |||
| ms.topic | how-to | |||
| helpviewer_keywords |
|
|||
| monikerRange | =azuresqldb-mi-current||>=sql-server-2016 | |||
| ms.custom |
|
[!INCLUDE SQL Server SQL MI] This topic describes how to configure publishing and distribution in [!INCLUDEssnoversion] by using [!INCLUDEssManStudioFull], [!INCLUDEtsql], or Replication Management Objects (RMO).
For more information, see View and modify replication security settings.
Configure distribution using the New Publication Wizard or the Configure Distribution Wizard. After the Distributor is configured, view and modify properties in the Distributor Properties - <Distributor> dialog box. Use the Configure Distribution Wizard if you want to configure a Distributor so that members of the db_owner fixed database roles can create publications, or because you want to configure a remote Distributor that is not a Publisher.
-
In [!INCLUDEmsCoName] [!INCLUDEssManStudioFull], connect to the server that will be the Distributor (in many cases, the Publisher and Distributor are the same server), and then expand the server node.
-
Right-click the Replication folder, and then click Configure Distribution.
-
Follow the Configure Distribution Wizard to:
-
Select a Distributor. To use a local Distributor, select ServerName will act as its own Distributor; SQL Server will create a distribution database and log. To use a remote Distributor, select Use the following server as the Distributor, and then select a server. The server must already be configured as a Distributor, and the Publisher must be enabled to use the Distributor. For more information, see Enable a Remote Publisher at a Distributor (SQL Server Management Studio).
If you select a remote Distributor, you must enter a password on the Administrative Password page for connections made from the Publisher to the Distributor. This password must match the password specified when the Publisher was enabled at the remote Distributor.
-
Specify a root snapshot folder (for a local Distributor). The snapshot folder is simply a directory that you have designated as a share; agents that read from and write to this folder must have sufficient permissions to access it. Each Publisher that uses this Distributor creates a folder under the root folder, and each publication creates folders under the Publisher folder in which to store snapshot files. For more information on securing the folder appropriately, see Secure the Snapshot Folder.
-
Specify the distribution database (for a local Distributor). The distribution database stores metadata and history data for all types of replication and transactions for transactional replication.
-
Optionally enable other Publishers to use the Distributor. If other Publishers are enabled to use the Distributor, you must enter a password on the Distributor Password page for connections made from these Publishers to the Distributor.
-
Optionally script configuration settings. For more information, see Scripting Replication.
Replication publishing and distribution can be configured programmatically using replication stored procedures.
- Execute sp_get_distributor (Transact-SQL) to determine if the server is already configured as a Distributor.
-
If the value of
installedin the result set is0, execute sp_adddistributor (Transact-SQL) at the Distributor on the master database. -
If the value of
distribution db installedin the result set is0, execute sp_adddistributiondb (Transact-SQL) at the Distributor on the master database. Specify the name of the distribution database for@database. Optionally, you can specify the maximum transactional retention period for@max_distretentionand the history retention period for@history_retention. If a new database is being created, specify the desired database property parameters.
-
At the Distributor, which is also the Publisher, execute sp_adddistpublisher (Transact-SQL), specifying the UNC share that will be used as default snapshot folder for
@working_directory.For a distributor on SQL Managed Instance, use an Azure storage account for
@working_directoryand the storage access key for@storage_connection_string. -
At the Publisher, execute sp_replicationdboption (Transact-SQL). Specify the database being published for
@dbname, the type of replication for@optname, and a value oftruefor@value.
-
Execute sp_get_distributor (Transact-SQL) to determine if the server is already configured as a Distributor.
-
If the value of
installedin the result set is0, execute sp_adddistributor (Transact-SQL) at the Distributor on the master database. Specify a strong password for@password. This password for thedistributor_adminaccount will be used by the Publisher when connecting to the Distributor. -
If the value of
distribution db installedin the result set is0, execute sp_adddistributiondb (Transact-SQL) at the Distributor on the master database. Specify the name of the distribution database for@database. Optionally, you can specify the maximum transactional retention period for@max_distretentionand the history retention period for@history_retention. If a new database is being created, specify the desired database property parameters.
-
-
At the Distributor, execute sp_adddistpublisher (Transact-SQL), specifying the UNC share that will be used as default snapshot folder for
@working_directory. If the Distributor will use [!INCLUDEssNoVersion] Authentication when connecting to the Publisher, you must also specify a value of0for@security_modeand the [!INCLUDEmsCoName] [!INCLUDEssNoVersion] login information for@loginand@password.For a distributor on SQL Managed Instance, use an Azure storage account for
@working_directoryand the storage access key for@storage_connection_string. -
At the Publisher on the master database, execute sp_adddistributor (Transact-SQL). Specify the strong password used in step 1 for
@password. This password will be used by the Publisher when connecting to the Distributor. -
At the Publisher, execute sp_replicationdboption (Transact-SQL). Specify the database being published for
@dbname, the type of replication for@optname, and a value of true for@value.
The following example demonstrates how to configure publishing and distribution programmatically. In this example, the name of the server that is being configured as a publisher and a local distributor is supplied using scripting variables. Replication publishing and distribution can be configured programmatically using replication stored procedures.
:::code language="sql" source="codesnippet/tsql/configure-publishing-and_1.sql":::
-
Create a connection to the server by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.ReplicationServer class. Pass the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.DistributionDatabase class.
-
Set the xref:Microsoft.SqlServer.Replication.DistributionDatabase.Name%2A property to the database name and set the xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A property to the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
Install the Distributor by calling the xref:Microsoft.SqlServer.Replication.ReplicationServer.InstallDistributor%2A method. Pass the xref:Microsoft.SqlServer.Replication.DistributionDatabase object from step 3.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.DistributionPublisher class.
-
Set the following properties of xref:Microsoft.SqlServer.Replication.DistributionPublisher:
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.Name%2A - name of the Publisher.
-
xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A - the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.DistributionDatabase%2A - the name of the database created in step 5.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.WorkingDirectory%2A - the share used to access snapshot files.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.PublisherSecurity%2A - the security mode used when connecting to the Publisher. xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.WindowsAuthentication%2A is recommended.
- Call the xref:Microsoft.SqlServer.Replication.DistributionPublisher.Create%2A method.
-
Create a connection to the remote Distributor server by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.ReplicationServer class. Pass the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.DistributionDatabase class.
-
Set the xref:Microsoft.SqlServer.Replication.DistributionDatabase.Name%2A property to the database name, and set the xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A property to the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
Install the Distributor by calling the xref:Microsoft.SqlServer.Replication.ReplicationServer.InstallDistributor%2A method. Specify a secure password (used by the Publisher when connecting to the remote Distributor) and the xref:Microsoft.SqlServer.Replication.DistributionDatabase object from step 3. For more information, see Secure the Distributor.
[!IMPORTANT]
When possible, prompt users to enter security credentials at runtime. If you must store credentials, use the cryptographic services provided by the [!INCLUDEmsCoName] Windows .NET Framework. -
Create an instance of the xref:Microsoft.SqlServer.Replication.DistributionPublisher class.
-
Set the following properties of xref:Microsoft.SqlServer.Replication.DistributionPublisher:
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.Name%2A - name of the local Publisher server.
-
xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A - the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 1.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.DistributionDatabase%2A - the name of the database created in step 5.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.WorkingDirectory%2A - the share used to access snapshot files.
-
xref:Microsoft.SqlServer.Replication.DistributionPublisher.PublisherSecurity%2A - the security mode used when connecting to the Publisher. xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.WindowsAuthentication%2A is recommended.
-
Call the xref:Microsoft.SqlServer.Replication.DistributionPublisher.Create%2A method.
-
Create a connection to the local Publisher server by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.ReplicationServer class. Pass the xref:Microsoft.SqlServer.Management.Common.ServerConnection from step 9.
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationServer.InstallDistributor%2A method. Pass the name of the remote Distributor and the password for the remote Distributor specified in step 5.
Important
When possible, prompt users to enter security credentials at runtime. If you must store credentials, use the cryptographic services provided by the Windows .NET Framework.
You can programmatically configure replication publishing and distribution by using Replication Management Objects (RMO).
[!code-csHowTo#rmo_AddDistPub]
[!code-vbHowTo#rmo_vb_AddDistPub]