简体   繁体   English

使用 ReadOnly 连接到 Python 中的 SQL Server

[英]Connect to SQL Server in Python with ReadOnly

I am trying to connect to the SQL server in python using pyodbc.我正在尝试使用 pyodbc 连接到 python 中的 SQL 服务器。 I am unable to connect because my database is ReadOnly.我无法连接,因为我的数据库是只读的。 I tried the solutions provided at other link :我尝试了其他链接提供的解决方案:

import pyodbc
readonly_conn_str = "DRIVER={SQL Server};SERVER=...; DATABASE=...;readonly=True;"

conn = pyodbc.connect(readonly_conn_str)

But, I am still getting the following error:但是,我仍然收到以下错误:

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The target database ('PRMOperationalDB') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online. (978) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The target database ('PRMOperationalDB') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online. (978); [42000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)") ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]目标数据库 ('PRMOperationalDB') 在可用性组中,当应用程序意图设置为时,当前可访问连接只读。有关应用程序意图的详细信息,请参阅 SQL Server 联机丛书。(978) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver]无效的连接字符串属性 (0); [42000] [Microsoft][ ODBC SQL Server 驱动程序][SQL Server]目标数据库 ('PRMOperationalDB') 位于可用性组中,当应用程序意图设置为只读时,当前可访问连接。有关应用程序意图的详细信息,请参阅 SQL Server 联机丛书. (978); [42000] [Microsoft][ODBC SQL Server Driver]无效的连接字符串属性 (0)")

Any suggestions how to resolve this issue?任何建议如何解决这个问题?

readonly=True is an attribute of pyodbc's connect() method, not a part of the connection string you pass to SQL Server. readonly=True是 pyodbc 的connect()方法的一个属性,而不是您传递给 SQL Server 的连接字符串的一部分。 IMHO try just passing all the attributes instead of building a full connection string:恕我直言,尝试只传递所有属性而不是构建完整的连接字符串:

conn = pyodbc.connect(driver='{SQL Server}', host=<server>, database=<db>,
  trusted_connection='yes', user='', password='', readonly = True)

Or, like the answer you mentioned offered, use:或者,就像您提到的答案一样,使用:

conn = pyodbc.connect('driver={ODBC Driver 17 for SQL Server};'
  + 'SERVER=...;DATABASE=...;'
  + 'UID=' + user + ';PWD=' + password + ';'
  + 'ApplicationIntent=ReadOnly')

Also, are you intentionally connecting explicitly to a secondary?另外,您是否有意明确连接到辅助设备? Why don't you connect to the AG name and if your app is doing only read operations then the worst that happens (if, say, someone breaks read-only routing) is that those read operations happen on the primary.为什么不连接到 AG 名称,如果您的应用程序只执行读取操作,那么最糟糕的情况(例如,如果有人破坏只读路由)是这些读取操作发生在主节点上。 Your code should never connect to a specific physical host / cluster node and assume that will always be a read-only secondary... what happens if there is a failover tomorrow?您的代码永远不应连接到特定的物理主机/集群节点,并假定它始终是只读辅助节点……如果明天发生故障转移会发生什么? Who's updating all the connection strings?谁在更新所有连接字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM