AlwaysOn Availability Group Read Only Routing Script


–AlwaysOn Availability Group Read Only Routing Script
https://www.sqlservercentral.com/scripts/alwayson-availability-group-read-only-routing-script
============================================
If your objective is to use your secondary node(s) as readable secondary’s, read on.

While implementing my availability group into higher environments, after having it working quite easily in development,
we ran into a few issues. After a long weekend, and making sure all the appropriate firewall ports were open,
to make the listener, primary and secondary nodes accessible to all of the other components, we found these issues were important;
mainly, because the database servers are in a different domain than the other components.

Not all settings are done via the SSMS UI when setting up the Availability Group
The routing URLs may need to use IP addresses
The routing list is needed for proper read only routing
Set the first six (6) variables for your environment and run the script in text output mode. The commands will be generated to properly configure your read only routing. The select statement at the end will show the key settings once the configuration is applied.

* Note: this script configures the primary and a single secondary node.
=====================================================
USE [master]
GO

SET NOCOUNT ON
GO

DECLARE @AGName VARCHAR(40) = ‘myAvailabilityGroup’ — Availability Group Name
, @PrimaryNodeName VARCHAR(40) = ‘ProdDB01’
, @SecondaryNodeName VARCHAR(40) = ‘ProdDB02’
, @PrimaryNodeIP VARCHAR(40) = ‘10.5.6.10’
, @SecondaryNodeIP VARCHAR(40) = ‘10.6.6.11’
, @Domain VARCHAR(40) = ‘.prod.net’
, @RouteUsingIP TINYINT = 1 — 1 for True, 0 for False (1 is recommended)
, @PrimaryRoutingURL VARCHAR(40) = ” — gets set by script
, @SecondaryRoutingURL VARCHAR(40) = ” — gets set by script
, @SQLCommand VARCHAR(2000)

IF @RouteUsingIP > 0
BEGIN
SET @PrimaryRoutingURL = @PrimaryNodeIP
SET @SecondaryRoutingURL = @SecondaryNodeIP
END
ELSE
BEGIN
SET @PrimaryRoutingURL = @PrimaryNodeName + @Domain
SET @SecondaryRoutingURL = @SecondaryNodeName + @Domain
END

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@PrimaryNodeName+”’ WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));’
PRINT @SQLCommand

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@PrimaryNodeName+”’ WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = N”TCP://’+@PrimaryRoutingURL+’:1433”));’
PRINT @SQLCommand

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@SecondaryNodeName+”’ WITH (SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY));’
PRINT @SQLCommand

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@SecondaryNodeName+”’ WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = N”TCP://’+@SecondaryRoutingURL+’:1433”));’
PRINT @SQLCommand

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@PrimaryNodeName+”’ WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=(”’+@SecondaryNodeName+”’,”’+@PrimaryNodeName+”’)));’
PRINT @SQLCommand

SET @SQLCommand = N’
ALTER AVAILABILITY GROUP [‘+@AGName+’] MODIFY REPLICA ON
N”’+@SecondaryNodeName+”’ WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=(”’+@PrimaryNodeName+”’,”’+@SecondaryNodeName+”’)));’
PRINT @SQLCommand

/*
SELECT replica_server_name ,endpoint_url ,availability_mode_desc ,failover_mode_desc
,session_timeout ,primary_role_allow_connections_desc ,secondary_role_allow_connections_desc
,create_date ,read_only_routing_url
FROM sys.availability_replicas
*/

Author: Rajib Kundu

Rajib Kundu is a SQL Server Evangelist/Cloud Architect with over 16 years of Expertise.A husband, father of boy, entrepreneur, Rajib has learned throughout his journey the value of hard work and giving back. Rajib is a hard working & high energy individual fueled by his passion to help people and solve problems on SQL Server and Robotic Process Automation. Rajib Kundu is a technology enthusiast with a track record of delivering breakthrough innovations at leading Indian pure play & multi national IT companies. Equally comfortable with executives, creative directors and engineers, Rajib has differentiated himself through his ability to balance business and technology needs to deliver powerful solutions. Rajib Kundu Innovative with demonstrated success in increasing revenues, market share, and earnings, achieving cost reduction, and improving client satisfaction in customer-facing operations and large, diverse organisation. Expertise in management and implementation of complex corporate acquisition initiatives and successful organisation Integrations. Talent for analysing competitive landscape, conducting research, and aligning product offering with customer requirements which translates into lower product development and Implementation costs. Reputation as a change agent With the ability to analyse issues, devise continuous process Improvements, and incorporate business process outsourcing Initiatives to Increase efficiency ,streamline operations, and decrease aggregate expenses With limited resources. You can contact me here: India:-+91-9731155800 | USA & Canada: +1- 647-694-1826 | Skype: rk_india1@rediffmail.com

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.