pymssql连接数据报"Connection to the database failed for an unknown reason"
2020-08-21 14:40 潇湘隐者 阅读(4476) 评论(0) 编辑 收藏 举报Python脚本采集某一台SQL Server服务器数据的时候,突然遇到“Connection to the database failed for an unknown reason”错误,更详细的信息如下
Traceback (most recent call last):
File "src/pymssql.pyx", line 636, in pymssql.connect
File "src/_mssql.pyx", line 1957, in _mssql.connect
File "src/_mssql.pyx", line 677, in _mssql.MSSQLConnection.__init__
_mssql.MSSQLDriverException: Connection to the database failed for an unknown reason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/konglb/python/get_server_info.py", line 84, in <module>
autocommit=True);
File "src/pymssql.pyx", line 645, in pymssql.connect
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/konglb/python/get_server_info.py", line 474, in <module>
logger.error(fe.message)
AttributeError: 'InterfaceError' object has no attribute 'message'
后面经过仔细的对比检查、验证测试,终于发现这台机器的IP地址后面有空格(一个表存储这些信息服务器信息,但是最近这个新增的记录在INSERT的时候,SERVER_IP字段多了一个空格),直接导致Python脚本使用pymssql连接数据库的时候遇到这个错误。后面我实验验证了一把,只要pymssql.connect的host字段的变量如果有空格,就会报这个错误。特此记录一下这个比较特殊的错误!
...................................................
for row in rows:
try:
src_db_conn = pymssql.connect(
host=row['SERVER_IP'],
user=row['USER_NAME'],
password=row['PASSWORD'],
database='master',
charset="utf8",
autocommit=True
);
sub_cursor =src_db_conn.cursor(as_dict=True)
................................................

· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2019-08-21 pymssql默认关闭自动模式开启事务行为浅析
2019-08-21 Azure上MySQL的离线备份:将备份拷贝到Azure Blob上
2018-08-21 MySQL如何判别InnoDB表是独立表空间还是共享表空间
2016-08-21 ORA-01336: specified dictionary file cannot be opened
2015-08-21 Linux NetHogs监控工具介绍
2015-08-21 Linux make: g++: Command not found
2013-08-21 Nagios学习实践系列——产品介绍篇