【发布时间】:2015-01-08 16:01:53
【问题描述】:
环境:
- SQL Server 2008 R2
- 数据库未接收到活动事务。
在生产环境中,我需要将 MDF 和 LDF 文件移动到新驱动器。由于我有一个时间窗口来停止活动事务,我想我可以只备份数据库,然后在将文件组配置到新位置时恢复它。
我认为这比使用新文件名分离和重新附加数据库要好得多。
由于我是新手,想在这里咨询专家。非常感谢任何建议/建议。
【问题讨论】:
标签: sql-server sql-server-2008-r2
环境:
在生产环境中,我需要将 MDF 和 LDF 文件移动到新驱动器。由于我有一个时间窗口来停止活动事务,我想我可以只备份数据库,然后在将文件组配置到新位置时恢复它。
我认为这比使用新文件名分离和重新附加数据库要好得多。
由于我是新手,想在这里咨询专家。非常感谢任何建议/建议。
【问题讨论】:
标签: sql-server sql-server-2008-r2
这是一个简单的例子。它假定您的数据库有一个.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
【讨论】: