共用方式為


sp_adddistributor (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

在 sys.servers 數據表中建立專案(如果沒有的話),將伺服器項目標示為散發者,並儲存屬性資訊。 這個預存程式會在資料庫的散發者端 master 執行,以註冊伺服器並標示為散發者。 在遠端散發者的情況下,也會從 master 資料庫在發行者端執行,以註冊遠端散發者。

Transact-SQL 語法慣例

語法

sp_adddistributor
    [ @distributor = ] N'distributor'
    [ , [ @heartbeat_interval = ] heartbeat_interval ]
    [ , [ @password = ] N'password' ]
    [ , [ @from_scripting = ] from_scripting ]
    [ , [ @encrypt_distributor_connection = ] N'encrypt_distributor_connection' ]
    [ , [ @trust_distributor_certificate = ] N'trust_distributor_certificate' ]
    [ , [ @host_name_in_distributor_certificate = ] N'host_name_in_distributor_certificate' ]
[ ; ]

引數

[ @distributor = ] N'distributor'

散發伺服器名稱。 @distributor為 sysname,沒有預設值。 只有在設定遠端散發者時,才會使用此參數。 它會在數據表中 msdb..MSdistributor 新增散發者屬性的專案。

注意

伺服器名稱可以指定為 <Hostname>,<PortNumber> 預設實例或 <Hostname>\<InstanceName>,<PortNumber> 具名實例。 使用自定義埠在Linux或 Windows 上部署 SQL Server,並停用瀏覽器服務時,請指定連線的埠號碼。 遠端散發者的自定義埠號碼使用適用於 SQL Server 2019 (15.x) 和更新版本。

[ @heartbeat_interval = ] heartbeat_interval

代理程式可以執行的最大分鐘數,而不需要記錄進度訊息。 @heartbeat_interval為 int,預設值為10分鐘。 建立 SQL Server Agent 作業,此作業會在此間隔執行,以檢查正在執行的復寫代理程序狀態。

[ @password = ] N'password'

distributor_admin登入的密碼 @password為 sysname,預設值為 NULL。 如果密碼為 NULL 或空字串, @password 會重設為隨機值。 新增第一個遠端散發者時,必須設定密碼。 distributor_admin登入和@password會儲存供散發者 RPC 連線使用的鏈接伺服器專案,包括本機連線。 如果散發者是本機,則distributor_admin的密碼會設定為新的值。 對於具有遠端散發者的發行者,在發行者和散發者端執行時,必須指定@passwordsp_adddistributor sp_changedistributor_password可用來變更散發者密碼。

重要

可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須將認證儲存在指令碼檔案中,則必須維護這個檔案的安全性,使他人無法在未獲授權的情況下擅自存取。

[ @from_scripting = ] from_scripting

@from_scripting為 bit,預設值為 0。 僅供參考之用。 不支援。 我們無法保證未來的相容性。

[ @encrypt_distributor_connection = ] N'encrypt_distributor_connection'

適用於: SQL Server 2025(17.x)及更新版本。

判斷從發行者到散發者的內部連結伺服器連線是否已加密。 這些值會對應至 OLE DB 提供者的 Encrypt 屬性。 @encrypt_distributor_connectionnvarchar(10),並且不能是 NULL

@encrypt_distributor_connection 可以是下列其中一個值:

  • mandatory (預設為 Microsoft OLE DB 提供者 19)
  • nofalse (預設為 Microsoft OLE DB 提供者 18)
  • trueyes
  • optional
  • strict

[ @trust_distributor_certificate = ] N'trust_distributor_certificate'

適用於: SQL Server 2025(17.x)及更新版本。

指出散發者的 TLS 憑證是否應該在未經驗證的情況下受信任。 此值會對應至 OLE DB 提供者的 TrustServerCertificate 屬性,而且在使用自我簽署憑證時,通常會與 Mandatory 加密設定搭配使用。 @trust_distributor_certificatenvarchar(5),並且不能是 NULL

@trust_distributor_certificate 可以是下列其中一個值:

  • no (預設值)
  • yes

注意

安全預設值與底層 OLEDB 提供者 19 有關,這增強了安全性。 覆寫預設值的選項比將執行個體設定為使用受信任憑證更不安全。 覆寫預設值之後,您可以選擇將 SQL Server 設定為使用憑證,然後使用 sp_changedistributor_property 預存程序將屬性設定 trust_distributor_certificate=no 回安全預設值。

[ @host_name_in_distributor_certificate = ] N'host_name_in_distributor_certificate'

適用於: SQL Server 2025(17.x)及更新版本。

指定散發者憑證中的主機名稱,當它與散發者名稱不同時,例如當 IP 位址或 DNS 別名用作散發者名稱時。 如果憑證中的主機名稱符合散發者名稱,請將 @host_name_in_distributor_certificate 參數保留空白。 @host_name_in_distributor_certificate 是任何字串值的 nvarchar(255), 預設值為 NULL

傳回碼值

0 (成功) 或 1 (失敗)。

備註

sp_adddistributor 用於快照式複寫、事務複製和合併式複寫。

範例

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Install the Distributor and the distribution database.
DECLARE @distributor AS sysname;
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @directory AS nvarchar(500);
DECLARE @publicationDB AS sysname;
-- Specify the Distributor name.
SET @distributor = $(DistPubServer);
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);
-- Specify the replication working directory.
SET @directory = N'\\' + $(DistPubServer) + '\repldata';
-- Specify the publication database.
SET @publicationDB = N'AdventureWorks2022'; 

-- Install the server MYDISTPUB as a Distributor using the defaults,
-- including autogenerating the distributor password.
USE master
EXEC sp_adddistributor @distributor = @distributor;

-- Create a new distribution database using the defaults, including
-- using Windows Authentication.
USE master
EXEC sp_adddistributiondb @database = @distributionDB, 
    @security_mode = 1;
GO

-- Create a Publisher and enable AdventureWorks2022 for replication.
-- Add MYDISTPUB as a publisher with MYDISTPUB as a local distributor
-- and use Windows Authentication.
DECLARE @distributionDB AS sysname;
DECLARE @publisher AS sysname;
-- Specify the distribution database.
SET @distributionDB = N'distribution';
-- Specify the Publisher name.
SET @publisher = $(DistPubServer);

USE [distribution]
EXEC sp_adddistpublisher @publisher=@publisher, 
    @distribution_db=@distributionDB, 
    @security_mode = 1;
GO

設定散發者以信任自我簽署憑證

若要覆寫 OLEDB 19 提供者的安全預設值,並設定 trust_distributor_certificate=yes 為散發者信任自我簽署憑證,請使用下列範例:

EXECUTE sys.sp_adddistributor @trust_distributor_certificate = 'yes';

注意

安全預設值與底層 OLEDB 提供者 19 有關,這增強了安全性。 覆寫預設值的選項比將執行個體設定為使用受信任憑證更不安全。 覆寫預設值之後,您可以選擇將 SQL Server 設定為使用憑證,然後使用 sp_changedistributor_property 預存程序將屬性設定 trust_distributor_certificate=no 回安全預設值。

欲了解更多資訊,請參閱 SQL Server 2025 中的遠端分發器破壞變更

權限

只有系統管理員固定伺服器角色的成員才能執行 sp_adddistributor