alwayson Readonly environment SQL Login routing secondary while preventing access primary

Ashwan 536 Reputation points
2023-08-18T00:34:13.6166667+00:00

we have SQL Server 2016 R3 , always on two nodes, read only routing is configured. We need to have SQL login and test connection to re routing to secondary site using sqlcmd and looks working. so question here is we need to prevent accidental connection to primary and allowing access to secondary. I deny access to SQL login then stop access secondary as well.

connect primary

USE [master]

GO

DENY CONNECT SQL TO [SQLloginusr]

GO

sqlcmd -U SQLloginusr -P SQLloginusr -S groupaglistener1 -d proddb -K ReadOnly

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'SQLloginusr'..

all working when enable the user from primary site.

can any one advice how to achieve this please

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Javier Villegas 900 Reputation points MVP
    2023-08-18T01:00:48.1333333+00:00

    Hello

    After creating the login and granting the permissions you could disable the login in the primary

    ALTER LOGIN [SQLloginusr] DISABLE

    GO

    Regards

    Javier


  2. Erland Sommarskog 120.2K Reputation points MVP
    2023-08-18T21:55:44.91+00:00

    One option to explore is to have a logon trigger. But I don't know if the logon trigger fires on the primary when you connect with Application Intent=ReadOnly, so you would have to test. Then again, maybe eventdata() includes information about application intent.

    (I have a lab AG here at home, but the read-only routing seems to be broken at the moment.)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.