IT

백업을 복원 할 때 모든 활성 연결을 해제하려면 어떻게합니까?

lottoking 2020. 5. 29. 08:06
반응형

백업을 복원 할 때 모든 활성 연결을 해제하려면 어떻게합니까?


활성 연결로 인해 SQL Server 2005가 백업을 복원하지 않습니다. 어떻게 강제 할 수 있습니까?


SQL Server Management Studio 2005

당신이 바로 데이터베이스를 클릭 클릭하면 Tasks다음을 클릭 Detach Database, 그것은 활성 연결이있는 대화 상자가 나타납니다.

분리 화면

"메시지"아래의 하이퍼 링크를 클릭하면 활성 연결을 종료 할 수 있습니다.

그런 다음 데이터베이스를 분리하지 않고 해당 연결을 종료 할 수 있습니다.

자세한 내용은 여기를 참조 하십시오 .

SQL Server Management Studio 2008

SQL Server Management Studio 2008의 인터페이스가 변경되었습니다. 단계는 다음과 같습니다 ( Tim Leung 사용 ).

  1. 개체 탐색기에서 서버를 마우스 오른쪽 단추로 클릭하고 '활동 모니터'를 선택하십시오.
  2. 이것이 열리면 프로세스 그룹을 펼치십시오.
  3. 이제 드롭 다운을 사용하여 데이터베이스 이름별로 결과를 필터링하십시오.
  4. 마우스 오른쪽 단추로 'Kill Process'옵션을 선택하여 서버 연결을 종료하십시오.

db를 단일 사용자 모드로 설정하고 복원 한 다음 다시 다중 사용자로 설정하려고합니다.

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


이 코드는 저에게 효과적이며 데이터베이스의 기존 연결을 모두 종료합니다. 데이터베이스 이름을 갖도록 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

이 후 나는 그것을 복원 할 수 있었다


SQL Server를 다시 시작하면 사용자 연결이 끊어집니다. 내가 찾은 가장 쉬운 방법-서버를 오프라인으로 전환하려는 경우에도 좋습니다.

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.


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

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.


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.


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


위의 어느 것도 나를 위해 일하지 않았습니다. 내 데이터베이스에 Activity Monitor 또는 sp_who를 사용한 활성 연결이 표시되지 않았습니다. 나는 궁극적으로 :

  • 데이터베이스 노드를 마우스 오른쪽 단추로 클릭하십시오
  • "분리 ..."를 선택하십시오.
  • "연결 끊기"상자를 확인하십시오.
  • 다시 부착

가장 우아한 솔루션은 아니지만 작동하며 SQL Server를 다시 시작할 필요가 없습니다 (DB 서버가 다른 데이터베이스를 많이 호스팅했기 때문에 옵션이 아닙니다)


나는 이것을 좋아한다.

롤백 즉시 데이터베이스 세트를 오프라인으로 변경

데이터베이스를 복원하십시오. 그 후

롤백 즉시 데이터베이스 세트를 온라인으로 변경

참고 URL : https://stackoverflow.com/questions/1154200/when-restoring-a-backup-how-do-i-disconnect-all-active-connections

반응형