无法恢复 sql server 中的数据库(单用户)
我正在尝试恢复 sql server 2005 Express 版本中的数据库。我知道要恢复数据库,我需要将其设置为单用户。我给出这个命令是为了使其成为单用户
USE [master]
ALTER DATABASE database_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE
。该命令正确执行,我什至可以在该数据库的对象资源管理器中看到一个小图像,显示现在是单用户。
现在我正在尝试按照以下步骤恢复数据库 -> 右键单击数据库和任务,然后恢复数据库。我选择备份文件所在的路径并单击恢复。
但我仍然收到该错误“无法获得独占访问权限,因为数据库正在使用中(microsoft.sqlserver.smo)。我错过了什么吗?我已经用谷歌搜索了它,并且大多数网站都表明数据库需要位于单用户中 :
我没有尝试过分离和附加数据库方法,我想知道这样做是否安全
感谢您的回答,所以我是。 我什至从选项
中选择了覆盖现有数据库。
I am trying to restore a database in my sql server 2005 express edition. I know that to restore the database I need to make it to single user. I am giving this command to make it to single user
USE [master]
ALTER DATABASE database_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE
This command executed properly and I can even see a small image in the object explorer on this database showing that this is now single user.
Now I am trying to restore the database, by following these steps
->right click on the database and tasks and then to restore database. I am selecting the path where the backup file is located and clicking on restore.
But I still get that error "Exclusive access could not be obtained because database is in use(microsoft.sqlserver.smo). Am I missing anything. I have googled it and all most all the sites suggest that database needs to be in single user mode and nothing else.
I did not try the detach and attaching of database method. I have never done that before and would like to know if that is safe to do.
edit: thanks for the answers. Both suggested me same answer so I am marking one answer as chosen.
I even selected overwrite the existing database from options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,最好进行备份和恢复,而不是分离和附加。
其次,您用来将数据库设置为
SINGLE_USER
的会话很可能是在您尝试运行恢复时仍然拥有该数据库的会话(因为您使用的是 GUI,所以它正在连接在它自己的会话下,因此无法访问)。要么作为文本命令进行恢复,要么切换查询窗口以首先使用另一个数据库,例如 master。或者您可以关闭查询窗口,使其不再连接。
您始终可以使用
select * from master.dbo.sysprocesses
检查谁已连接。更新
假设您要恢复的数据库已经存在,并且磁盘上只有一个备份文件(其中没有多个备份),并且恢复后不需要恢复日志文件完整备份,然后通过脚本恢复非常非常简单:
学习此语法将使您的生活更轻松,因为当您设置 SINGLE_USER 时,您已经处于连接的唯一会话中。另外,我发现输入恢复命令比使用 GUI 更快,而且我喜欢对事物的控制。重复这一点最终会在你的脑海中巩固它,你不必再查找语法了。
恢复日志文件甚至没有那么困难。只需记住一件简单的事情,
WITH NORECOVERY
:那里...您已经非常轻松地恢复了日志文件。您甚至可以使用
WITH STOPAT
恢复到准确的时间点!另外,如果您忘记并意外提交了最后一个日志恢复语句WITH NORECOVERY
,那么您只需发出RESTORE DATABASE DBName WITH RECOVERY;
来执行最后的步骤以使数据库可用(回滚未提交的事务等)。First, it's best to back up and restore rather than detach and attach.
Second, it's most likely that the session you're using to set the database to
SINGLE_USER
is the one that still has it when you try to run the restore (since you're using the GUI, it's connecting under its own session so it's unable to get access).Either do the restore as a text command or switch the query window to use another database first, such as master. Or you could just close the query window so it's no longer connected.
You can always check who's connected with
select * from master.dbo.sysprocesses
.Update
Assuming the database you want to restore already exists, and if you have a single backup file on disk (that doesn't have multiple backups in it) and there's no need to restore log files after restoring the full backup, then restoring via script is super, super easy:
Learning this syntax will make your life easier because then when you set SINGLE_USER you're already in the sole session that is connected. Plus, I find that typing the restore command is faster than using the GUI, and I like the control I have over things. Repetition of this eventually cements it in your mind and you don't have to look up the syntax any more.
It's not even that difficult to restore log files. Just one simple thing to remember,
WITH NORECOVERY
:There... you've restored your log files, very easily. You can even restore to an exact point in time using
WITH STOPAT
! Also, if you forget and accidentally submit the last log restore statementWITH NORECOVERY
then you just issueRESTORE DATABASE DBName WITH RECOVERY;
to perform the final steps to make the database usable (rolling back uncommitted transactions, etc.).您可以使用此脚本终止使用数据库的所有进程,然后尝试再次恢复它:
You can use this script to kill all processes using the database and then try to restore it again:
祝你好运。
Good luck.