zoukankan      html  css  js  c++  java
  • pymssql连接数据报"Connection to the database failed for an unknown reason"

    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)
         ................................................
  • 相关阅读:
    【数通大讲坛02】IP子网划分(VLSM)
    【数通大讲坛01】利用TCP/IP模型理解数据通信过程
    新公司的第一个任务-重构系统(二)
    作为一名程序员的心得分享
    新公司的第一个任务-重构系统(一)
    第一次离职
    2018年终总结
    跳槽并不简单
    小公司的项目交付
    记一次项目测试间隔太长的问题
  • 原文地址:https://www.cnblogs.com/kerrycode/p/13540949.html
Copyright © 2011-2022 走看看