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

Troubleshooting Guide: SQL Server Error 13127 (Database Mirroring/Availability Group Principal Issue)

Why This Error Happens

Error 13127 typically occurs when the database mirroring or Always On Availability Groups endpoint cannot properly communicate the principal role status, often due to a mismatch in the mirroring state metadata stored in the system catalog. This frequently happens after an unexpected failover, a corrupted endpoint configuration, or when the service account lacks the necessary CONNECT permission on the mirroring endpoint. In some cases, this is triggered by network latency causing the principal to lose quorum with the witness or secondary replicas, leaving the database in an inconsistent state.

Step-by-Step Resolution Guide

Step 1: Verify Endpoint Status and Permissions

  • Open SQL Server Management Studio (SSMS) and connect to the principal instance.
  • Run SELECT * FROM sys.database_mirroring_endpoints; to confirm the endpoint state is STARTED.
  • Ensure the service account has CONNECT permission: GRANT CONNECT ON ENDPOINT::[Mirroring_Endpoint_Name] TO [DOMAIN\ServiceAccount];
  • Check that the endpoint is listening on the correct port (default 5022) using netstat -ano | findstr 5022 in an elevated Command Prompt.

Step 2: Check Firewall and Network Configuration

  • Navigate to Windows Defender Firewall with Advanced Security on Windows Server 2025 / Windows 11.
  • Confirm an inbound rule exists allowing TCP traffic on port 5022 (or your custom mirroring port) for sqlservr.exe.
  • Run Test-NetConnection -ComputerName <SecondaryServerName> -Port 5022 in PowerShell to confirm connectivity between principal and secondary/witness servers.
  • If blocked, add a new rule: New-NetFirewallRule -DisplayName "SQL Mirroring" -Direction Inbound -Protocol TCP -LocalPort 5022 -Action Allow.

Step 3: Reset the Mirroring Session or Availability Group

  • On the principal database, run ALTER DATABASE [YourDB] SET PARTNER OFF; to break a stale mirroring session.
  • Re-establish mirroring using ALTER DATABASE [YourDB] SET PARTNER = 'TCP://SecondaryServer:5022'; from the principal.
  • For Availability Groups, instead remove and re-add the replica via SSMS > Always On High Availability > Availability Group Replicas.
  • Restart the SQL Server service (net stop MSSQLSERVER then net start MSSQLSERVER) to flush cached endpoint states.

Step 4: Review SQL Server Error Logs and Repair Metadata

  • Navigate to C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOG (path varies per instance name/version folder).
  • Search for entries immediately preceding Error 13127 to identify if a role-switch or timeout triggered it.
  • If metadata corruption is suspected, run DBCC CHECKDB('YourDB') WITH NO_INFOMSGS, ALL_ERRORMSGS; to validate database consistency.

Alternative or Verification Steps

  • Confirm resolution by querying SELECT database_id, mirroring_role_desc, mirroring_state_desc FROM sys.database_mirroring; and verifying the role reads PRINCIPAL with state SYNCHRONIZED.
  • If the issue persists, fail over manually to the secondary replica (ALTER DATABASE [YourDB] SET PARTNER FAILOVER;) and then fail back once connectivity is restored to force a clean resynchronization.