DBAtlas · Database operations atlas

Check the listener, its IPs and read-only routing configuration

SQL ServerAlways On & HA

Command

-- Listener, port and IP states:
SELECT ag.name AS ag_name, l.dns_name, l.port, l.is_conformant,
       ip.ip_address, ip.ip_subnet_mask, ip.state_desc
FROM   sys.availability_group_listeners l
  JOIN sys.availability_groups ag ON ag.group_id = l.group_id
  LEFT JOIN sys.availability_group_listener_ip_addresses ip ON ip.listener_id = l.listener_id;

-- Read-only routing list: where read intent goes when X is primary:
SELECT ag.name AS ag_name, pr.replica_server_name AS when_primary_is,
       rl.routing_priority, sr.replica_server_name AS route_to,
       sr.read_only_routing_url, sr.secondary_role_allow_connections_desc
FROM   sys.availability_read_only_routing_lists rl
  JOIN sys.availability_replicas pr ON pr.replica_id = rl.replica_id
  JOIN sys.availability_replicas sr ON sr.replica_id = rl.read_only_replica_id
  JOIN sys.availability_groups ag   ON ag.group_id = pr.group_id
ORDER BY ag.name, when_primary_is, rl.routing_priority;

-- Replica endpoint and connection settings:
SELECT replica_server_name, endpoint_url, read_only_routing_url,
       secondary_role_allow_connections_desc, seeding_mode_desc, session_timeout
FROM   sys.availability_replicas;

-- Routing test from the command line (-d is required with -K ReadOnly):
-- sqlcmd -S tcp:AGL-Musteri,1433 -d Musteri -E -M -K ReadOnly -Q "SELECT @@SERVERNAME"

What it does

Shows the listener DNS name, port and the cluster state of each subnet IP; for each primary scenario in the read-only routing list, which replica and URL read-intent connections are routed to.

When to use it

When applications cannot connect after a failover, ApplicationIntent=ReadOnly connections land on the primary, or a new subnet/replica was added.

Caution

Read-only routing works only for connections made through the listener with ApplicationIntent=ReadOnly and an explicit database; direct connections to an instance name are not routed. A replica with an empty read_only_routing_url cannot be a routing target. On a multi-subnet listener, clients without MultiSubnetFailover=True see 20-30 s connection timeouts after failover. Read-only query; safe.

Open in DBAtlas →

Related