Have you ever encountered a problem with having low disk space on the drive where SQL Server database files are stored whether it’s your development environment or the production environment? In Dev. environment you still can re-create, restore from the backup file, and set it to another location.
By default, the SQL Server stores database files in its installation (Data) folder:
But what if you are facing a low disk space issue in your production environment, there should be a way to move database files (MDF and LDF) to another drive (with sufficient disk space) while the actual database will still be hosted by the same SQL Server instance. We will see how to move the database files to another location. In this example we will use a sample AdventureWorks database.
Pre-requisites
If a database is being used by any Windows services or other resources, these must be stopped to allow altering SQL database files. Also, any existing connections to a database must be closed. Before the first step, make sure to locate the appropriate MDF and LDF files for a database you want to work with. By default, these names are in the following format:
- Databasename_Data.mdf – for MDF file
- Databasename_log.ldf – for LDF file
The above mentioned format does not need to be necessarily used, so make sure you are targeting correct files.
Moving database files to another location
- Run the following SQL script to set a new location for SQL database files:
ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Data, FILENAME = 'D:\NEW_LOCATION\AdventureWorks2012_Data.mdf'); GO ALTER DATABASE AdventureWorks2012 MODIFY FILE ( NAME = AdventureWorks2012_Log, FILENAME = 'D:\NEW_LOCATION\AdventureWorks2012_Log.ldf'); GO
The NEW_LOCATION is a folder created on a separate drive as we will change from a default C to D drive on a local machine which is having sufficient disk space for SQL database files. A specified folder must be created first, to be used as a new location for SQL database files in the above SQL statement
- Run the following SQL script to take a SQL database offline:
ALTER DATABASE AdventureWorks2012 SET OFFLINE; GO
This is important to perform the next step. If a database is being used by any application, this step cannot be accomplished, unless all connections to a database are closed.
- Move MDF and LDF files of the specific SQL database to a new location specified in the statement above. This means simply cutting mentioned files from the existing location and moving them to a newly specified one.
Important note: Make sure that SQL Server can access the specified location. Otherwise, the following error will appears:
Msg 5120, Level 16, State 101, Line 15 Unable to open the physical file “D:\NEW_LOCATION\AdventureWorks2012_Data.mdf”. Operating system error 5: “5(Access is denied.)”.
To fix the issue:
- Start SQL Server Configuration Manager
- Right-click a SQL Server instance that hosts a database in which files are moved to a new location and choose the Properties option from the drop-down list:
Instead of the current account, switch to the one that has access to a drive where files are moved:
Once this is done, a database can be set online by running the following query to get back a database online:
ALTER DATABASE AdventureWorks2012 SET ONLINE; GO
To verify that the process is finished successfully run the following query:
SELECT name, physical_name AS NewLocation, state_desc AS OnlineStatus FROM sys.master_files WHERE database_id = DB_ID(N'AdventureWorks2012'); GO
This should give the following result:
Once this is done, a SQL database will be hosted on a drive with sufficient free space and the user can continue using it.