page contents

About the Post

Author Information

How to: Move SQL Databases (System)

This is the second article about moving system databases and I just got motivated writing it after reading Erik Darling‘s article Moving Databases with ALTER DATABASE. I wrote my rough draft in an earlier article (that I ended up putting up on my site) on how to move system databases in SQL Server 2008R2. In this feature, I will talk about moving system databases in versions (SQL Server 2008 and later) since they are using the same manner in currently supported SQL versions today,

As you know, things change from time to time and that is especially true with database administration. The data in the database is always needed, but the resources where this data resides may change. For example, a LUN might not be able to expand to present more capacity, a server might be old and needs to be retired or other reasons along this line of thought. The end result is that these data files will need to migrate to a different storage device to allow the system to keep presenting the information. Which leads to the question of how do you perform this task?

I attempted to explain this in earlier articles using the correct method (which pointed to a script you could download from David Levy Blog | Twitter) and an older line of thought that is not recommended. In that earlier article, I was talking about moving databases by detaching / reattaching databases, but that is just too risky as it could lead to data corruption. There might be queries still in the transaction log that have yet to be processed or an active running query that is still running. So forcibly taking a database offline by using the detach approach is not the right line of attack.

The proper way to proceed is to use the ALTER DATABASE method and that is the road I am going down. You can also read the MSDN article called Move System Databases for more information.

As usual, before you begin any modifications with SQL Server, make sure you have adequate backups, in case something goes wrong. You will need to also verify that the backup you took is working by performing a test. Restore it to another instance and then query it to confirm the results are accurate. No one will take responsibility for what happens to your data files as that burden lies on your shoulders.

In this post, I will start with moving the system database.

As you saw in the previous article, I have configured my SQL instance to save my MDF data files to my H drive and my transaction logs to my O drive:


However, when I run this query to check my system database names and locations:

SELECT name, physical_name AS CurrentLocation, state_desc

FROM
sys.master_files

WHERE database_id in
(DB_ID(N'master'),DB_ID(N'model'),DB_ID(N'msdb'));

GO

I see that they got moved to the C Drive due to the nature of the SQL GUI installer:


Which now means that I need to move them to the proper locations, which in my case is the MDF data files to my H drive and my transaction logs to my O drive. To move a system database data or log file as part of a planned relocation or scheduled maintenance operation, follow these steps. This procedure applies to all system databases except the master and Resource databases.

–SYNTAX: ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = ‘new_path\os_file_name’ )

ALTER
DATABASE msdb MODIFY
FILE ( NAME = MSDBData , FILENAME
=
'H:\Program Files\MSSQL10_50.MSSQLSERVER\MSSQL\Data\MSDBData.mdf')

ALTER
DATABASE msdb MODIFY
FILE ( NAME = MSDBLog , FILENAME
=
'O:\Program Files\MSSQL10_50.MSSQLSERVER\MSSQL\TLogs\MSDBLog.ldf')

ALTER
DATABASE model MODIFY
FILE ( NAME = modeldev , FILENAME
=
'H:\Program Files\MSSQL10_50.MSSQLSERVER\MSSQL\Data\model.mdf')

ALTER
DATABASE model MODIFY
FILE ( NAME = modellog , FILENAME
=
'O:\Program Files\MSSQL10_50.MSSQLSERVER\MSSQL\TLogs\modellog.ldf')

Note: My TempDB is properly configured, so it will be ignored, but it is the same steps as the ones I am doing for both MSDB and Model.


After you run that command, you will see that the files got moved, when I run this query to check my system database names and locations:

SELECT name, physical_name AS CurrentLocation, state_desc

FROM
sys.master_files

WHERE database_id in
(DB_ID(N'master'),DB_ID(N'model'),DB_ID(N'msdb'));

GO

With the exception of the Master DB, you can see that they are pointed to my current drives.


To change the Master DB, you will need to stop the SQL server.


Then you will get a notice that this action will also stop the SQL Server Agent


Now, you will need to right click on the SQL Service for the instance (in my case it is the default MSSQLSERVER) and select properties, then the Advanced tab and click the arrow next to the Startup Parameters


Note: the –“d option is for the mdf data files and the –“l is for the transaction logs

In SQL Server 2012, it looks a little different for the menu (and is easier to modify)


Here is the view in SQL Server 2014


Now, after you will need to paste or type in the new locations


SQL Server 2012 View


SQL Server 2014 View


Then you will get a notice that the changes do not take effect until you restart the service


Now you need to copy the files either with a script (preferably one with a progress bar) or using Windows Explorer.


You will need to start service.


Then you can check to see if all is well (in addition to seeing the databases come online)


Running the query (plus seeing the service start properly) lets you know that you are configured properly

SELECT name, physical_name AS CurrentLocation, state_desc

FROM
sys.master_files

WHERE database_id in
(DB_ID(N'master'),DB_ID(N'model'),DB_ID(N'msdb'));

GO

Now your system databases are moved to the proper location and as usual, I hope this helps you!

Tags: , , , , , , , , , , , ,

Trackbacks/Pingbacks

  1. How to: Move SQL Databases (User) | Scott Mattie's Blog - April 4, 2016

    […] next article will cover moving system databases (Master, Model, MSDB and TempDB) for SQL Server 2008R2, 2012, and […]

  2. HOW TO: ROBOCOPY Move SQL Files | Scott Mattie's Blog - May 15, 2016

    […] an earlier article, I talked about moving the system databases from the default location of your C drive to a separate […]

Leave a Reply

Copy Protected by Chetan's WP-Copyprotect.