无法恢复 sql server 中的数据库(单用户)

发布于 2024-09-17 19:36:49 字数 516 浏览 8 评论 0原文

我正在尝试恢复 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 技术交流群。

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

发布评论

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

评论(3

探春 2024-09-24 19:36:49

首先,最好进行备份和恢复,而不是分离和附加。

其次,您用来将数据库设置为 SINGLE_USER 的会话很可能是在您尝试运行恢复时仍然拥有该数据库的会话(因为您使用的是 GUI,所以它正在连接在它自己的会话下,因此无法访问)。

要么作为文本命令进行恢复,要么切换查询窗口以首先使用另一个数据库,例如 master。或者您可以关闭查询窗口,使其不再连接。

您始终可以使用 select * from master.dbo.sysprocesses 检查谁已连接。

更新

假设您要恢复的数据库已经存在,并且磁盘上只有一个备份文件(其中没有多个备份),并且恢复后不需要恢复日志文件完整备份,然后通过脚本恢复非常非常简单:

RESTORE DATABASE DBName FROM DISK = 'C:\path\DBNameBackup.bak';

学习此语法将使您的生活更轻松,因为当您设置 SINGLE_USER 时,您已经处于连接的唯一会话中。另外,我发现输入恢复命令比使用 GUI 更快,而且我喜欢对事物的控制。重复这一点最终会在你的脑海中巩固它,你不必再查找语法了。

恢复日志文件甚至没有那么困难。只需记住一件简单的事情,WITH NORECOVERY

RESTORE DATABASE DBName FROM DISK = 'C:\path\DBNameBackup.bak' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup1.log' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup2.log' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup3.log' WITH NORECOVERY;
... 4 5 6 7 and so on
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackupX.log' WITH RECOVERY;

那里...您已经非常轻松地恢复了日志文件。您甚至可以使用 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:

RESTORE DATABASE DBName FROM DISK = 'C:\path\DBNameBackup.bak';

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:

RESTORE DATABASE DBName FROM DISK = 'C:\path\DBNameBackup.bak' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup1.log' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup2.log' WITH NORECOVERY;
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackup3.log' WITH NORECOVERY;
... 4 5 6 7 and so on
RESTORE LOG DBName FROM DISK = 'C:\path\DBNameBackupX.log' WITH RECOVERY;

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 statement WITH NORECOVERY then you just issue RESTORE DATABASE DBName WITH RECOVERY; to perform the final steps to make the database usable (rolling back uncommitted transactions, etc.).

寄与心 2024-09-24 19:36:49

您可以使用此脚本终止使用数据库的所有进程,然后尝试再次恢复它:

declare @sql as varchar(20), @spid as int
select @spid = min(spid)  from master..sysprocesses  where dbid = db_id('<database_name>') 
and spid != @@spid    

while (@spid is not null)
begin
    print 'Killing process ' + cast(@spid as varchar) + ' ...'
    set @sql = 'kill ' + cast(@spid as varchar)
    exec (@sql)

    select 
        @spid = min(spid)  
    from 
        master..sysprocesses  
    where 
        dbid = db_id('<database_name>') 
        and spid != @@spid
end 

print 'Process completed...'

You can use this script to kill all processes using the database and then try to restore it again:

declare @sql as varchar(20), @spid as int
select @spid = min(spid)  from master..sysprocesses  where dbid = db_id('<database_name>') 
and spid != @@spid    

while (@spid is not null)
begin
    print 'Killing process ' + cast(@spid as varchar) + ' ...'
    set @sql = 'kill ' + cast(@spid as varchar)
    exec (@sql)

    select 
        @spid = min(spid)  
    from 
        master..sysprocesses  
    where 
        dbid = db_id('<database_name>') 
        and spid != @@spid
end 

print 'Process completed...'
执笔绘流年 2024-09-24 19:36:49
  • 转到左侧列表中“常规”下方的“选项”项。
  • 确保选中“覆盖现有数据库”(“恢复选项”部分)。

祝你好运。

  • Go to "Options" item just under "General" on the left hand side list.
  • Make sure that "Overwrite the existing database" is checked ("Restore Options" section).

Good luck.

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