2015-01-08 220 views
0

環境:移動SQL數據庫文件MDF和LDF到新位置

  • SQL Server 2008 R2的
  • 數據庫沒有收到交易活躍。

在生產環境中,我需要將MDF & LDF文件移動到新驅動器。由於我有一段時間來停止活動事務,因此我認爲我可以對數據庫進行備份,然後在將文件組配置到新位置時進行恢復。

我覺得這比用新文件名分離和重新附加數據庫要好得多。

由於我是新手,想與這裏的專家覈對一下。任何建議/建議非常感謝。

+0

[移動一個SQL Server數據庫到新的服務器]的可能重複(http://stackoverflow.com/questions/4522983/moving-a-sql-server-database-to-a-new-server ) – Tanner

回答

1

下面是一個簡單的例子。它假定您的數據庫有一個單獨的.mdf(數據)文件和一個.ldf(日誌)文件。我將以[model]數據庫爲例。

--First, make note of the current location of the db files. 
--Copy and paste the physical_names somewhere. Trust me, if you forget 
--where the files were originally, this will save you some heartache. 
SELECT d.name, f.name, f.physical_name 
FROM master.sys.master_files f 
JOIN master.sys.databases d 
    ON d.database_id = f.database_id 
WHERE d.name = 'model' --Replace with the name of your db. 

--Now set the new file paths. 
--You can run the ALTER DATABASE statements while the db is online. 

--Run once for the mdf/data file. 
ALTER DATABASE [model] --Replace with the name of your db. 
MODIFY FILE 
(
    NAME = 'modeldev', --this is the "logical" file name. 
    FILENAME = 'D:\SqlData\model.mdf' --Replace with the new path\filename. 
) 

--Run once for the ldf/data file. 
ALTER DATABASE [model] --Replace with the name of your db. 
MODIFY FILE 
(
    NAME = 'modellog', 
    FILENAME = 'D:\SqlData\modellog.ldf' --Replace with the new path\filename. 
) 


--When business rules allow, take the db OFFLINE. 
ALTER DATABASE [model] --Replace with the name of your db. 
SET OFFLINE 

--Move the physical db files to the new location on disk. 

--Bring the db back ONLINE to complete the task. 
ALTER DATABASE [model] --Replace with the name of your db. 
SET ONLINE 
+0

當所有事件都發生在同一個SQL Server實例上時,我更喜歡這種方法通過備份/恢復。涉及的I/O應該較少。 detach/reattach方法應該也一樣,儘管我認爲MS說這個方法將被棄用... – DMason

相關問題