我如何将自己的pipe理员权限授予本地SQL Server实例?

我安装了SQL Server 2008 R2到我的本地机器。 但是,由于权限(或缺less),我无法创build新的数据库。

“拒绝创build数据库权限”

所以,我试图将pipe理权限分配给我当前的login

“用户没有执行此操作的权限。”

我也尝试创build一个新的login,将有pipe理员权限,但没有运气。 我如何授予自己的pipe理权限,以便我可以创build一个数据库? 我可以重新安装,但我不喜欢。

是的 – 您似乎忘了在安装SQL Server时将自己添加到sysadminangular色。 如果您是您计算机上的本地pipe理员,则此博客文章可帮助您使用SQLCMD将您的帐户导入到SQL Server系统pipe理员组中,而无需重新安装。 这是SQL Server中的一个安全漏洞,如果你问我,但在这种情况下它会帮助你。

编辑:改变死链接指向archive.org。 原帖在这里

打开命令提示符窗口。 如果您的SQL Server的默认实例已经在运行,请在命令提示符处运行以下命令来停止SQL Server服务:

net stop mssqlserver 

现在转到安装SQL Server的目录。 该目录可以是以下其中之一:

 C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Binn 

找出你的MSSQL目录和CD ,如下所示:

 CD C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn 

现在运行以下命令以单用户模式启动SQL Server。 在指定SQLCMD ,只能创build一个SQLCMD连接(从另一个命令提示符窗口)。

 sqlservr -m"SQLCMD" 

现在,打开另一个命令提示符窗口,与上面以单用户模式启动SQL Server的用户相同,并在其中运行:

 sqlcmd 

然后按回车。 现在,您可以对以单用户模式运行的SQL Server实例执行SQL语句:

 create login [<<DOMAIN\USERNAME>>] from windows; -- For older versions of SQL Server: EXEC sys.sp_addsrvrolemember @loginame = N'<<DOMAIN\USERNAME>>', @rolename = N'sysadmin'; -- For newer versions of SQL Server: ALTER SERVER ROLE [sysadmin] ADD MEMBER [<<DOMAIN\USERNAME>>] GO; 

来源 。

这是一个脚本,声称能够解决这个问题。

使用这个简单的脚本获取pipe理员权限到您的本地SQL Server Express

下载链接到脚本

描述

通过此命令脚本,您可以轻松地将自己添加到本地SQL Server实例的sysadminangular色。 您必须是Windows本地pipe理员组的成员,或者有权访问用户的凭据。 该脚本支持SQL Server 2005及更高版本。

如果您是试图使用由其他人安装的SQL Server 2008 Express的开发人员,该脚本将非常有用。 在这种情况下,您通常不具有SQL Server 2008 Express实例的pipe理权限,因为默认情况下只有安装SQL Server 2008的用户才具有pipe理权限。

安装SQL Server 2008 Express的用户可以使用SQL Server Management Studio向您授予必要的权限。 但是如果SQL Server Management Studio没有安装? 或更糟的是,如果安装用户不可用了?

这个脚本只需点击几下即可解决问题!

注意:您需要为BAT文件提供一个“实例名称”(可能是“MSSQLSERVER”,但可能不会):您可以通过首先在“Microsoft SQL Serverpipe理控制台“:

  SELECT @@servicename 

然后复制BAT文件提示“SQL实例名称”时使用的结果。

  @echo off rem rem **************************************************************************** rem rem Copyright (c) Microsoft Corporation. All rights reserved. rem This code is licensed under the Microsoft Public License. rem THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF rem ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY rem IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR rem PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. rem rem **************************************************************************** rem rem CMD script to add a user to the SQL Server sysadmin role rem rem Input: %1 specifies the instance name to be modified. Defaults to SQLEXPRESS. rem %2 specifies the principal identity to be added (in the form "<domain>\<user>"). rem If omitted, the script will request elevation and add the current user (pre-elevation) to the sysadmin role. rem If provided explicitly, the script is assumed to be running elevated already. rem rem Method: 1) restart the SQL service with the '-m' option, which allows a single connection from a box admin rem (the box admin is temporarily added to the sysadmin role with this start option) rem 2) connect to the SQL instance and add the user to the sysadmin role rem 3) restart the SQL service for normal connections rem rem Output: Messages indicating success/failure. rem Note that if elevation is done by this script, a new command process window is created: the output of this rem window is not directly accessible to the caller. rem rem setlocal set sqlresult=N/A if .%1 == . (set /P sqlinstance=Enter SQL instance name, or default to SQLEXPRESS: ) else (set sqlinstance=%1) if .%sqlinstance% == . (set sqlinstance=SQLEXPRESS) if /I %sqlinstance% == MSSQLSERVER (set sqlservice=MSSQLSERVER) else (set sqlservice=MSSQL$%sqlinstance%) if .%2 == . (set sqllogin="%USERDOMAIN%\%USERNAME%") else (set sqllogin=%2) rem remove enclosing quotes for %%i in (%sqllogin%) do set sqllogin=%%~i @echo Adding '%sqllogin%' to the 'sysadmin' role on SQL Server instance '%sqlinstance%'. @echo Verify the '%sqlservice%' service exists ... set srvstate=0 for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j if .%srvstate% == .0 goto existerror rem rem elevate if <domain/user> was defaulted rem if NOT .%2 == . goto continue echo new ActiveXObject("Shell.Application").ShellExecute("cmd.exe", "/D /Q /C pushd \""+WScript.Arguments(0)+"\" & \""+WScript.Arguments(1)+"\" %sqlinstance% \""+WScript.Arguments(2)+"\"", "", "runas"); >"%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js" call "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js" "%cd%" %0 "%sqllogin%" del "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js" goto :EOF :continue rem rem determine if the SQL service is running rem set srvstarted=0 set srvstate=0 for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j if .%srvstate% == .0 goto queryerror rem rem if required, stop the SQL service rem if .%srvstate% == .1 goto startm set srvstarted=1 @echo Stop the '%sqlservice%' service ... net stop %sqlservice% if errorlevel 1 goto stoperror :startm rem rem start the SQL service with the '-m' option (single admin connection) and wait until its STATE is '4' (STARTED) rem also use trace flags as follows: rem 3659 - log all errors to errorlog rem 4010 - enable shared memory only (lpc:) rem 4022 - do not start autoprocs rem @echo Start the '%sqlservice%' service in maintenance mode ... sc start %sqlservice% -m -T3659 -T4010 -T4022 >nul if errorlevel 1 goto startmerror :checkstate1 set srvstate=0 for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j if .%srvstate% == .0 goto queryerror if .%srvstate% == .1 goto startmerror if NOT .%srvstate% == .4 goto checkstate1 rem rem add the specified user to the sysadmin role rem access tempdb to avoid a misleading shutdown error rem @echo Add '%sqllogin%' to the 'sysadmin' role ... for /F "usebackq tokens=1,3" %%i in (`sqlcmd -S np:\\.\pipe\SQLLocal\%sqlinstance% -E -Q "create table #foo (bar int); declare @rc int; execute @rc = sp_addsrvrolemember '$(sqllogin)', 'sysadmin'; print 'RETURN_CODE : '+CAST(@rc as char)"`) do if .%%i == .RETURN_CODE set sqlresult=%%j rem rem stop the SQL service rem @echo Stop the '%sqlservice%' service ... net stop %sqlservice% if errorlevel 1 goto stoperror if .%srvstarted% == .0 goto exit rem rem start the SQL service for normal connections rem net start %sqlservice% if errorlevel 1 goto starterror goto exit rem rem handle unexpected errors rem :existerror sc query %sqlservice% @echo '%sqlservice%' service is invalid goto exit :queryerror @echo 'sc query %sqlservice%' failed goto exit :stoperror @echo 'net stop %sqlservice%' failed goto exit :startmerror @echo 'sc start %sqlservice% -m' failed goto exit :starterror @echo 'net start %sqlservice%' failed goto exit :exit if .%sqlresult% == .0 (@echo '%sqllogin%' was successfully added to the 'sysadmin' role.) else (@echo '%sqllogin%' was NOT added to the 'sysadmin' role: SQL return code is %sqlresult%.) endlocal pause 

我采用了一个SQL 2012数据库,我不是一个系统pipe理员,而是一台机器上的pipe理员。 我使用“以pipe理员身份运行”SSMS,添加我的NT帐户作为SQLlogin,并将服务器angular色设置为sysadmin。 没问题。

它实际上足以将-m添加到Sql Serverconfigurationpipe理器上的启动参数,重新启动服务,在您的帐户上转到ssms添加checkboxsysadmin,然后删除-m再次重新启动并像往常一样使用。

数据库引擎服务启动选项

-m以单用户模式启动SQL Server的实例。