ALTER DATABASE 失败,因为无法对数据库加锁
我需要重新启动数据库,因为某些进程无法正常工作。我的计划是让它离线然后再次上线。
我试图在 Sql Server Management Studio 2008 中执行此操作:
use master;
go
alter database qcvalues
set single_user
with rollback immediate;
alter database qcvalues
set multi_user;
go
我收到这些错误:
Msg 5061, Level 16, State 1, Line 1
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Msg 5061, Level 16, State 1, Line 4
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 4
ALTER DATABASE statement failed.
我做错了什么?
I need to restart a database because some processes are not working. My plan is to take it offline and back online again.
I am trying to do this in Sql Server Management Studio 2008:
use master;
go
alter database qcvalues
set single_user
with rollback immediate;
alter database qcvalues
set multi_user;
go
I am getting these errors:
Msg 5061, Level 16, State 1, Line 1
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Msg 5061, Level 16, State 1, Line 4
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 4
ALTER DATABASE statement failed.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
收到错误后,运行
在列表中查找数据库。连接可能未终止。如果发现任何与数据库的连接,请运行
,其中
是连接到数据库的会话的 SPID。删除与数据库的所有连接后尝试您的脚本。
不幸的是,我不知道您遇到此问题的原因,但这里有一个链接,显示该问题已发生在其他地方。
http://www.geakeit.co.uk/2010/12/11/sql-take-offline-fails-alter-database-failed-because-a-lock-could-not-错误5061/
After you get the error, run
Look for the database in the list. It's possible that a connection was not terminated. If you find any connections to the database, run
where
<SPID>
is the SPID for the sessions that are connected to the database.Try your script after all connections to the database are removed.
Unfortunately, I don't have a reason why you're seeing the problem, but here is a link that shows that the problem has occurred elsewhere.
http://www.geakeit.co.uk/2010/12/11/sql-take-offline-fails-alter-database-failed-because-a-lock-could-not-error-5061/
我通过执行以下操作成功重现了此错误。
连接 1(保持运行几分钟)
连接 2 和 3
I managed to reproduce this error by doing the following.
Connection 1 (leave running for a couple of minutes)
Connections 2 and 3
我会在这里添加这一点,以防有人像我一样幸运。
查看 sp_who2 进程列表时,请注意不仅为受影响的数据库运行的进程,还为 master 运行的进程。就我而言,阻止数据库的问题与启动 xp_cmdshell 的存储过程有关。
检查 master 数据库是否有任何处于 KILL/RollBack 状态的进程。
如果您遇到同样的问题,仅 KILL 命令可能无济于事。
您可以重新启动SQL Server,或者更好的方法是在SQL Server操作系统的Windows进程下找到cmd.exe并将其杀死。
I will add this here in case someone will be as lucky as me.
When reviewing the sp_who2 list of processes note the processes that run not only for the effected database but also for master. In my case the issue that was blocking the database was related to a stored procedure that started a xp_cmdshell.
Check if you have any processes in KILL/RollBack state for master database
If you have the same issue, just the KILL command will probably not help.
You can restarted the SQL server, or better way is to find the cmd.exe under windows processes on SQL server OS and kill it.
如果它是“过渡中”,请尝试此操作...
http://learnmysql.blogspot.com/2012/05/database-is-in-transition-try-statement.html
Try this if it is "in transition" ...
http://learnmysql.blogspot.com/2012/05/database-is-in-transition-try-statement.html
只是补充我的两分钱。我将自己置于同样的情况,同时搜索数据库登录成功运行该语句所需的最低权限:
当使用 sysadmin 执行时,ALTER 语句似乎成功完成 登录,但在“仅”具有有限权限的登录下执行时,它需要连接清理部分,例如:
PS 我花了几个小时试图弄清楚为什么“ALTER DATABASE..”在以下情况下不起作用在具有 dbcreator 角色 + ALTER ANY DATABASE 权限的登录名下执行。这是我的 MSDN 线程!
Just to add my two cents. I've put myself into the same situation, while searching the minimum required privileges of a db login to run successfully the statement:
It seems that the ALTER statement completes successfully, when executed with a sysadmin login, but it requires the connections cleanup part, when executed under a login which has "only" limited permissions like:
P.S. I've spent hours trying to figure out why the "ALTER DATABASE.." does not work when executed under a login that has dbcreator role + ALTER ANY DATABASE privileges. Here's my MSDN thread!
在 SQL Management Studio 中,转到安全性 ->登录并双击您的登录名。从左列中选择服务器角色,并验证是否选中了 sysadmin。
就我而言,我登录的帐户没有该权限。
哈!
In SQL Management Studio, go to Security -> Logins and double click your Login. Choose Server Roles from the left column, and verify that sysadmin is checked.
In my case, I was logged in on an account without that privilege.
HTH!
杀死进程 ID 对我来说效果很好。
当在新的查询窗口上运行“EXEC sp_who2”命令时...并过滤“繁忙”数据库的结果时,使用“KILL”命令杀死进程就成功了。之后一切又恢复正常了。
Killing the process ID worked nicely for me.
When running "EXEC sp_who2" Command over a new query window... and filter the results for the "busy" database , Killing the processes with "KILL " command managed to do the trick. After that all worked again.
我知道这是一篇旧文章,但我最近遇到了一个非常类似的问题。不幸的是,我无法使用任何更改数据库命令,因为无法放置独占锁。但我始终无法找到与数据库的开放连接。我最终不得不强行删除数据库的健康状态,以强制其进入还原状态而不是恢复状态。
I know this is an old post but I recently ran into a very similar problem. Unfortunately I wasn't able to use any of the alter database commands because an exclusive lock couldn't be placed. But I was never able to find an open connection to the db. I eventually had to forcefully delete the health state of the database to force it into a restoring state instead of in recovery.
在极少数情况下(例如,提交大量事务后),正在运行的 CHECKPOINT 系统进程在数据库文件上持有 FILE 锁,会阻止转换到 MULTI_USER 模式。
In rare cases (e.g., after a heavy transaction is commited) a running CHECKPOINT system process holding a FILE lock on the database file prevents transition to MULTI_USER mode.
在我的场景中,sp_who2 下没有进程阻止数据库。但是,我们发现,由于该数据库比其他数据库大得多,因此挂起的进程仍在运行,这就是为什么在我们尝试通过右键单击暂停的数据库“恢复数据”后,可用性组下的数据库仍然显示为红色/脱机。
要检查是否仍有进程在运行,只需执行以下命令:
从 sys.dm_exec_requests 选择完成百分比
其中%_complete > 0
In my scenario, there was no process blocking the database under sp_who2. However, we discovered because the database is much larger than our other databases that pending processes were still running which is why the database under the availability group still displayed as red/offline after we tried to 'resume data'by right clicking the paused database.
To check if you still have processes running just execute this command:
select percent complete from sys.dm_exec_requests
where percent_complete > 0