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

Troubleshooting Guide: Error 5130 – Log Cannot Be Rebuilt When Database Mirroring Is Enabled

Why This Error Happens

Error 5130 occurs because SQL Server will not allow a transaction log rebuild on a database that is currently configured for database mirroring, as rebuilding the log would break the Log Sequence Number (LSN) chain required for the mirror to stay in sync with the principal. This typically happens when an administrator attempts to run ALTER DATABASE ... REBUILD LOG or use DBCC log rebuild operations while mirroring endpoints are still active. The operation is blocked by design to prevent data divergence between the principal and mirror databases, rather than being caused by a file corruption or permissions issue.

Step-by-Step Resolution Guide

Step 1: Verify the Current Mirroring State

  • Open SQL Server Management Studio (SSMS) and connect to the instance running on Windows Server 2025 / Windows 11.
  • Run the following query to check mirroring status:
  SELECT name, mirroring_state_desc FROM sys.database_mirroring WHERE mirroring_state_desc IS NOT NULL;
  • Confirm the database in question shows a state such as SYNCHRONIZED or SUSPENDED rather than NULL (not mirrored).

Step 2: Remove Database Mirroring Before Rebuilding the Log

  • Break the mirroring session using:
  ALTER DATABASE [YourDatabaseName] SET PARTNER OFF;
  • Confirm mirroring has been fully removed by re-running the query from Step 1 and checking that mirroring_state_desc returns NULL.
  • Ensure the mirroring endpoints are stopped via sys.database_mirroring_endpoints if the partner relationship persists.

Step 3: Rebuild the Transaction Log

  • With mirroring disabled, set the database into emergency mode if needed:
  ALTER DATABASE [YourDatabaseName] SET EMERGENCY;
  • Run the rebuild command:
  ALTER DATABASE [YourDatabaseName] REBUILD LOG ON (NAME=YourDatabaseName_log, FILENAME='C:\SQLData\YourDatabaseName_log.ldf');
  • Set the database back to multi-user mode once the rebuild completes:
  ALTER DATABASE [YourDatabaseName] SET MULTI_USER;

Step 4: Re-Establish Database Mirroring

  • After the log rebuild is confirmed successful, re-configure mirroring from the principal to the mirror server using SSMS or T-SQL ALTER DATABASE ... SET PARTNER = 'TCP://MirrorServer:5022'.
  • Re-synchronize the databases by taking a fresh full backup and log backup, then restoring them with NORECOVERY on the mirror instance before re-enabling the partnership.

Alternative or Verification Steps

  • Verify success by running DBCC CHECKDB on the rebuilt database and confirming no consistency errors, then check sys.database_mirroring to confirm the mirroring state returns to SYNCHRONIZED.
  • If mirroring cannot be cleanly removed due to network or endpoint issues, consider migrating to Always On Availability Groups as a modern alternative, since legacy database mirroring is deprecated in SQL Server 2025.