I have 2 Microsoft SQL Server database servers DBServer1 and DBServer2 and 2 databases DB1 and DB2. Each of the databases is in a different availability group between the 2 servers. Therefore:
Availability Group 1 (AG1) contains a DB1 in a cluster between DBServer1 and DBServer2 and has listenerIP such as 192.168.80.1
Availability Group 2 (AG2) contains a DB2 in a cluster between DBServer1 and DBServer2 and has listenerIP such as 192.168.80.2
The database DB1 has some synonyms pointing to some tables of the listener of DB2. For example:
USE [DB1]
CREATE SYNONYM [dbo].[REMOTETABLE] FOR [192.168.80.2].[DB2].[dbo].[REMOTETABLE]
Then I try to perform a query to DB1 such as:
SELECT *
FROM [DB1].[dbo].[REMOTETABLE]
However the synonym does not work as I would expect. When both of the databases are in the same server (either both in DB1 or both in DB2) a query works perfectly and returns the content of REMOTETABLE from DB2. However when one of the database is in DBServer1 and the other in DBServer2 the query fails with the following error:
The target database, 'DB2', is participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access.
To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group. For more information, see the ALTER AVAILABILITY GROUP statement in SQL Server Books Online.
I would like to understand why is this happening if the synonym is pointing to the listener. Also, is there any way to make synonyms works with listeners?
Thank you very much