简体   繁体   English

SQL-Server:错误 - 无法获得独占访问,因为数据库正在使用中

[英]SQL-Server: Error - Exclusive access could not be obtained because the database is in use

I am actually trying to make a script (in Sql Server 2008) to restore one database from one backup file.我实际上是在尝试制作一个脚本(在 Sql Server 2008 中)从一个备份文件中恢复一个数据库。 I made the following code and I am getting an error -我编写了以下代码,但出现错误 -

Msg 3101, Level 16, State 1, Line 3
Exclusive access could not be obtained because 
the database is in use.
Msg 3013, Level 16, State 1, Line 3
RESTORE DATABASE is terminating abnormally.

How do I fix this problem?我该如何解决这个问题?

IF DB_ID('AdventureWorksDW') IS NOT NULL 
BEGIN 
RESTORE DATABASE [AdventureWorksDW] 
FILE = N'AdventureWorksDW_Data' 
FROM  
DISK = N'C:\Program Files\Microsoft SQL Server\
MSSQL10_50.SS2008\MSSQL\Backup\AdventureWorksDW.bak' 
WITH  FILE = 1, 
MOVE N'AdventureWorksDW_Data' 
TO N'C:\Program Files\Microsoft SQL Server\
MSSQL10_50.SS2008\MSSQL\DATA\AdventureWorksDW.mdf', 
MOVE N'AdventureWorksDW_Log'  
TO N'C:\Program Files\Microsoft SQL Server\
MSSQL10_50.SS2008\MSSQL\DATA\AdventureWorksDW_0.LDF', 
NOUNLOAD,  STATS = 10 
END
  1. Set the path to restore the file.设置恢复文件的路径。
  2. Click "Options" on the left hand side.单击左侧的“选项”。
  3. Uncheck "Take tail-log backup before restoring"取消选中“恢复前进行尾日志备份”
  4. Tick the check box - "Close existing connections to destination database".勾选复选框 - “关闭与目标数据库的现有连接”。 在此处输入图片说明
  5. Click OK.单击确定。

I'll assume that if you're restoring a db, you don't care about any existing transactions on that db.我假设如果您正在恢复一个数据库,您不关心该数据库上的任何现有事务。 Right?对? If so, this should work for you:如果是这样,这应该适合您:

USE master
GO

ALTER DATABASE AdventureWorksDW
SET SINGLE_USER
--This rolls back all uncommitted transactions in the db.
WITH ROLLBACK IMMEDIATE
GO

RESTORE DATABASE AdventureWorksDW
FROM ...
...
GO

Now, one additional item to be aware.现在,要注意另外一项。 After you set the db into single user mode, someone else may attempt to connect to the db.将数据库设置为单用户模式后,其他人可能会尝试连接到数据库。 If they succeed, you won't be able to proceed with your restore.如果它们成功,您将无法继续进行恢复。 It's a race!这是一场比赛! My suggestion is to run all three statements at once.我的建议是同时运行所有三个语句。

execute this query before restoring database:在恢复数据库之前执行此查询:

alter database [YourDBName] 
set offline with rollback immediate

and this one after restoring:恢复后的这个:

  alter database [YourDBName] 
  set online

For me, the solution is:对我来说,解决办法是:

  1. Check Overwrite the existing database(WITH REPLACE) in optoins tab at left hand side.在左侧的 optoins 选项卡中选中 Overwrite the existing database(WITH REPLACE)。

  2. Uncheck all other options.取消选中所有其他选项。

  3. Select source and destination database.选择源和目标数据库。

  4. Click ok.单击确定。

That's it.就是这样。

Use the following script to find and kill all the opened connections to the database before restoring database.在恢复数据库之前,使用以下脚本查找并终止所有打开的数据库连接。

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...'

Hope this will help...希望这会有所帮助...

我刚刚重新启动了 sqlexpress 服务,然后恢复正常

I think you just need to set the db to single user mode before attempting to restore, like below, just make sure you're using master我认为您只需要在尝试恢复之前将 db 设置为单用户模式,如下所示,只需确保您使用的是master

USE master
GO
ALTER DATABASE AdventureWorksDW
SET SINGLE_USER

taking original db to offline worked for me将原始数据库离线对我有用

下线

Use Master
alter database databasename set offline with rollback immediate;

--Do Actual Restore
RESTORE DATABASE databasename
FROM DISK = 'path of bak file'
WITH MOVE 'datafile_data' TO 'D:\newDATA\data.mdf',
MOVE 'logfile_Log' TO 'D:\newDATA\DATA_log.ldf',replace

alter database databasename set online with rollback immediate;
GO

解决方案 1:重新启动 SQL 服务并尝试恢复数据库 解决方案 2:重新启动系统/服务器并尝试恢复数据库 解决方案 3:收回当前数据库,删除当前/目标数据库并尝试恢复数据库。

Setting the DB to single-user mode didn't work for me, but taking it offline, and then bringing it back online did work.将数据库设置为单用户模式对我不起作用,但将其脱机,然后将其重新联机确实有效。 It's in the right-click menu of the DB, under Tasks.它位于数据库的右键单击菜单中的任务下。

Be sure to check the 'Drop All Active Connections' option in the dialog.请务必选中对话框中的“删除所有活动连接”选项。

I experienced this issue when trying to restore a database on MS SQL Server 2012.我在尝试恢复 MS SQL Server 2012 上的数据库时遇到了这个问题。

Here's what worked for me :这是对我有用的

I had to first run the RESTORE FILELISTONLY command below on the backup file to list the logical file names:我必须首先在备份文件上运行下面的RESTORE FILELISTONLY命令以列出逻辑文件名:

RESTORE FILELISTONLY 
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\my_db_backup.bak'

This displayed the LogicalName and the corresponding PhysicalName of the Data and Log files for the database respectively:这分别显示了数据库的数据和日志文件的LogicalName和相应的PhysicalName

LogicalName      PhysicalName               
com.my_db        C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db.mdf
com.my_db_log    C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db_log.ldf

All I had to do was to simply replace the LogicalName and the corresponding PhysicalName of the Data and Log files for the database respectively in my database restore script:我所要做的就是在我的数据库恢复脚本中分别替换数据库的LogicalName和相应的PhysicalName的数据和日志文件:

USE master;
GO

ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

    
RESTORE DATABASE my_db
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\my_db_backup.bak'
    WITH REPLACE,
    MOVE 'com.my_db' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db.mdf',
    MOVE 'com.my_db_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db_log.ldf'
GO
    
ALTER DATABASE my_db SET MULTI_USER;
GO

And the Database Restore task ran successfully:并且数据库还原任务成功运行:

That's all.就这样。

I hope this helps我希望这有帮助

Vinu M Shankar's solution worked for me. Vinu M Shankar 的解决方案对我有用。

But I had to check the: "Take tail-log backup before restoring" checkbox for mine to work.但是我必须检查: “在恢复之前进行尾日志备份”复选框才能使我的工作。

Here's a way I am doing database restore from production to development:这是我从生产到开发进行数据库恢复的一种方式:

NOTE: I am doing it via SSAS job to push production database to development daily:注意:我正在通过 SSAS 作业将生产数据库推送到每天的开发中:

Step1: Delete previous day backup in development: Step1:删除开发中的前一天备份:

declare @sql varchar(1024);

set @sql = 'DEL C:\ProdAEandAEXdataBACKUP\AE11.bak'
exec master..xp_cmdshell @sql

Step2: Copy production database to development: Step2:复制生产数据库到开发:

declare @cmdstring varchar(1000)
set @cmdstring = 'copy \\Share\SQLDBBackup\AE11.bak C:\ProdAEandAEXdataBACKUP'
exec master..xp_cmdshell @cmdstring 

Step3: Restore by running .sql script步骤 3:通过运行 .sql 脚本恢复

SQLCMD -E -S dev-erpdata1 -b -i "C:\ProdAEandAEXdataBACKUP\AE11_Restore.sql"

Code that is within AE11_Restore.sql file: AE11_Restore.sql 文件中的代码:

RESTORE DATABASE AE11
FROM DISK = N'C:\ProdAEandAEXdataBACKUP\AE11.bak'
WITH MOVE 'AE11' TO 'E:\SQL_DATA\AE11.mdf',
MOVE 'AE11_log' TO 'D:\SQL_LOGS\AE11.ldf',
RECOVERY;

I got this error when there was not enough disk space to restore Db.当没有足够的磁盘空间来恢复 Db 时,我收到了这个错误。 Cleaning some space solved it.清理一些空间解决了它。

I got this error when unbeknownst to me, someone else was connected to the database in another SSMS session.我在不知情的情况下收到此错误,其他人在另一个 SSMS 会话中连接到数据库。 After I signed them out the restore completed successfully.在我将它们注销后,恢复成功完成。

In my case although i tried all of the above solutions, the thing that worked, was to restart SSMS.就我而言,尽管我尝试了上述所有解决方案,但有效的是重新启动 SSMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM