尝试使用 pyodbc 连接到 SQL Server 数据库时出现操作错误
我正在尝试使用 Python 3 中的 pyodbc 连接到 SQL Server 数据库。但是当我尝试建立连接时出现错误。
我做了这样的事情:
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
我得到这个:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol][error:140B40C7:SSL routines:SSL_do_handshake:peer did not return a certificate] (-1) (SQLDriverConnect)')
有人知道如何解决这个问题吗?该数据库不是我自己的,所以我希望有一个不需要更改任何设置的解决方案。
我在 Linux 的 Windows 子系统中运行 Ubuntu。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个
适用于 SQL Server 的 ODBC 驱动程序 18.0 已发布
因此这
相同
与如果您不想要加密连接则必须选择退出
因此,如果您的服务器使用自签名证书,您还必须选择退出证书验证。所以
There is a breaking change in ODBC Driver 18 for SQL Server
ODBC Driver 18.0 for SQL Server Released
So this
is the same as
If you don't want an encrypted connection you must opt out:
So if your server is using a self-signed certificate, you also must opt out of certificate validation. so
我最终从 WSL 中取出了我的脚本。在我的例子中,在 Windows 下运行相同的命令(使用 David 的添加或
ODBC Driver 17 for SQL Server
而不是 18)可以正常工作。I ended up taking my script out of WSL. Running the same command (with David's additions or
ODBC Driver 17 for SQL Server
instead of 18) under Windows works without issues in my case.我使用了 pyodbc 库,但不断出现错误。使用 pymssql 对我有用:
不需要端口、用户名和密码
I used the pyodbc library, but kept getting errors. Using pymssql worked for me:
No need for port, username and password
对于 2016 年之前的 SQL Server,请尝试使用 JDCB 驱动程序而不是 ODBC 驱动程序。
Maven JDBC:
Python / Pyspark 代码:
For SQL Servers older than 2016, try using JDCB driver instead of ODBC driver.
Maven JDBC:
Python / Pyspark code:
这超出了原来问题的范围。最好是对 @david-browne-microsoft 的答案的评论,但我还不能发表评论。
对于那些使用 sqlalchemy 的用户(尤其是 1.4 之前的版本,没有 sqlalchemy.engine.URL.create 方法,否则请参阅 如何使用 SQLAlchemy create_engine() 和包含 @ 的密码),您可以连接像这样:
所以
Encrypt=no&TrustServerCertificate=yes
可以简单地添加到查询参数中。我一直在相关问题的其他答案中看到trusted_connection=yes
,但这对我不起作用。信任这里连接的原因是它是本地主机上的一个具有自签名证书的开发数据库,这对于生产来说并不安全。
在我的连接字符串中,驱动程序参数应该已经经过 URL 编码,例如
'ODBC+Driver+18+for+SQL+Server'
。This goes beyond the scope of the original question. Preferably it would be a comment on the answer by @david-browne-microsoft, but I can't post a comment yet.
For those using sqlalchemy (especially versions older than 1.4 that don't have sqlalchemy.engine.URL.create method, otherwise see How do I use SQLAlchemy create_engine() with password that includes an @), you can connect like this:
So
Encrypt=no&TrustServerCertificate=yes
can simply be added to the query parameters. I kept seeingtrusted_connection=yes
in other answers to related questions, but that did not work for me.The reason for trusting the connection here is that it is a development database on localhost with self-signed certificates, this is not safe for production.
In my connection string the driver parameter should be already URL encoded, e.g.
'ODBC+Driver+18+for+SQL+Server'
.