本文可帮助你解决在不同实例之间移动 MSDB 数据库时发生的权限问题。
原始产品版本: Microsoft SQL Server
原始 KB 数: 2000274
现象
假设出现了下面这种情景:
可以使用备份和还原过程,或通过复制数据库文件(mdf 和 ldf)将 msdb 数据库从一个实例移到另一个实例。 然后,在目标服务器上,不属于 SQL Server 中 Sysadmin 固定角色的用户尝试执行以下操作之一:
- 在 SQL Server Management Studio 中查看作业。
- 使用 T-SQL 直接调用 SQL 代理相关的存储过程(例如xp_sqlagent_enum_jobs或sp_get_composite_job_info)。
在此方案中,用户将收到类似于以下内容的错误消息:
执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo)
对对象“xp_sqlagent_enum_jobs”、“数据库”mssqlsystemresource“、”架构“sys”拒绝 EXECUTE 权限。 (Microsoft SQL Server,错误: 229)
原因
出现此问题的原因是不同实例上的 SQL 代理证书不同。
解决方法
若要解决此问题,需要在目标服务器上使用以下脚本将 master 中的证书替换为还原 的 msdb 数据库中的证书:
use msdb
go
-- Backup the Agent certificate from the remote server to a file
BACKUP CERTIFICATE [##MS_AgentSigningCertificate##] TO FILE = 'MS_AgentSigningCertificate.remote_server.cer'
go
use master
go
-- re-create the agent certificate on master
-- Note: Because we are making these changes using a regular user and not as part of setup, the name
-- cannot include the ## token.
-- Creating a regular certificate in this case should be the equivalent as we only need it to derive a SID
CREATE CERTIFICATE [MS_AgentSigningCertificate.remote_server] FROM FILE = 'MS_AgentSigningCertificate.remote_server.cer'
go
-- Recreate the user mapped to the cert and grant the same permissions that the regular certificate needs.
CREATE USER [MS_AgentSigningCertificate.remote_server] FROM CERTIFICATE [MS_AgentSigningCertificate.remote_server]
go
GRANT EXECUTE TO [MS_AgentSigningCertificate.remote_server]
go
注意
在运行本文中讨论的脚本之前,master 和模型中的证书相同,执行脚本会导致以下错误消息:
Msg 15232,级别 16,状态 1,第 7 行
名称为“MS_AgentSigningCertificate.remote_server20009”的证书已存在,或者此证书已添加到数据库中。
如果遇到本文中讨论的症状(即使证书相同),请联系Microsoft客户支持服务(CSS)获取更多帮助。