代码改变世界

SQL Server复制删除发布时遇到错误18752

2024-01-25 16:33  潇湘隐者  阅读(67)  评论(0编辑  收藏  举报

朋友反馈他无法删除一台SQL Server数据库上的发布,具体情况为删除一个SQL Server Replication的发布时,遇到下面错误

TITLE: Microsoft SQL Server Management Studio
------------------------------

无法删除发布“REPL_ITSMS”。

------------------------------
ADDITIONAL INFORMATION:

执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo)

------------------------------

Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related procedure, drop the connection with session ID 75 over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.
Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related proceduredrop the connection with session ID 75 over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.
Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related proceduredrop the connection with session ID 75 over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.
Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related proceduredrop the connection with session ID 75 over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.
Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related proceduredrop the connection with session ID 75 over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.
在执行 'sp_droppublication' 的过程中出错。调用 'sp_replcmds' 失败,错误代码: '18752',返回代码: '1'
已将数据库上下文更改为 "itsms"。 (Microsoft SQL ServerError18752)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=15.00.4312&EvtSrc=MSSQLServer&EvtID=18752&LinkId=20476

好久没有维护SQL Server数据库了,感觉生疏了很多,检查后发现其实出现这个错误,是因为一次只能有一个日志读取器代理或日志相关过程(sp_repldone、sp_replcmds 和 sp_replshowcmds)连接到某个数据库。 如果执行了一个日志相关过程,那么在启动日志读取器代理或者执行另一个日志相关过程之前,请删除执行第一个过程时所用的连接,或者在该连接上执行 sp_replflush。

其实,官方文档[1]关于错误代码18752有详细的说明,只要详细阅读文档,就能知道如何解决这个错误了。这里特此记录一下。具体如下所示:

说明

有多个当前连接正在尝试执行以下任一日志相关过程: sp_repldone、 sp_replcmds或 sp_replshowcmds。 sp_repldone (Transact-SQL) 和 sp_replcmds (Transact-SQL) 存储过程是日志读取器代理用于查找和更新已发布数据库中复制事务的相关信息的存储过程。 sp_replshowcmds (Transact-SQL) 存储过程用于排查某些类型的事务复制问题。

在以下情形下将引发此错误:

  • 如果某个已发布数据库的日志读取器代理正在运行,而另一个日志读取器代理试图在同一个数据库上运行,则对第二个代理引发此错误,并且此错误将出现在代理历史记录中。

  • 有时看起来像是有多个代理,则可能其中一个代理是执行孤立进程的结果。

  • 如果启动了已发布数据库的日志读取器代理,而用户在同一个数据库上执行 sp_repldone、 sp_replcmds或 sp_replshowcmds ,则在执行存储过程的应用程序(如 sqlcmd)中将引发此错误。

  • 如果已发布数据库的日志读取器代理不在运行状态,而用户在执行 sp_repldone、 sp_replcmds或 sp_replshowcmds 后没有关闭用于执行此过程的连接,则当日志读取器代理尝试连接到数据库时将引发此错误。

用户操作

以下步骤可以帮助您解决这个问题。 如果任何一个步骤能正确启动日志读取器代理,则没有必要完成剩余的步骤。

  • 检查日志读取器代理的历史记录,查找可能导致此错误的其他任何错误。 有关在复制监视器中查看代理状态和错误详细资料的信息,请参阅使用复制监视器查看信息和执行任务。

  • 检查 sp_who (Transact-SQL) 的输出,了解连接到已发布数据库的特定进程标识号 (SPID) 。 关闭所有可能运行 sp_repldone、 sp_replcmds或 sp_replshowcmds的连接。

  • 重新启动日志读取器代理。 有关详细信息,请参阅启动和停止复制代理 (SQL Server Management Studio).

  • 在分发服务器上重新启动 SQL Server 代理服务(使之在群集中脱机或联机)。 如果计划的作业有可能在任何其他 实例中执行了sp_repldone 、 sp_replcmds 或 sp_replshowcmds SQL Server ,则也要为那些实例重新启动 SQL Server 代理。 有关详细信息,请参阅启动、停止或暂停 SQL Server 代理服务。

  • sp_replflush (发布服务器上对发布数据库执行 Transact-SQL) ,然后重启日志读取器代理。

  • 如果错误继续出现,请增加代理的日志记录并指定日志的输出文件。 此操作可能会提供找到该错误和/或其他错误消息的步骤,具体取决于错误的上下文。

其实这里的解决方法也很简单,就是执行命令sp_replflush后,就可以正常的删除复制的发布了。

参考资料

[1]

1: https://learn.microsoft.com/zh-cn/sql/relational-databases/replication/mssql-eng018752?view=sql-server-ver16