SQL AlwaysOn AG Listeners Setup v1.0
SQL AlwaysOn AG Listeners Setup v1.0
Availability databases hosted on SQL Server Always On Availability Groups (AG) can be connected using
a unique Virtual Network Name (VNN), called the Availability Group Listener. When an Availability Group
is enabled, clients can connect to databases in both primary and secondary replicas without explicitly
specifying the SQL Server instance name. You don’t even need to know the instance name to connect to
an AG.
When you have configured read-only routing for secondary replicas and the application or client
connection contains the application intent set to read-only, the listener will redirect the connection to
readable secondary replicas, Otherwise, the listener redirects both read-write and read-only intent
connections to the primary replica. If there is a failover, the listener will redirect connections to the new
primary and secondary connections based on the application intent.
Example:
DNS: salesag.mscorp.com
Port: 1433
IP: static or DHCP
The Listener is always owned by the SQL Server instance where the primary replica resides. At the time
of failover, the new primary replica will own the listener.
A few good practices to follow when creating the Availability Group Listener.
Use a static IP address for reliable connections and multi subnet failover
Use a meaningful and unique listener name for each AG
Test the Listener on a staging server before implementing it on production environment
Additionally, to avoid the accidental deletion on cluster objects, enable the Protect objects from
accidental deletion option.
The Availability Group Listener can be created while creating the AG, or it can be created afterward. The
Listener creation options are available on the Specify Replicas window, under the Listener tab.
If you want to skip the Listener creation while creating Availability Group, you can leave the default
option, Do not create an availability group listener now, selected. Check the below image for reference:
After creating the Availability Group, you can configure the listener from Availability Groups -> Group
Name -> Add Listener. Either in the create an availability group listener or the New Availability Group
Listener wizards, specify the Listener DNS Name, Port, and Network Mode values. The following figure
shows the example availability group listener from my lab.
To add an IP address, click on the Add button and provide the IP address in the Add IPv4 Address text
box.
Additionally, you can script out the changes to a query window and see T-SQL script. The code from my
demo cluster are shown here:
You can configure port 1433, the default SQL Server instance port, for the Availability Group Listener. In
that case, you don’t have to specify the port number explicitly in a connection string or client
connection. If you specify a custom port, the client must explicitly specify the port. If you have more
than one SQL Server instance on the machine, I recommend you use a different port because the
listener may be configured to the named instance, which is often listening on a different port.
To connect to a database in an AG through the Availability Group Listener, specify the listener name in
the server name box as shown below:
I am using the default port number in the dialog above. If you are using a different port number, specify
it explicitly in the connection string by including it after the name, preceded by a comma. An example
connection string with a different port number (3306) is provided below:
When you create an Availability Group Listener, the cluster will create a computer object on a domain
controller and assign its computer name as the virtual network name automatically. To perform this, the
cluster should have the Create Computer Object permission in your Active Directory.
Alternatively, I created a listener with the help of a Domain Administrator by following this method.
First, create a computer object in Active Directory and assign the listener name as the computer name
to that object. The cluster, the name of the cluster that owns the AG, will have the full control on the
newly created computer object. Add an entry in DNS to link that computer name with a static IP
address. Now create the listener on the AG. In the troubleshooting section of this article, we look into
the common error scenarios with listener creation permission.
On SQL Server, you need sysadmin privilege to create the availability group listener and at least the
ALTER AVAILABILITY GROUP permission to modify the listener.
In a SQL Server Availability Group (AG), the read-only routing feature provides scalability by redirecting
read-only connections (SELECT queries) to readable secondary replicas. The routing of these connection
to secondary replicas works only when the applications or clients connecting availability databases
through availability group listener. If you are connecting directly to SQL Server instance name, at the
time of failover, connections will not automatically failover to new primary server.
As stated earlier, listener will redirect connections to both primary and secondary replicas. If you have
configured any of the secondary replicas as readable and specify the application intent in your
connection string as read only, the listener will redirect connections to the secondary readable replicas
based on the read-only routing configuration. Refer here for a detailed explanation. An example
connection string with the Application Intent and default database set is given below:
connUrl =
"jdbc:sqlserver://mscorpag.mscorp.com:3306;databaseName=mscorp;user=rathish;password=pwd;Appl
icationIntent=ReadOnly";
You must specify the ApplicationIntent and Default database in the connection string for read-only
routing to work, otherwise the connection will be redirected to primary replica only. Later in this article,
we will see the troubleshooting steps for this scenario.
You should set the MultiSubnetFailover option as True to enable this option and provide faster failover
when your AG spans multiple subnets. It is recommended to set this option as True, even if the AG only
spans a single subnet. This provides additional optimizations, even for single subnets at the time of
failover.
connUrl =
"jdbc:sqlserver://mscorpag.mscorp.com:3306;databaseName=mscorp;user=rathish;password=pwd;Appl
icationIntent=ReadOnly;MultiSubnetFailover=True";
SQL Server provides the following catalog views to monitor availability group listener properties:
You can remove Availability Group Listener, either using SSMS or T-SQL. In SSMS, right-click on listener
name and select the Delete options to remove the availability group listener.
Here are a few sample scenarios that might occur along with a few solutions.
Message:
Possible Cause 1: The cluster name account does not have the Create Computer Objects in Active
Directory Organizational Unit.
Solution: If your organisation polity allowed, grant Create Computer Object permission to cluster
account, or pre-stage the required objects on the Active Directory, organizational unit.
When you create an Availability Group Listener, a virtual computer object will be created at the
particular organizational unit of active directory automatically. You can pre-stage the virtual computer
objects as follows:
Create a computer objects under the organizational unit where you hosted the cluster. Check with your
domain administrator if you don’t have access to create objects on Active Directory Domain Controller.
Grant Full control permission to cluster name account on newly created computer object. Note that, this
name should be same as the listener name, you will be creating on the AG.
Possible Cause: You have not mentioned the default database in the connection string. For more
information on this behavior, click here.
Error: You are unable to connect to availability group listener in a multi-subnet environment. This error
usually occurs at the time of failover.
Possible Cause 1: Your application uses legacy data provider that does not support the multi-subnet
failover features.
Solution: Use the newer version of SQLClient drivers that supports multi-subnet features.
Possible Cause 2: MultiSubnetFailover parameters are not used in the connection string.
Example:
connUrl =
"jdbc:sqlserver://mscorpag.mscorp.com:3306;databaseName=mscorp;user=rathish;password=pwd;Appl
icationIntent=ReadOnly;MultiSubnetFailover=True";
Error: After failover in a multi-subnet environment, ping command from client not resolving to new IP
address of the listener and DNS entry of the listener name shows IPs of both subnets.
Possible Cause: This error generally occurs, when listener is created using the Failover Cluster Manager,
rather than from SSMS.
Example:
PowerShell:
Import-Module FailoverClusters
Get-ClusterResource dbcluster |Set-ClusterParameter
RegisterAllProvidersIP 0
Cluster.exe:
cluster /cluster: dbcluster res mscorpag /priv
RegisterAllProvidersIP=0
If still ping to listener returning wrong IP address, from application/client system, open a command
prompt as administrator and run the ipconfig /flushdns command.
Error 19456, Severity 16: None of the IP addresses configured for the availability group listener can be
hosted by the server '%.*ls'. Either configure a public cluster network on which one of the specified IP
addresses can be hosted, or add another listener IP address which can be hosted.
Error: Error 8557
Message:
Message: Error 8557 (Your computer could not be joined to the domain. You have exceeded the
maximum number of computer accounts you are allowed to create in this domain. Contact your system
administrator to have this limit reset or increased.)
A computer object is created automatically whenever you create an Availability Group Listener. Even if
you drop the listener, the computer object remains there in Active Directory. By default, authenticated
domain users can create up to 10 computer objects. When this limit is exceeded, it will throw an error at
the time of the Availability Group Listener creation.
Best practices
A few good practices to follow when creating the Availability Group Listener.
Use a static IP address for reliable connections and multi subnet failover
Use a meaningful and unique listener name for each AG
Test the Listener on a staging server before implementing it on production environment
Additionally, to avoid the accidental deletion on cluster objects, enable the Protect objects from
accidental deletion option.
Additional Information
Windows Permissions
Permissions Link
The cluster object name (CNO) of WSFC cluster that is Steps for configuring the account for
hosting the availability group must have Create the person who installs the
If your organization requires that you prestage the Steps for prestaging an account for
computer account for a listener virtual network name, a clustered service or
you will need membership in the Account application in Failover Cluster Step-
Operator group or your domain administrator's by-Step Guide: Configuring Accounts
assistance. in Active Directory.
Tip
Generally, it is simplest not to prestage the computer account for a listener virtual
network name. If you can, let the account to be created and configured automatically
when you run the WSFC High Availability wizard.
Ref: https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/create-or-
configure-an-availability-group-listener-sql-server?view=sql-server-ver15