pymssql连接数据报"Connection to the database failed for an unknown reason"

血红的双手。 提交于 2020-10-01 10:32:32

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'

 

clip_image001

 

后面经过仔细的对比检查、验证测试,终于发现这台机器的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)
     ................................................
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!