Troubleshooting Guide: SQL Server Error 5003 – Database Mirroring Cannot Be Enabled with Offline Files
Why This Error Happens
Error 5003 occurs when SQL Server detects that one or more files belonging to the database are in an OFFLINE state, which prevents the database from meeting the prerequisites for mirroring setup. Database mirroring requires every data and log file associated with the database to be fully online and accessible, since the mirroring process needs to read the complete file structure to establish the initial synchronization with the mirror instance. This typically happens after a restore operation leaves secondary filegroups offline, a disk/storage failure takes a file offline, or a file was manually set to OFFLINE and never brought back online.
Step-by-Step Resolution Guide
Step 1: Identify the Offline Files
- Run the following query to check file states for the target database:
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID('YourDatabaseName');
- Look for any rows where
state_descreturnsOFFLINEorRESTORINGinstead ofONLINE. - Note the logical file name and physical path for each offline file.
Step 2: Verify Underlying Storage and File Accessibility
- Confirm the physical disk or volume hosting the file (e.g.,
D:\SQLData\YourDB_Secondary.ndf) is mounted and accessible in Windows Server 2025 / Windows 11. - Check Windows Disk Management (
diskmgmt.msc) to ensure the volume isn’t offline or in a degraded state. - If the file was moved or deleted, restore it from the last known good backup to its original path.
Step 3: Bring the File Back Online
- Execute the following T-SQL command to attempt to bring the file online:
ALTER DATABASE YourDatabaseName
MODIFY FILE (NAME = YourLogicalFileName, OFFLINE = OFF);
- If the file was offline due to corruption, instead run a targeted file restore:
RESTORE DATABASE YourDatabaseName
FILE = 'YourLogicalFileName'
FROM DISK = 'C:\Backups\YourDB.bak'
WITH NORECOVERY;
- Follow up with a transaction log restore and
WITH RECOVERYto finalize the file state.
Step 4: Re-Attempt Database Mirroring Setup
- Open SQL Server Management Studio (SSMS) and navigate to Database Properties > Mirroring.
- Alternatively, re-run the mirroring T-SQL command:
ALTER DATABASE YourDatabaseName SET PARTNER = 'TCP://MirrorServer:5022';
- Confirm no error 5003 is thrown during the configuration process.
Alternative or Verification Steps
- Re-run the
sys.master_filesquery from Step 1 to confirm all files now showstate_desc = ONLINEbefore retrying mirroring. - If files cannot be recovered due to permanent storage loss, consider removing the affected secondary filegroup (if non-critical) or restoring the entire database from a full backup to a healthy storage location before re-enabling mirroring.