AlwaysOn:Joining database on secondary replica resulted in an error

Many a times when you try to create a new Availability group it would fail with the below error message :

 

TITLE: Microsoft SQL Server Management Studio
------------------------------

Joining database on secondary replica resulted in an error.  (Microsoft.SqlServer.Management.HadrTasks)

------------------------------
ADDITIONAL INFORMATION:

Attempting to join availability group resulted in an error. (Microsoft.SqlServer.Management.HadrTasks)

------------------------------

Failed to join the database 'GAlwaysOnDB' to the availability group 'GAG' on the availability replica 'GGAURAV-N03'. (Microsoft.SqlServer.Smo)

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=12.0.2000.8+((SQL14_RTM).140220-1752)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&LinkId=20476

------------------------------

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------

The connection to the primary replica is not active.  The command cannot be processed. (Microsoft SQL Server, Error: 35250)

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.2000&EvtSrc=MSSQLServer&EvtID=35250&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------

 

Few of the things to check:

1. You are able to connect to the secondary replica from the SSMS on the primary replica. If this fails, make sure we have the ports on which SQL is listening is opened on Firewall.

2. Also, make sure that the UDP port 1434 is opened so that browser is able to listen to the incoming connection. Else, it would be failing with the below error : 

TITLE: Connect to Server
------------------------------

Cannot connect to GGAURAV-N02\DISTRIBUTION.

------------------------------
ADDITIONAL INFORMATION:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (Microsoft SQL Server, Error: -1)

MSSQLSvc/GGAURAV.xxxxx.cxxxxx.xxxxxxt.com:1433

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=-1&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------

 

3. Check if the port which is used for mirroring endpoint is opened in the firewall as the communication happens from this port.

4. Check if we are using the same domain account on all the replicas, else we have proper permissions on the endpoint for the service accounts for data to flow through.

5. Check if you have the SPN's created for the SQL instance. Please make use of setspn utility to check. setspn -L accountname.

 

Happy Learning & Troubleshooting!!

Disclaimer:  Everything here, is my personal opinion and is not read or approved by Microsoft before it is posted. No warranties or other guarantees will be offered as to the quality of the opinions or anything else offered here.