How to fix Error 3161 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 3161 – “The Primary File Is Unavailable”

Why This Error Happens

This error occurs when SQL Server cannot locate, access, or read the primary data file (.mdf) during a RESTORE operation, typically because the file path specified in the backup header no longer matches the actual file system structure on the target server. It can also result from insufficient NTFS permissions for the SQL Server service account, the file being locked by another process, or underlying disk/storage corruption. In some cases, this happens when restoring a database backup across different server environments (e.g., migrating from an on-premises instance to Windows Server 2025) where default data paths differ.

Step-by-Step Resolution Guide

Step 1: Verify the Original File Paths in the Backup

  • Open SQL Server Management Studio (SSMS) and connect to your SQL Server 2025 instance.
  • Run the following query to inspect the logical and physical file paths stored in the backup set:
  RESTORE FILELISTONLY FROM DISK = 'C:\Backups\YourDatabase.bak';
  • Compare the PhysicalName column output against the actual folder structure on your Windows Server 2025/Windows 11 machine.

Step 2: Use WITH MOVE to Redirect File Paths

  • If the original paths don’t exist on the new server, explicitly redirect them during restore.
  • Execute a corrected restore command:
  RESTORE DATABASE YourDatabase
  FROM DISK = 'C:\Backups\YourDatabase.bak'
  WITH MOVE 'YourDatabase_Data' TO 'D:\SQLData\YourDatabase.mdf',
  MOVE 'YourDatabase_Log' TO 'D:\SQLLogs\YourDatabase_log.ldf',
  REPLACE;
  • Ensure the destination folders (e.g., D:\SQLData\ and D:\SQLLogs\) already exist before running this command.

Step 3: Check NTFS Permissions on the Target Directory

  • Right-click the target folder (e.g., D:\SQLData\) in File Explorer and select Properties > Security.
  • Confirm that the SQL Server service account (commonly NT Service\MSSQLSERVER or a custom domain service account) has Full Control permissions.
  • If permissions are missing, click Edit, add the service account, and grant Full Control, then click Apply.

Step 4: Confirm the File Isn’t Locked or In Use

  • Use Resource Monitor (resmon.exe) or Process Explorer to check if another process has a lock on the .mdf file.
  • Stop any conflicting services (such as antivirus real-time scanning or backup agents) temporarily and retry the restore operation.
  • Restart the SQL Server service if necessary via services.msc or by running:
  Restart-Service -Name 'MSSQLSERVER'

Step 5: Run a Disk Integrity Check

  • If the file paths and permissions are correct but the error persists, the underlying disk may have corruption issues.
  • Run chkdsk D: /f /r from an elevated Command Prompt and reboot the server to repair file system errors before attempting the restore again.

Alternative or Verification Steps

  • After completing the restore, run sp_helpdb YourDatabase or query sys.master_files to confirm the database status shows ONLINE and all file paths are correctly mapped.
  • If the issue persists, restore the backup to a different drive entirely (e.g., a secondary SSD) to rule out storage-specific corruption, then use DBCC CHECKDB to validate database integrity once restored.