AOAG Read OnlySecondaryReplica
AOAG Read OnlySecondaryReplica
Availability Groups:
read-only
secondary replicas
Information in this document, including URL and other Internet website references, is subject to change without
notice. Unless otherwise noted, the companies, organizations, products, domain names, email addresses, logos,
people, places, and events depicted in examples herein are fictitious. No association with any real company,
organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any
purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering
subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the
furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other
intellectual property.
Microsoft, Windows, Windows Server, PowerShell, and SQL Server are either registered trademarks or trademarks of
the Microsoft group of companies.
Introduction .............................................................................................................. 4
Objectives.................................................................................................................. 5
Overview of availability groups (AG) and read-only secondary replica...... 7
Configure Always On Availability Groups for SQL Server .............................. 8
Overview of Always On Availability Group Listeners ......................................16
Load-balance secondary replicas ...................................................................... 22
Monitor availability groups.................................................................................. 30
Appendix ................................................................................................................. 33
• Three SQL Servers (deployed on Windows Server) with details deployed to the back-end subnet and joined
to the domain
Domain
Controller
WIN-AGDC
AG Node AG Node AG Node
WIN-AGNODE01 WIN-AGNODE02 WIN-AGNODE03
SSMS
Node.js
SQL Server SQL Server SQL Server
MyExpenses
• Data migration
3. To launch the New Availability Group Wizard, select the New Availability Group Wizard command.
4. The first time you run this wizard, an introduction page appears. To bypass this page in the future, you can
click Do not show this page again. After reading this page, click Next.
5. On the Specify Availability Group Options page, enter the name of the new availability group in the
Availability group name field. This name must be a valid SQL Server identifier that is unique on the
cluster and in your domain. The maximum length for an availability group name is 128 characters.
6. Next, specify the cluster type. Here we will choose WSFC. For details, see Specify Availability Group Name
Page.
7. On the Select Databases page, the grid lists user databases on the connected server instance that are
eligible to become the availability databases. You can select one or more of the listed databases to
participate in the new availability group. The primary database for the MyExpense application has been
created and populated, as well as fully backed up. Select the Expenses database.
Note: These databases will be the initial primary databases. For each listed database, the Size column
displays the database size, if known. The Status column indicates whether a given database meets the
prerequisites for availability databases. If you change a database to make it eligible, click Refresh to
8. On the Specify Replicas page, specify and configure replicas for the new availability group. This page
contains four tabs. Below are the details:
Replicas Use this tab to specify each instance of SQL Server that will host a secondary
replica. Note that the server instance to which you are currently connected must
host the primary replica.
In this lab, we will be using our second node as a secondary replica (win-
agnode02).
To set this up, click Add Replica and use win-agnode02 for the secondary
replica.
Listener Use this tab to create an availability group listener. By default, the wizard does
not create a listener. We will create the listener later in our lab
• You can also do Full database and log backup. Select this option if your environment meets the
requirements for automatically starting initial data synchronization.
• Join only: If you have manually prepared secondary databases on the server instances that will host
the secondary replicas, you can select this option. The wizard will join the existing secondary databases
to the availability group.
• Skip initial data synchronization: Select this option if you want to use your own database and log
backups of your primary databases. For more information, see Start Data Movement on an Always On
Secondary Database (SQL Server).
10. The Validation page verifies whether the values you specified in this wizard meet the requirements of the
New Availability Group Wizard. To make a change, click Previous to return to an earlier wizard page to
change one or more values. Then, click Next to return to the Validation page, and click Re-run
Validation.
11. On the Summary page, review your choices for the new availability group. To make a change, click
Previous to return to the relevant page. After making the change, click Next to return to the Summary
page.
12. The Progress page displays the progress in creating the availability group (configuring endpoints, creating
the availability group, and joining the secondary replica to the group).
14. When the wizard completes, click Close to exit. As an alternative to using the New Availability Group
Wizard, you can use Transact-SQL or SQL Server PowerShell cmdlets. For more information, see Create an
Availability Group (Transact-SQL) or Create an Availability Group (SQL Server PowerShell).
15. Now we need to verify that the database is created on the secondary servers. For this, connect to On our
secondary SQL Server replica (win-agnode02).
16. Finally, validate that the Expenses database has been created and is synchronized by opening the sql script
at C:\HOL Files\SQLScript\DBSynchronized.sql.
2. Expand the Always On High Availability node and the Availability Groups node (MyExpenseAG).
3. To create a listener, right-click the Availability Group Listeners node, and then select the New Listener
command to open the New Availability Group Listener dialog box. For more information, see Add
Availability Group Listener (Dialog Box).
5. Right-click the domain node in the tree (aoag.sqlserver.labs), and select Reload.
7. Right-click the WIN-AGDC node in the tree, and select Clear Cache.
The Availability Group listener should now be reachable, once we configure our application.
• Security
5. Be sure to update the database connection details for this app as well by opening the file
./server/config/server.config.js and updating the login password and port information accordingly.
6. Here you need to specify the logon information:
Username: sa and password: Pass@word1
Change the port: 1433
Change the ‘host’ value from ‘localhost’ to ‘ExListener’
Next, we will configure the application with an availability group listener for an Always On availability group.
Always On Availability Groups: read-only secondary replicas 26
Requirements and recommendations for client connection strings
For a client application to use read-only routing, its connection string must satisfy the following requirements:
• Use the TCP protocol
• Reference the listener of an availability group that is configured to support read-only routing
The following sample connection string for the .NET Framework Data Provider 4.0.2 for SQL Server illustrates the
parts of a connection string that are required and recommended for read-only routing:
Server=tcp:MyAgListener,1433;Database=Db1;IntegratedSecurity=SSPI;ApplicationIntent=ReadO
nly;MultiSubnetFailover=True
1. On Visual Studio Code, click F5 to run the application. You can see it executing and listening on port 8000.
• Fully capable System Center Operations Manager (SCOM) alerting and ticketing to provide details that
enable faster issue resolution
• Custom tasks that manage Always On Availability Groups from the System Center Operations Manager
console
For more information, see System Center Management Pack for SQL Server.
SQL Server Management Studio: The Object Explorer Details pane displays basic information about the
availability groups hosted on the instance of SQL Server to which you are connected. Visit Use Object Explorer
Details to monitor availability groups for more information. Properties dialog boxes enable you to view the
properties of availability groups, replicas, or listeners and, in some cases, to change their values. For more
information, see Availability Group Properties and Availability Replica Properties for more information.
For this lab, however, we will use Dynamic Management Views (DMVs) to execute predefined, built-in views to
monitor replicas.
The Transact-SQL (DMVs) Always On Availability Groups catalog and dynamic management views provide a
wealth of information about your availability groups and their replicas, databases, listeners, and WSFC cluster
environment. For more information, see Monitor Availability Groups (Transact-SQL).
1. To monitor Always On Availability Groups, a feature on a server instance, use the SERVERPROPERTY
built-in function:
This returns server property information about whether Always On Availability Groups is enabled and, if
so, whether it has started on the server instance.
2. To monitor the WSFC cluster that hosts a local server instance and is enabled for Always On Availability
Groups, use the view:
sys.dm_hadr_cluster
If the WSFC node that hosts an instance of SQL Server enabled with Always On Availability Groups has
WSFC quorum, then sys.dm_hadr_cluster returns a row that exposes the cluster name and quorum
information. If the WSFC node has no quorum, no rows are returned.
sys. availability_groups
This returns a row for each availability group for which the local instance of SQL Server hosts an availability
replica. Each row contains a cached copy of the availability group metadata.
2. On the Start menu, point to All Programs -> Microsoft SQL -> Server 2016 -> Configuration Tools,
and then click SQL Server Configuration Manager.
3. In SQL Server Configuration Manager, click SQL Server Services. Right-click SQL Server (<instance
name>), where <instance name> is the name of a local server instance for which you want to enable
Always On Availability Groups, and then click Properties.
5. Verify that the Windows failover cluster name field contains the name of the local failover cluster. If this
field is blank, the server instance currently does not support Always On Availability Groups. It might not be
supported because the local computer is not a cluster node, the WSFC cluster has been shut down, or this
edition of SQL Server 2016 does not support Always On Availability Groups.
6. Select the Enable Always On Availability Groups check box, and click OK.
SQL Server Configuration Manager saves your change. Then, you must manually restart the SQL Server service.
This enables you to choose a restart time that is best for your business requirements. When the SQL Server service
restarts, Always On will be enabled, and the IsHadrEnabled server property will be set to 1.
You can also use PowerShell: Enable and Disable Always On Availability Groups (SQL Server).
Repeat steps 1‒6 for each SQL Server in your cluster.
4. You will need to supply the SA password you provided in provisioning the SQL Server.
5. The default port in the sample db.config.js file expects tcp port 11433. In db.config.js, change this value to
1433.
6. Return to the Node.js command prompt, and enter node app.js.
7. You will be presented with two data load options. Choose option 1, which will take a few seconds to run.
This will create a database named Expenses, and populate data needed for the app to run.