Python连接 SQL Server 报错:DB-Lib error message 20002, severity 9
一、版本说明
python版本: 3.12.1
pymssql版本: 2.3.0 # pymssql.version_info()
SQL Server版本:SQL Server 2008
OS版本: rocky linux 9.4
二、报错信息
Traceback (most recent call last):
File "src/pymssql/_pymssql.pyx", line 650, in pymssql._pymssql.connect
File "src/pymssql/_mssql.pyx", line 2155, in pymssql._mssql.connect
File "src/pymssql/_mssql.pyx", line 712, in pymssql._mssql.MSSQLConnection.__init__
File "src/pymssql/_mssql.pyx", line 1881, in pymssql._mssql.maybe_raise_MSSQLDatabaseException
File "src/pymssql/_mssql.pyx", line 1898, in pymssql._mssql.raise_MSSQLDatabaseException
pymssql._mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.16.250)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.16.250)\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/pymssql/_pymssql.pyx", line 659, in pymssql._pymssql.connect
pymssql.exceptions.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.16.250)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.16.250)\n')
三、排查
通过python自带的os模块,详细打印连接详细日志os.environ['TDSDUMP'] = 'stdout'
从报错信息上可以发现,是找不到/root/.freetds.conf 或者 /usr/local/etc/freetds.conf文件导致的,配置文件内容如下,下面配置是官网提供的示例:
[global]
port = 1433
tds version = 7.0
详情可以查看官网,地址是:https://www.pymssql.org/freetds.html
四、解决
添加上官网的配置
重新连接,没有报错,执行SQL,有数据输出
来源:https://blog.csdn.net/m0_37886429/article/details/139231488
本站大部分文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了您的权益请来信告知我们删除。邮箱:1451803763@qq.com