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)
................................................

