To perform management you almost always need to connect to the primary replica, but how can I figure out which one is primary without just connecting to the instances one by one until I get lucky?
https://cdn.meme.am/instances/500x/55766239.jpg |
Enter the T-SQL:
SELECT
AG.name AS AvailabilityGroupName
, HAGS.primary_replica AS PrimaryReplicaName
, HARS.role_desc as LocalReplicaRoleDesc
, DRCS.database_name AS DatabaseName
, HDRS.synchronization_state_desc as SynchronizationStateDesc
, HDRS.is_suspended AS IsSuspended
, DRCS.is_database_joined AS IsJoined
FROM master.sys.availability_groups AS AG
LEFT OUTER JOIN master.sys.dm_hadr_availability_group_states as HAGS
ON AG.group_id = HAGS.group_id
INNER JOIN master.sys.availability_replicas AS AR
ON AG.group_id = AR.group_id
INNER JOIN master.sys.dm_hadr_availability_replica_states AS HARS
ON AR.replica_id = HARS.replica_id AND HARS.is_local = 1
INNER JOIN master.sys.dm_hadr_database_replica_cluster_states AS DRCS
ON HARS.replica_id = DRCS.replica_id
LEFT OUTER JOIN master.sys.dm_hadr_database_replica_states AS HDRS
ON DRCS.replica_id = HDRS.replica_id
AND DRCS.group_database_id = HDRS.group_database_id
ORDER BY AG.name, DRCS.database_name
This query can be run on any of the replicas, and it will return information about the Availability Groups and their member databases, *including* the name of the primary replica instance!
http://vignette4.wikia.nocookie.net/randycunningham9thgradeninja/images/9/97/YES_cat.jpg/revision/latest?cb=20150330230809 |
Hope this helps!
Is [is_database_joined] accurate in master.sys.dm_hadr_database_replica_cluster_states? I have DB outside AAG never shows up.
ReplyDeleteI find powershell is much easier for this. I have a function I use all the time that takes a param for a computer name and returns the SQL Instance Object. Then it is just three lines to get the primary from any host.
ReplyDelete$instance = get-csqlinstance 'yourhostname' #function that takes the computer name and returns the #Microsoft.SqlServer.Management.SMO.Server object of the instance
$ags = $instance.AvailabilityGroups # get the ags on that server
$pris = $ags | Select -Property Parent, Name, PrimaryReplicaServerName -Unique #return the Servername, agname, primary replica
No joins quick and easy. You could also return any of the other info you have in the sql script. Just another way to go about it I find PowerShell is much better than SQL at many things.