分离数据库/脱机失败

发布于 2024-08-03 01:23:31 字数 1105 浏览 4 评论 0原文

我目前正在分离生产服务器上的开发数据库。由于这是生产服务器,我不想重新启动 sql 服务。这是最坏的情况。

显然我尝试通过 SSMS 分离它。告诉我有一个活动连接,我断开了它。第二次分离时,它告诉我这是不可能的,因为它正在使用中。

我尝试了 EXEC sp_detach_db 'DB' 但没有成功。

我尝试让数据库脱机。它运行了大约 15 分钟,当我感到无聊时,我就把它关掉了。

无论如何,我尝试了一切...我确保使用 SSMS 分离数据库中的连接指示器来终止所有连接。

以下返回 0 个结果:

USE master SELECT * FROM sys.sysprocesses WHERE dbid = DB_ID('DB')

以下内容现在运行了 18 分钟:

ALTER DATABASE DB SET OFFLINE WITH ROLLBACK IMMEDIATE

我确实在这一切过程中定期重新启动 SMSS,以确保 SSMS 不是罪魁祸首通过无形地锁定某些东西。

难道就没有办法暴力破解吗?数据库模式是我非常喜欢的东西,但数据是消耗性的。

希望有某种快速修复方法吗? :)

DBA 今晚将尝试重置该流程,但我想知道解决此问题的方法,以防万一。

谢谢!

ps:我正在使用 DTC ...所以也许这可以解释为什么我的数据库突然被锁定?

编辑:

我现在正在执行以下操作,这会导致最终部分的无限执行。第一个查询甚至返回 0,所以我认为杀死用户根本不重要。

使用[大师] GO

SELECT * FROM sys.sysprocesses WHERE dbid = DB_ID('Database')

GO

DECLARE @return_value int

EXEC @return_value = [dbo].[usp_KillUsers] @p_DBName = 'Database'

SELECT 'Return Value' = @return_value

GO

ALTER DATABASE 数据库通过回滚立即脱机

GO

I'm currently in the process of detaching a development database on the production server. Since this is a production server I don't want to restart the sql service. That is the worst case scenario.

Obviously I tried detaching it through SSMS. Told me there was an active connection and I disconnected it. When detaching the second time it told me that was impossible since it was in use.

I tried EXEC sp_detach_db 'DB' with no luck.

I tried getting the database offline. That ran for about 15 minutes when I got bored and turned it off.

Anyway, I tried everything ... I made sure all connections were killed using the connections indicator in detach database using SSMS.

The following returned 0 results:

USE master
SELECT * FROM sys.sysprocesses WHERE dbid = DB_ID('DB')

And the following is running for 18 minutes now:

ALTER DATABASE DB SET OFFLINE WITH ROLLBACK IMMEDIATE

I did restart SMSS regularly during all this to make sure SSMS wasn't the culprit by locking something invisibly.

Isn't there a way to brute force it? The database schema is something I'm pretty fond of but the data is expendable.

Hopefully there is some sort of a quick fix? :)

The DBA will try to reset the process tonight but I'd like to know the fix for this just in case.

Thx!

ps: I'm using DTC ... so perhaps this might explain why my database got locked up all of a sudden?

edit:

I'm now doing the following which results in an infinite execution of the final part. The first query even returns 0, so I suppose the killing of the users won't even matter.

USE [master]
GO

SELECT * FROM sys.sysprocesses WHERE dbid = DB_ID('Database')

GO

DECLARE @return_value int

EXEC @return_value = [dbo].[usp_KillUsers]
@p_DBName = 'Database'

SELECT 'Return Value' = @return_value

GO

ALTER DATABASE Database SET OFFLINE WITH ROLLBACK IMMEDIATE

GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

以可爱出名 2024-08-10 01:23:31

您如何连接到 SQL Server?您是否有可能在自己连接到数据库时尝试分离数据库?这可能会阻止分离,具体取决于所涉及的 SQL Server 版本。

您可以尝试使用 DAC 来实现类似的功能。

How are you connecting to SQL Server? Is it possible that you're trying to detach the database while you yourself are connected to it? This can block a Detach, depending on the version of SQL Server involved.

You can try using the DAC for stuff like this.

予囚 2024-08-10 01:23:31

尝试在分离数据库之前终止所有连接,IE:

    USE [master]
GO
/****** Object:  StoredProcedure [dbo].[usp_KillUsers]    Script Date: 08/18/2009 10:42:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_KillUsers]
  @p_DBName SYSNAME = NULL
AS

/* Check Paramaters                    */
/* Check for a DB name                 */
IF (@p_DBName IS NULL)
BEGIN
  PRINT 'You must supply a DB Name'
  RETURN
END -- DB is NULL
IF (@p_DBName = 'master')
BEGIN
  PRINT 'You cannot run this process against the master database!'
  RETURN
END -- Master supplied
IF (@p_DBName = DB_NAME())
BEGIN
  PRINT 'You cannot run this process against your connections database!'
  RETURN
END -- your database supplied

SET NOCOUNT ON

/* Declare Variables                   */
DECLARE @v_spid INT,
        @v_SQL  NVARCHAR(255)

/* Declare the Table Cursor (Identity) */
DECLARE c_Users CURSOR
   FAST_FORWARD FOR
 SELECT spid
   FROM master..sysprocesses (NOLOCK)
  WHERE db_name(dbid) LIKE @p_DBName

OPEN c_Users

FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS <> -1)
BEGIN
  IF (@@FETCH_STATUS <> -2)
  BEGIN
    SELECT @v_SQL = 'KILL ' + CONVERT(NVARCHAR, @v_spid)
--    PRINT @v_SQL
    EXEC (@v_SQL)
  END -- -2
  FETCH NEXT FROM c_Users INTO @v_spid
END -- While

CLOSE c_Users
DEALLOCATE c_Users

这是一个终止所有用户与数据库的连接的脚本,只需传递数据库名称,它就会关闭它们。然后你可以尝试分离数据库。这个脚本是我不久前发现的,我不能声称它是我自己的。我并不是说这是任何形式的抄袭,我只是没有来源。

Try killing all connections before detaching the database, IE:

    USE [master]
GO
/****** Object:  StoredProcedure [dbo].[usp_KillUsers]    Script Date: 08/18/2009 10:42:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_KillUsers]
  @p_DBName SYSNAME = NULL
AS

/* Check Paramaters                    */
/* Check for a DB name                 */
IF (@p_DBName IS NULL)
BEGIN
  PRINT 'You must supply a DB Name'
  RETURN
END -- DB is NULL
IF (@p_DBName = 'master')
BEGIN
  PRINT 'You cannot run this process against the master database!'
  RETURN
END -- Master supplied
IF (@p_DBName = DB_NAME())
BEGIN
  PRINT 'You cannot run this process against your connections database!'
  RETURN
END -- your database supplied

SET NOCOUNT ON

/* Declare Variables                   */
DECLARE @v_spid INT,
        @v_SQL  NVARCHAR(255)

/* Declare the Table Cursor (Identity) */
DECLARE c_Users CURSOR
   FAST_FORWARD FOR
 SELECT spid
   FROM master..sysprocesses (NOLOCK)
  WHERE db_name(dbid) LIKE @p_DBName

OPEN c_Users

FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS <> -1)
BEGIN
  IF (@@FETCH_STATUS <> -2)
  BEGIN
    SELECT @v_SQL = 'KILL ' + CONVERT(NVARCHAR, @v_spid)
--    PRINT @v_SQL
    EXEC (@v_SQL)
  END -- -2
  FETCH NEXT FROM c_Users INTO @v_spid
END -- While

CLOSE c_Users
DEALLOCATE c_Users

This is a script to kill all user connections to a database, just pass the database name, and it will close them. Then you can try to detach the database. This script is one I found a while back and I cannot claim it as my own. I do not mean this as any sort of plagarism, I just don't have the source.

假装爱人 2024-08-10 01:23:31

SELECT DISTINCT req_transactionUOW FROM syslockinfo

KILL 'number_returned' (process_id -2 的那个)

原因是 DTC 有点烦人,并且由于事务失败而完全锁定了数据库。现在我想知道发生这种情况的原因。但至少它让我能够在问题再次发生时重置损坏的事务。

我将其发布在这里,因为我确信它会帮助一些遇到相同问题的人。

SELECT DISTINCT req_transactionUOW FROM syslockinfo

KILL 'number_returned' (the one(s) with process_id -2)

The cause was DTC being a little bit annoying and locking up the database completely with a failed transaction. Now I would like to know the reason why this happened. But at least it gives me the ability to reset the broken transactions when the problem re-occurs.

I'm posting it here since I'm sure it'll help some people who are experiencing the same issues.

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