How to move database files of a Mirrored SQL Server Database
By: Manvendra Singh | Updated: 2012-05-30 | Comments (14) | Related: 1 | 2 | 3 | More > Database Mirroring
As you may know, you cannot detach a mirrored database or bring it offline to move a data or log file from one drive to another drive. Moving a database file for a mirrored database is not the same as moving a normal database. Here I will show you the step by step process on how to move the data and/or log file(s) from one drive to another drive with minimum downtime.
Moving database files can be done two ways; by detaching the database then moving the database file(s) to the target location and then attaching the database from the new location. The other option is to run an ALTER statement to change the file location in the system catalog view, bring the database offline, then copy the file(s) to the target location and bring the database online. With database mirroring enabled for the database, both options will fail because your database is mirrored. We can't detach the mirrored database, nor can we bring it OFFLINE.
Here is step by step solution to reduce your downtime and move your database file from one location to another location for a mirrored database.
Check the database files location for all database files. Here we are going to move database "NASSP2".
Here we can see two database files placed on the C: drive. As per best practice, we should not place database files on the system C: drive, so this is what we are going to move.
Check the database mirroring configuration for your database. Run the below script to check the database mirroring status and its current partner name.
SELECT (SELECT DB_NAME(7)AS DBName), database_id, mirroring_state_desc, mirroring_role_desc, mirroring_partner_name, mirroring_partner_instance FROM sys.database_mirroring WHERE database_id=7
We can see the mirroring_role_desc for this server is principal and its partner/mirrored instance name.
If the database file size is big it will take some time to copy from one drive to another drive. So to over come this issue and minimize the downtime, we will failover our database from our principal server to its MIRROR server and route our application to the new principal server (earlier mirrored box) to bring the application online and run business as usual. Run the below command to failover this database.
ALTER DATABASE NASSP2 SET PARTNER FAILOVER
Now we can again check the database mirroring configuration to see the current mirroring state. Run the same script which we ran in step 2. This time the output is the same, except one column. Here mirroring_state_desc is MIRROR where earlier it was principal.
Now our principal instance has become mirrored. Ask your application team to change the ODBC configurations and route the application connection to the new principal server. Now we can do make changes without downtime being of any concern. Note that if you have databases in a shared environment, then you may need to failover all databases to the mirrored server to reduce any downtime. The technique requires stopping the database services, so this could impact other databases on this server.
As we saw in step 1, two database files are on the C: drive. Now we have to move these two database files from 'C:' to 'E\MSSQL2008\DATA' drive. First we need to run an ALTER DATABASE statement to change the file location in master database system catalog view. Make sure to run this ALTER statement for every file that needs to be moved to the new location.
ALTER DATABASE NASSP2 MODIFY FILE (NAME='NASSP2_System_Data', FILENAME='E:\MSSQL2008\DATA\nassp2_system_data.mdf') go ALTER DATABASE NASSP2 MODIFY FILE (NAME='NASSP2_log', FILENAME='E:\MSSQL2008\DATA\nassp2_log.ldf') go
Now, stop the SQL Server instance to copy the data and log file(s) to the target location. I used PowerShell to stop and start the services. You can use services.msc utility or SQL Server Configuration Manager as well.
STOP-SERVICE MSSQLSERVER -FORCE
Check the status of SQL Server service.
Now copy both database files (nassp2_system_data.mdf and nassp2_log.ldf) to the new target location ('C' to 'E:\MSSQL2008\DATA').
Once both files has been copied, start the SQL Server services.
Check the status of SQL Server service
Once SQL Server has started, failback your database from current principal to your primary box. Run step 1 again to check the location of the files and run step 2 again to check the mirroring status.
- Learn more database mirroring Database Mirroring Articles
About the author
View all my tips
Article Last Updated: 2012-05-30