I'm not looking to relocate the database to another server entirely, but just move the data file(s) and log file to another drive with more space. I've seen conflicting directions on how to do this, so I'm looking for the recommended proper way of doing it.
views:
411answers:
3Detaching and reattaching in the new location is usually the best way to go
Anthony
2009-07-13 14:46:02
Very nice. I found another MS article, but it was pre-2005. This link had all the information I needed and it worked. Thank you!
Kilhoffer
2009-07-13 15:19:52
+1
A:
To be absolutely safe, I would do the following:
- Backup the database to a BAK file.
- Take the current database offline, or delete it if you want to.
- Restore the database and change the location of the MDF and LDF files.
Scripts sample:
-- Get the file list from a backup file.
-- This will show you current logical names and paths in the BAK file
RESTORE FILELISTONLY FROM disk = N'C:\Backups\MyDatabaseName.bak'
-- Perform the restore of the database from the backup file.
-- Replace 'move' names (MDFLogicalName, LDFLogicalName) with those found in
-- the previous filelistonly command
restore database MyDatabaseName
from disk = N'C:\Backups\MyDatabaseName.bak'
with move 'MDFLogicalName' to 'D:\SQLData\MyDatabaseName.mdf',
move 'LDFLogicalName' to 'D:\SQLLogs\MyDatabaseName_log.ldf',
replace, stats=10;
Notes
The first script will get you the current names and paths that you'll need in the second script. The second script restores the database back to the name you want it to have, but you can change where you want to store. In the example above, it moves the MDF and LDF files to the D: drive.
Jay S
2009-07-13 14:43:47
One problem with this approach is that it requires much more time for the backup/restore. Restores will always take longer than copying the mdf/ldf and reattaching. It can become prohibitive with DBs of any significant size
Anthony
2009-07-13 14:47:42
Agreed, but whenever I start messing around with the databases, especially when there are so many developers or websites dependent on them, I like to go through backup files just in case something goes wrong and I need to get back to a good state.
Jay S
2009-07-13 15:06:13
+1
A:
Another way - detach database files (database->tasks->detach), move them to new drive and then attach again. But way described by Jay S is the simpliest.
Alex_L
2009-07-13 14:53:13