Manual failover without data loss
Use this method when the primary replica is available, but you need to temporarily or permanently change which instance hosts the primary replica. To avoid potential data loss, before you issue the manual failover, ensure that the target secondary replica is up to date.
To manually fail over without data loss:
- Make the current primary and target secondary replica
SYNCHRONOUS_COMMIT
. - To identify that active transactions are committed to the primary replica and at least one synchronous secondary replica, run the following query:
synchronization_state_desc
isSYNCHRONIZED
. - Update
REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT
to 1.The following script setsREQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT
to 1 on an availability group namedag1
. Before you run the following script, replaceag1
with the name of your availability group:SQLCopyALTER AVAILABILITY GROUP [AGRScale] SET (REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 1);
This setting ensures that every active transaction is committed to the primary replica and at least one synchronous secondary replica. NoteThis setting is not specific to failover and should be set based on the requirements of the environment. - Set the primary replica offline to prepare for the role change:
- Promote the target secondary replica to primary.
- Update the role of the old primary to
SECONDARY
, run the following command on the SQL Server instance that hosts the old primary replica: - Resume data movement, run the following command for every database in the availability group on the SQL Server instance that hosts the primary replica.
- Re-create any listener you created for read-scale purposes and that isn’t managed by a cluster manager. If the original listener points to the old primary, drop it and re-create it to point to the new primary.
ALTER AVAILABILITY GROUP [AGRScale]
MODIFY REPLICA ON N'<node2>'
WITH (AVAILABILITY_MODE = SYNCHRONOUS_COMMIT);
SELECT ag.name,
drs.database_id,
drs.group_id,
drs.replica_id,
drs.synchronization_state_desc,
ag.sequence_number
FROM sys.dm_hadr_database_replica_states drs, sys.availability_groups ag
WHERE drs.group_id = ag.group_id;
ALTER AVAILABILITY GROUP [AGRScale]
SET (REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 1);
ALTER AVAILABILITY GROUP [AGRScale] OFFLINE
ALTER AVAILABILITY GROUP AGRScale FORCE_FAILOVER_ALLOW_DATA_LOSS;
ALTER AVAILABILITY GROUP [AGRScale]
SET (ROLE = SECONDARY);
Comments