Sql-Server-2014

移動日誌數據庫文件

  • July 22, 2021

我想將一個 ldf 文件從一個驅動器移動到另一個驅動器。某些絕對不是我的白痴將日誌文件恢復到了現在幾乎已滿的錯誤驅動器。

對於 logship 環境,數據庫是只讀/備用的。Alter database .. modify file不起作用,因為數據庫是只讀的。我無法分離/附加,因為我無法使用create .. for attach.

如果我使用更改數據庫:

Msg 5004, Level 16, State 4, Line 9
To use ALTER DATABASE, the database must be in a writable state in which a checkpoint can be executed.

如果我嘗試附加:

Msg 1824, Level 16, State 1, Line 11
Cannot attach a database that was being restored.

我做的小測試案例:

-- create ro/standby db
restore database test 
   from disk = 'D:\BakMcBakface.bak'
   with 
   move 'data' to 'D:\test.mdf',
   move 'log' to 'D:\test.ldf',
   standby='D:\test.standby'

-- try alter
ALTER DATABASE test MODIFY FILE ( NAME = 'log', FILENAME = 'd:\test2.ldf' );

-- try detach/attach db
EXEC master.dbo.sp_detach_db @dbname = N'test'

-- attach db
CREATE DATABASE [test] 
ON 
   (filename=N'D:\test.mdf'),
   (filename=N'D:\test2.ldf')
   for attach
   -- with standby='D:\test.standby'

作為最後的手段,我可​​以進行完全恢復,但這是一個多 TB 數據庫,我盡量避免這種情況。如果我可以在待機模式下連接,我就完成了。我也可以直接修改 sys 表,但我寧願不是 Cthulhu 回歸的原因。

而不是分離/附加,使數據庫離線,更改位置,移動文件,然後重新聯機。範例 DDL:

--take database offline
ALTER DATABASE test SET OFFLINE;

--modify the file location
ALTER DATABASE test MODIFY FILE ( NAME = 'log', FILENAME = 'd:\test2.ldf' );

--manually move the file to the new location

--finally, set database online
ALTER DATABASE test SET ONLINE;

引用自:https://dba.stackexchange.com/questions/296001