xp_regread() returned error 5, 'Access is denied.'

感情迁移 提交于 2019-11-29 16:02:29

You might need to add the account to the sysadmin server role, which allows a member to perform every activity.

For 2008r2, execute this command:

EXEC sp_addsrvrolemember 'NT AUTHORITY\SYSTEM', 'sysadmin';

This is a common error encountered when attempting to update the registry from SQL Server, and there are some weird and non-obvious pathing issues in newer versions such as SQL Server 2017.

The error is not due to security within SQL Server, but instead Windows security related to the permissions on the registry keys as relates to the users under which specific SQL Server processes are running.

For instance, to execute MSSQL related registry commands from SQL Server, the system group NETWORKSERVICE needs full control on the relevant registry path. This is because SQL Agent (by default) runs as a user in that system group.

For SQL Server settings, the Registry path is: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server

As an example, the command in SQL Server to set the log file size limit to be 1024 KB is as follows:

USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
 N'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLServer', 
 N'ErrorLogSizeInKb', REG_DWORD, 1024
GO

Also note the truncated path above. The actual full registry path (for SQL2017) is as follows:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQLServer

The xp_instance_regwrite SQL Server command automatically injects the version key into the path. For SQL Server 2017 that key is MSSQL14.MSSQLSERVER.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!