Troubleshooting Guide: SQL Server Error 5559 – “Could Not Open the Database”
Why This Error Happens
Error 5559 typically occurs when SQL Server 2025 cannot access a database file due to the file being in an inconsistent state, often caused by an improperly completed FILESTREAM or memory-optimized filegroup operation. This can also result from file system permission restrictions on the database container directory, or because the database was left in a transitional state after a failed upgrade or restore operation. In some cases, Windows Server 2025 security policies or antivirus/EDR software may lock the underlying container files, preventing SQL Server’s Database Engine from mounting them correctly.
Step-by-Step Resolution Guide
Step 1: Verify Database and Filegroup Container Status
- Open SQL Server Management Studio (SSMS) and run
SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabaseName';to check if the database is marked as SUSPECT, RECOVERY_PENDING, or OFFLINE. - Cross-reference with
SELECT * FROM sys.database_files;to confirm the physical file paths (especially for memory-optimized FILESTREAM containers) still exist on disk. - Check the SQL Server Error Log located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor accompanying messages referencing the specific container GUID folder.
Step 2: Correct File System Permissions and Unlock Containers
- Navigate to the database’s root folder (commonly
D:\SQLData\YourDatabase\or wherever FILESTREAM filegroups reside). - Right-click the folder, select Properties > Security, and ensure the SQL Server service account (e.g.,
NT Service\MSSQLSERVER) has Full Control. - Temporarily disable Windows Defender real-time scanning or add an exclusion for the SQL data directory via
Windows Security > Virus & threat protection > Manage settings > Add exclusion. - Restart the SQL Server service using
net stop MSSQLSERVERfollowed bynet start MSSQLSERVERin an elevated PowerShell terminal.
Step 3: Run Consistency Checks and Repair
- Execute
DBCC CHECKDB ('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS;to identify corruption in the container structure. - If corruption is confirmed and no clean backup is available, set the database to emergency mode using
ALTER DATABASE YourDatabaseName SET EMERGENCY;followed byALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;then runDBCC CHECKDB ('YourDatabaseName', REPAIR_ALLOW_DATA_LOSS);. - Set the database back to multi-user mode once repairs are complete:
ALTER DATABASE YourDatabaseName SET MULTI_USER;.
Step 4: Restore From Backup (If Repair Fails)
- If DBCC repair does not resolve the container issue, restore the database from the most recent full backup using
RESTORE DATABASE YourDatabaseName FROM DISK = 'E:\Backups\YourDatabase.bak' WITH REPLACE;. - Apply any subsequent differential or transaction log backups to minimize data loss.
Alternative or Verification Steps
- After applying fixes, confirm the database is accessible by running
SELECT state_desc FROM sys.databases WHERE name = 'YourDatabaseName';and verifying it returns ONLINE. - If the issue persists, use the SQL Server Configuration Manager to confirm the service is running under the correct account with network access to the storage volume, and check Windows Event Viewer (
Windows Logs > Application) for disk I/O errors (Event ID 9 or 153) that may indicate underlying storage hardware failure.