代码改变世界

SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”

2017-02-24 11:41  潇湘隐者  阅读(1641)  评论(2编辑  收藏  举报

SQL Server 2005(Microsoft SQL Server 2005 - 9.00.5000.00)下的一个作业执行一个存储过程,存储过程中动态SQL语句使用链接服务器(Linked Servers),从另外一台SQL Server服务器获取数据。但是执行时报“Access to the remote server is denied because the current security context is not trusted”。但是在SSMS客户端手工执行存储过程却又是OK的。

 

遇到这个问题很是诡异,看似是一个权限和安全方面的原因,但是有不少作业也是这么做的,唯独这台服务器遇到这个问题。网上也有大把人遇到这个问题。官方都有一些资料。

 

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/512991fd-af88-4a87-8b3e-39e0e346040e/access-to-the-remote-server-is-denied-because-the-current-security-context-is-not-trusted?forum=sqlsecurity

 

https://blogs.msdn.microsoft.com/deepakbi/2010/03/02/job-fails-on-linked-server-access-to-the-remote-server-is-denied-because-the-current-security-context-is-not-trusted-sqlstate-42000-error-15274/

 

http://dba.stackexchange.com/questions/114723/current-security-context-is-not-trusted-for-job-running-as-a-sql-login-accessi

 

https://www.sqlservercentral.com/Forums/Topic579778-146-1.aspx

 

提供的解决方法也五花八门的。我都一一尝试了一下,结果都没有解决我的问题。最后按照下面这篇文章的介绍解决了问题。

 

http://stackoverflow.com/questions/10526198/access-to-the-remote-server-is-denied-because-the-current-security-context-is-no

 

修改作业的Owner,将作业Owner从sa改为链接服务器(Linked Server)的安全上下文( Current Security Context)所使用的账号(服务器两边都有该相同账号),问题解决。虽然问题解决了,但是还是没有明白root cause,跟一些人沟通都无解。暂时先记录一下这个问题。

 

----------------------------------------------------------------------------分割线---------------------------------------------------------

 

上面问题的原因已经弄清楚了,具体参考博客The server principal "sa" is not able to access the database "xxxx" under the current security context相同的原因)其实是因为在作业的步骤里面,有个高级选项,有个Run as User,里面设置了以某个特殊账户执行。所以遇到了上面问题, 而我上面阴错阳差的解决了,直到今天才找到Root Cause。