2013-01-15 64 views
0

我尝试使用下面的脚本来配置SQL Server作业代理:配置SQL Server作业代理

USE [msdb] 
GO 
CREATE USER [mydbuser] FOR LOGIN [mydbuser] 
GO 
EXEC sp_addrolemember N'db_datareader', N'mydbuser' 
GO 
EXEC sp_addrolemember N'SQLAgentOperatorRole', N'mydbuser' 
GO 
EXEC sp_addrolemember N'SQLAgentReaderRole', N'mydbuser' 
GO 
EXEC sp_addrolemember N'SQLAgentUserRole', N'mydbuser' 
go 
sp_configure 'show advanced options',1 
go 
reconfigure with override 
go 
     sp_configure 'xp_cmdshell',1 
go 
reconfigure with override 
go 
sp_configure 'show advanced options',0 
go 
reconfigure with override 


use master 
go 
GRANT EXECUTE on xp_cmdshell to db_job 
go 

use master 
go 
EXEC sp_xp_cmdshell_proxy_account 'mydomain\db_job', 'password-1' 
go 

CREATE CREDENTIAL [Job3] WITH IDENTITY = N'mydomain\mydomainuser', SECRET =    N'mydomain\mydomainuser' 
GO 

USE [msdb] 
GO 
EXEC msdb.dbo.sp_add_proxy @proxy_name=N'LW_JobRunner',@credential_name=N'Job', 
@enabled=1 
GO 
EXEC msdb.dbo.sp_grant_proxy_to_subsystem @proxy_name=N'LW_JobRunner', @subsystem_id=3 
GO 
EXEC msdb.dbo.sp_grant_login_to_proxy @proxy_name=N'LW_JobRunner',   @login_name=N'mydbuser' 
GO 

/* Setting up job Category */ 

DECLARE @ReturnCode INT 
SELECT @ReturnCode = 0 

EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL',    @name=N'MyJob' 
go 

USE MASTER 
GO 

EXEC master.dbo.xp_sqlagent_proxy_account N'SET', 
    N'DEV',   -- agent_domain_name 
    N'Administrator', -- agent_username 
    N'password'  -- agent_password 

但在执行sp xp_sqlagent_proxy_account的最后一步,说

找不到存储过程xp_sqlagent_proxy_account。

我相信我执行它的master分贝与sa凭证......

的,因为这对增加一个工作,我得到这样

错误在sys.servers中找不到服务器'myIpAddress'。确认 指定了正确的服务器名称。如有必要,执行 存储过程sp_addlinkedserver将服务器添加到sys.servers中

如何解决此问题?

回答

0

当非系统管理员用户尝试执行xp_cmdshell时,通常会发生此错误。但是,您要说明您确定您正在使用sa证书在master db中执行。

你可能想尝试运行此:

-- Enable non-system administrators to run the job and to execute xp_cmdshell. 
EXECUTE msdb..sp_set_sqlagent_properties @sysadmin_only = 0 
GO 

这将允许非SA用户访问xp_cmdshell的。如果它可以正常工作,那意味着脚本运行的用户帐户是非sa帐户。

希望这会有所帮助。

拉吉

+0

我收到以下错误@@ sysadmin_only标志通过的SQLAgent不再支持,在这里只保留向后兼容 – user824910

+0

你是对的。既然你的问题没有谈论SQL版本,我提出了一个通用的建议。我刚才注意到了SQL 2008标签。抱歉 – Raj