恢复备份时,如何断开所有活动连接?

发布于 2024-07-27 00:36:36 字数 51 浏览 6 评论 0原文

由于活动连接,My SQL Server 2005 无法恢复备份。 我怎样才能强迫它?

My SQL Server 2005 doesn't restore a backup because of active connections. How can I force it?

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

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

发布评论

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

评论(10

海的爱人是光 2024-08-03 00:36:36

您想要将数据库设置为单用户模式,进行恢复,然后将其设置回多用户模式:

ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK AFTER 60 --this will give your current connections 60 seconds to complete

--Do Actual Restore
RESTORE DATABASE YourDB
FROM DISK = 'D:\BackUp\YourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:\Data\YourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:\Data\YourLDFFile.ldf'

/*If there is no error in statement before database will be in multiuser
mode.  If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO

参考:Pinal Dave (http: //blog.SQLAuthority.com

官方参考:https:// msdn.microsoft.com/en-us/library/ms345598.aspx

You want to set your db to single user mode, do the restore, then set it back to multiuser:

ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK AFTER 60 --this will give your current connections 60 seconds to complete

--Do Actual Restore
RESTORE DATABASE YourDB
FROM DISK = 'D:\BackUp\YourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:\Data\YourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:\Data\YourLDFFile.ldf'

/*If there is no error in statement before database will be in multiuser
mode.  If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO

Reference : Pinal Dave (http://blog.SQLAuthority.com)

Official reference: https://msdn.microsoft.com/en-us/library/ms345598.aspx

〆凄凉。 2024-08-03 00:36:36

SQL Server Management Studio 2005

当您右键单击数据库并单击任务,然后单击分离数据库时,它会弹出一个包含活动连接的对话框。

分离屏幕

通过单击“消息”下的超链接,您可以终止活动连接。

然后,您可以终止这些连接,而无需分离数据库。

更多信息请参见此处

SQL Server Management Studio 2008

SQL Server Management Studio 2008 的界面已更改,步骤如下(通过:Tim Leung)

  1. 在对象资源管理器中右键单击服务器并选择“活动监视器”。
  2. 打开后,展开进程组。
  3. 现在使用下拉列表按数据库名称过滤结果。
  4. 通过选择右键单击“终止进程”选项来终止服务器连接。

SQL Server Management Studio 2005

When you right click on a database and click Tasks and then click Detach Database, it brings up a dialog with the active connections.

Detach Screen

By clicking on the hyperlink under "Messages" you can kill the active connections.

You can then kill those connections without detaching the database.

More information here.

SQL Server Management Studio 2008

The interface has changed for SQL Server Management studio 2008, here are the steps (via: Tim Leung)

  1. Right-click the server in Object Explorer and select 'Activity Monitor'.
  2. When this opens, expand the Processes group.
  3. Now use the drop-down to filter the results by database name.
  4. Kill off the server connections by selecting the right-click 'Kill Process' option.
优雅的叶子 2024-08-03 00:36:36

这段代码对我有用,它杀死了数据库的所有现有连接。
您所要做的就是更改 Set @dbname = 'databaseName' 行,以便它具有您的数据库名称。

Use Master
Go

Declare @dbname sysname

Set @dbname = 'databaseName'

Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = min(spid) from master.dbo.sysprocesses
        where dbid = db_id(@dbname) and spid > @spid
End

之后我能够恢复它

This code worked for me, it kills all existing connections of a database.
All you have to do is change the line Set @dbname = 'databaseName' so it has your database name.

Use Master
Go

Declare @dbname sysname

Set @dbname = 'databaseName'

Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = min(spid) from master.dbo.sysprocesses
        where dbid = db_id(@dbname) and spid > @spid
End

after this I was able to restore it

马蹄踏│碎落叶 2024-08-03 00:36:36

尝试这个:

DECLARE UserCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT
    spid
FROM
    master.dbo.sysprocesses
WHERE DB_NAME(dbid) = 'dbname'--replace the dbname with your database
DECLARE @spid SMALLINT
DECLARE @SQLCommand VARCHAR(300)
OPEN UserCursor
FETCH NEXT FROM UserCursor INTO
    @spid
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @SQLCommand = 'KILL ' + CAST(@spid AS VARCHAR)
    EXECUTE(@SQLCommand)
    FETCH NEXT FROM UserCursor INTO
        @spid
END
CLOSE UserCursor
DEALLOCATE UserCursor
GO

Try this:

DECLARE UserCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT
    spid
FROM
    master.dbo.sysprocesses
WHERE DB_NAME(dbid) = 'dbname'--replace the dbname with your database
DECLARE @spid SMALLINT
DECLARE @SQLCommand VARCHAR(300)
OPEN UserCursor
FETCH NEXT FROM UserCursor INTO
    @spid
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @SQLCommand = 'KILL ' + CAST(@spid AS VARCHAR)
    EXECUTE(@SQLCommand)
    FETCH NEXT FROM UserCursor INTO
        @spid
END
CLOSE UserCursor
DEALLOCATE UserCursor
GO
原来分手还会想你 2024-08-03 00:36:36

重新启动 SQL Server 将断开用户连接。 我发现的最简单的方法 - 如果你想让服务器离线也很好。

但由于一些非常奇怪的原因,“脱机”选项不能可靠地执行此操作,并且可能会挂起或混淆管理控制台。 重新启动然后脱机工作

有时这是一个选项 - 例如,如果您停止了作为连接源的网络服务器。

Restarting SQL server will disconnect users. Easiest way I've found - good also if you want to take the server offline.

But for some very wierd reason the 'Take Offline' option doesn't do this reliably and can hang or confuse the management console. Restarting then taking offline works

Sometimes this is an option - if for instance you've stopped a webserver that is the source of the connections.

王权女流氓 2024-08-03 00:36:36

我在 SQL Server 2008 中自动执行恢复过程时遇到了这个问题。
我的(成功的)方法是结合了所提供的两个答案。

首先,我运行该数据库的所有连接,并终止它们。

DECLARE @SPID int = (SELECT TOP 1 SPID FROM sys.sysprocess WHERE dbid = db_id('dbName'))
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = top 1 spid from master.dbo.sysprocesses
        where dbid = db_id('dbName')
End

然后,我将数据库设置为单用户模式

ALTER DATABASE dbName SET SINGLE_USER

然后,我运行恢复...

RESTORE DATABASE and whatnot

再次终止连接

(same query as above)

并将数据库设置回多用户模式。

ALTER DATABASE dbName SET MULTI_USER

这样,我确保在设置为单一模式之前没有任何连接占用数据库,因为如果有连接,前者就会冻结。

I ran across this problem while automating a restore proccess in SQL Server 2008.
My (successfull) approach was a mix of two of the answers provided.

First, I run across all the connections of said database, and kill them.

DECLARE @SPID int = (SELECT TOP 1 SPID FROM sys.sysprocess WHERE dbid = db_id('dbName'))
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = top 1 spid from master.dbo.sysprocesses
        where dbid = db_id('dbName')
End

Then, I set the database to a single_user mode

ALTER DATABASE dbName SET SINGLE_USER

Then, I run the restore...

RESTORE DATABASE and whatnot

Kill the connections again

(same query as above)

And set the database back to multi_user.

ALTER DATABASE dbName SET MULTI_USER

This way, I ensure that there are no connections holding up the database before setting to single mode, since the former will freeze if there are.

峩卟喜欢 2024-08-03 00:36:36

这些都不适合我,无法删除或断开当前用户的连接。 也看不到任何与数据库的活动连接。 重新启动 SQL Server(右键单击并选择重新启动)允许我执行此操作。

None of these were working for me, couldn't delete or disconnect current users. Also couldn't see any active connections to the DB. Restarting SQL Server (Right click and select Restart) allowed me to do it.

抚你发端 2024-08-03 00:36:36

要添加到已经给出的建议中,如果您有一个通过 IIS 运行且使用数据库的 Web 应用程序,您可能还需要在恢复时停止(而不是回收)该应用程序的应用程序池,然后重新启动-开始。 停止应用程序池会终止活动的 http 连接,并且不再允许更多连接,否则最终可能会允许触发连接数据库的进程,从而锁定数据库。 这是 Umbraco 内容管理系统在恢复数据库时的一个已知问题

To add to advice already given, if you have a web app running through IIS that uses the DB, you may also need to stop (not recycle) the app pool for the app while you restore, then re-start. Stopping the app pool kills off active http connections and doesn't allow any more, which could otherwise end up allowing processes to be triggered that connect to and thereby lock the database. This is a known issue for example with the Umbraco Content Management System when restoring its database

难理解 2024-08-03 00:36:36

以上都不适合我。 我的数据库没有使用活动监视器或 sp_who 显示任何活动连接。 我最终不得不:

  • 右键单击​​数据库节点
  • 选择“分离...”
  • 检查“删除连接”框
  • 重新附加

不是最优雅的解决方案,但它可以工作,并且不需要重新启动 SQL Server(这对我来说不是一个选项,因为数据库服务器托管了许多其他数据库)

None of the above worked for me. My database didn't show any active connections using Activity Monitor or sp_who. I ultimately had to:

  • Right click the database node
  • Select "Detach..."
  • Check the "Drop Connections" box
  • Reattach

Not the most elegant solution but it works and it doesn't require restarting SQL Server (not an option for me, since the DB server hosted a bunch of other databases)

尬尬 2024-08-03 00:36:36

我更喜欢这样做,

更改数据库设置为脱机并立即回滚

,然后恢复数据库。
之后,

更改数据库设置并立即回滚

I prefer to do like this,

alter database set offline with rollback immediate

and then restore your database.
after that,

alter database set online with rollback immediate

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