恢复备份时,如何断开所有活动连接?
由于活动连接,My SQL Server 2005 无法恢复备份。 我怎样才能强迫它?
My SQL Server 2005 doesn't restore a backup because of active connections. How can I force it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您想要将数据库设置为单用户模式,进行恢复,然后将其设置回多用户模式:
参考: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:
Reference : Pinal Dave (http://blog.SQLAuthority.com)
Official reference: https://msdn.microsoft.com/en-us/library/ms345598.aspx
SQL Server Management Studio 2005
当您右键单击数据库并单击
任务
,然后单击分离数据库
时,它会弹出一个包含活动连接的对话框。通过单击“消息”下的超链接,您可以终止活动连接。
然后,您可以终止这些连接,而无需分离数据库。
更多信息请参见此处。
SQL Server Management Studio 2008
SQL Server Management Studio 2008 的界面已更改,步骤如下(通过:Tim Leung)
SQL Server Management Studio 2005
When you right click on a database and click
Tasks
and then clickDetach Database
, it brings up a dialog with the active connections.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)
这段代码对我有用,它杀死了数据库的所有现有连接。
您所要做的就是更改 Set @dbname = 'databaseName' 行,以便它具有您的数据库名称。
之后我能够恢复它
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.
after this I was able to restore it
尝试这个:
Try this:
重新启动 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.
我在 SQL Server 2008 中自动执行恢复过程时遇到了这个问题。
我的(成功的)方法是结合了所提供的两个答案。
首先,我运行该数据库的所有连接,并终止它们。
然后,我将数据库设置为单用户模式
然后,我运行恢复...
再次终止连接
并将数据库设置回多用户模式。
这样,我确保在设置为单一模式之前没有任何连接占用数据库,因为如果有连接,前者就会冻结。
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.
Then, I set the database to a single_user mode
Then, I run the restore...
Kill the connections again
And set the database back to 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.
这些都不适合我,无法删除或断开当前用户的连接。 也看不到任何与数据库的活动连接。 重新启动 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.
要添加到已经给出的建议中,如果您有一个通过 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
以上都不适合我。 我的数据库没有使用活动监视器或 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:
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)
我更喜欢这样做,
更改数据库设置为脱机并立即回滚
,然后恢复数据库。
之后,
更改数据库设置并立即回滚
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