Lollipop   

Welcome to PcCare.com

SQL Server 2008 moving database directories


Moving tables from one location to another is usually easy, just detach, move file, reattach from new location. System databases will not permit movement and require the following steps.

Start SQL Server configuration manager

Select database->Right Click->Start->Right Click->Properties

Select tab Advanced

Select startup parameters

SQL Server configuration manager properties

Select Startup Parameters

Change startup parameters to reflect your database's destination directory (Destination database directory shown in red below)

-dC:\myprojects\SqlDb\uAttack\Data\master.mdf;-eC:\myprojects\SqlDb\uAttack\Log\ERRORLOG;-lC:\myprojects\SqlDb\uAttack\Data\mastlog.ldf

SQL Server configuration manager->Select database->right click->Stop

Copy all database files from original location to new location->typically it includes all files under MSSQL\Data directory. Note, at this point sql server has not been configured to utilize anthing other than the master database.

Here's what the destination directory looked after the files were moved: dir /b "C:\myprojects\SqlDb\uAttack\Data"

master.mdf
mastlog.ldf
model.mdf
modellog.ldf
MSDBData.mdf
MSDBLog.ldf
MS_AgentSigningCertificate.cer
SQL2008_500457_uattack.LDF
SQL2008_500457_uattack.mdf
tempdb.mdf
templog.ldf


The above change to the startup parameters also changed the destination directory for log files, create that directory in you new folder. Only perform this step to move log directory.

mkdir C:\myprojects\SqlDb\uAttack\LOG

SQL Server configuration manager->Select Database->right click->Start

The start should execute without any errors. Now stop it to configure the other sql server files.

SQL Server configuration manager->Select Database->right click->Stop

Determine your sql servers service startup name

Start->Computer->Manage

Open Services->Right click sql server instance->Properties

SQL Server Services properties

Record the Service Name parameter for later steps. It may contain an MSSQL$ prefix, you'll need this.

Verify you'v Logged into windows using an administrator account. If possible, pick an account which also has administrative privledges on sql server. Otherwise slqcmd utility commands used below require additional login parameters.

Start a command prompt

Start->Accessories->Command Prompt

Start sql server from a command prompt using the name you recorded above. You must include the /f and /T3608 command line options

NET START MSSQLSERVER /f /T3608

Start up sql servers command line utility

sqlcmd

To display all the current database locations, note, GO on the second line is necessary

SELECT name, physical_name FROM sys.master_files
Go

A list of database names and their file locations are listed. For any incorrect database file location, enter the following command. Note, the master.mdf and mastlog.ldf should already be correct from steps above.

update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\tempdb.mdf' where name = 'tempdev'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\templog.ldf' where name = 'templog'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\model.mdf' where name = 'modeldev'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\modellog.ldf' where name = 'modellog'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\MSDBData.mdf' where name = 'MSDBData'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\MSDBLog.ldf' where name = 'MSDBLog'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\SQL2008_500457_uattack.mdf' where name = 'SQL2008_500457_uattack_data'
go
update sys.master_files set physical_name = 'C:\myprojects\SqlDb\uAttack\Data\SQL2008_500457_uattack.ldf' where name = 'SQL2008_500457_uattack_log'
go

NET STOP MSSQLSERVER

Exit the command prompt by typing: Exit

SQL Server configuration manager->Select Database->right click->start

DISCLAIMER: It is assumed that users are familiar with the operating system they are using and comfortable with making the suggested changes. PcCare.com will not be held responsible if changes you make cause a system failure.

Please review our Terms of Service and Privacy statement before initiating service or using this site. Microsoft® and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. PcCare Site Map. About Us

PcCare.com is owned and operated by TechnoChill Inc.