ALTER DATABASE 失败,因为无法对数据库加锁

发布于 2024-10-11 20:36:03 字数 716 浏览 4 评论 0原文

我需要重新启动数据库,因为某些进程无法正常工作。我的计划是让它离线然后再次上线。

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

瞳孔里扚悲伤 2024-10-18 20:36:03

收到错误后,运行

EXEC sp_who2

在列表中查找数据库。连接可能未终止。如果发现任何与数据库的连接,请运行

KILL <SPID>

,其中 是连接到数据库的会话的 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

EXEC sp_who2

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

KILL <SPID>

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/

强者自强 2024-10-18 20:36:03

我通过执行以下操作成功重现了此错误。

连接 1(保持运行几分钟)

CREATE DATABASE TESTING123
GO

USE TESTING123;

SELECT NEWID() AS X INTO FOO
FROM sys.objects s1,sys.objects s2,sys.objects s3,sys.objects s4 ,sys.objects s5 ,sys.objects s6

连接 2 和 3

set lock_timeout 5;

ALTER DATABASE TESTING123 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

I managed to reproduce this error by doing the following.

Connection 1 (leave running for a couple of minutes)

CREATE DATABASE TESTING123
GO

USE TESTING123;

SELECT NEWID() AS X INTO FOO
FROM sys.objects s1,sys.objects s2,sys.objects s3,sys.objects s4 ,sys.objects s5 ,sys.objects s6

Connections 2 and 3

set lock_timeout 5;

ALTER DATABASE TESTING123 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
策马西风 2024-10-18 20:36:03

我会在这里添加这一点,以防有人像我一样幸运。

查看 sp_who2 进程列表时,请注意不仅为受影响的数据库运行的进程,还为 ma​​ster 运行的进程。就我而言,阻止数据库的问题与启动 xp_cmdshell 的存储过程有关。

检查 ma​​ster 数据库是否有任何处于 KILL/RollBack 状态的进程。

SELECT *
FROM sys.sysprocesses
WHERE cmd = 'KILLED/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

SELECT *
FROM sys.sysprocesses
WHERE cmd = 'KILLED/ROLLBACK'

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.

尹雨沫 2024-10-18 20:36:03

如果它是“过渡中”,请尝试此操作...

http://learnmysql.blogspot.com/2012/05/database-is-in-transition-try-statement.html

USE master
GO

ALTER DATABASE <db_name>

SET OFFLINE WITH ROLLBACK IMMEDIATE
...
...
ALTER DATABASE <db_name> SET ONLINE

Try this if it is "in transition" ...

http://learnmysql.blogspot.com/2012/05/database-is-in-transition-try-statement.html

USE master
GO

ALTER DATABASE <db_name>

SET OFFLINE WITH ROLLBACK IMMEDIATE
...
...
ALTER DATABASE <db_name> SET ONLINE
楠木可依 2024-10-18 20:36:03

只是补充我的两分钱。我将自己置于同样的情况,同时搜索数据库登录成功运行该语句所需的最低权限:

ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE

当使用 sysadmin 执行时,ALTER 语句似乎成功完成 登录,但在“仅”具有有限权限的登录下执行时,它需要连接清理部分,例如:

ALTER ANY DATABASE

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:

ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE

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:

ALTER ANY DATABASE

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!

溺ぐ爱和你が 2024-10-18 20:36:03

在 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!

夏至、离别 2024-10-18 20:36:03

杀死进程 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.

听不够的曲调 2024-10-18 20:36:03

我知道这是一篇旧文章,但我最近遇到了一个非常类似的问题。不幸的是,我无法使用任何更改数据库命令,因为无法放置独占锁。但我始终无法找到与数据库的开放连接。我最终不得不强行删除数据库的健康状态,以强制其进入还原状态而不是恢复状态。

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.

淡看悲欢离合 2024-10-18 20:36:03

在极少数情况下(例如,提交大量事务后),正在运行的 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.

一枫情书 2024-10-18 20:36:03

在我的场景中,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文