2012-04-12 79 views
12

我不知道爲什麼我得到此錯誤並找不到任何解決方案。我可以使用freetds tsql連接到SQL Server數據庫,但當使用pymssql.connect連接時,我仍然收到錯誤。無法使用pymssql連接到SQL Server數據庫,但可以使用基礎freetds進行連接tsql

特定的錯誤是:

pymssql.OperationalError: (18456, "Login failed for user 'xxx'.DB-Lib error message 18456, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n")

我已配置爲freetds的設定爲:

[custom_config] 
    host = myhost 
    port = 1433 
    tds version = 7.0 
    encryption = request 
    dump file = /tmp/freetds.log 

運行:

tsql -S custom_config -U tsmv -P xxx 

回報:

locale is "en_US.UTF-8" 
locale charset is "UTF-8" 
using default charset "UTF-8" 
1> 

它允許我查詢數據庫。

但是,在運行:

python 
>> import pymssql 
>> pymssql.connect(server='custom_config', user='user', password='xxx', database='database') 

引發上述錯誤。

我正在使用Linux CentOS,python 2.6.6,freetds 0.92 dev(我試過用tdsver = 7.0編譯的其他版本)。

的freetds的日誌:

log.c:196:Starting log file for FreeTDS 0.92 
on 2012-04-12 10:39:15 with debug flags 0x4fff. 
iconv.c:330:tds_iconv_open(0x1391b70, ISO-8859-1) 
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1 
iconv.c:187:local name for UTF-8 is UTF-8 
iconv.c:187:local name for UCS-2LE is UCS-2LE 
iconv.c:187:local name for UCS-2BE is UCS-2BE 
iconv.c:349:setting up conversions for client charset "ISO-8859-1" 
iconv.c:351:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion 
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion 
iconv.c:394:tds_iconv_open: done 
net.c:205:Connecting to xx.x.x.xxx port 1433 (TDS version 7.1) 
net.c:270:tds_open_socket: connect(2) returned "Operation now in progress" 
net.c:310:tds_open_socket() succeeded 
util.c:156:Changed query state from DEAD to IDLE 
net.c:741:Sending packet 
0000 12 01 00 34 00 00 00 00-00 00 15 00 06 01 00 1b |...4.... ........| 
0010 00 01 02 00 1c 00 0c 03-00 28 00 04 ff 08 00 01 |........ .(......| 
0020 55 00 00 02 4d 53 53 51-4c 53 65 72 76 65 72 00 |U...MSSQ LServer.| 
0030 c7 39 00 00   -      |.9..| 

net.c:555:Received header 
0000 04 01 00 25 00 00 01 00-      |...%....| 

net.c:609:Received packet 
0000 04 01 00 25 00 00 01 00-00 00 15 00 06 01 00 1b |...%.... ........| 
0010 00 01 02 00 1c 00 01 03-00 1d 00 00 ff 0a 00 0f |........ ........| 
0020 a0 00 00 02 00   -      |.....| 

login.c:1057:detected flag 2 
login.c:782:quietly sending TDS 7+ login packet 
token.c:328:tds_process_login_tokens() 
net.c:555:Received header 
0000 04 01 00 72 00 51 01 00-      |...r.Q..| 

net.c:609:Received packet 
0000 04 01 00 72 00 51 01 00-aa 5e 00 18 48 00 00 01 |...r.Q.. .^..H...| 
0010 0e 1d 00 4c 00 6f 00 67-00 69 00 6e 00 20 00 66 |...L.o.g .i.n. .f| 
0020 00 61 00 69 00 6c 00 65-00 64 00 20 00 66 00 6f |.a.i.l.e .d. .f.o| 
0030 00 72 00 20 00 75 00 73-00 65 00 72 00 20 00 27 |.r. .u.s .e.r. .'| 
0040 00 74 00 73 00 6d 00 76-00 27 00 2e 00 0c 4d 00 |.t.s.m.v .'....M.| 
0050 43 00 53 00 2d 00 44 00-41 00 54 00 41 00 42 00 |C.S.-.D. A.T.A.B.| 
0060 41 00 53 00 45 00 00 01-00 fd 02 00 00 00 00 00 |A.S.E... ........| 
0070 00 00     -      |..| 

token.c:337:looking for login token, got aa(ERROR) 
token.c:122:tds_process_default_tokens() marker is aa(ERROR) 
token.c:2588:tds_process_msg() reading message 18456 from server 
token.c:2661:tds_process_msg() calling client msg handler 
dbutil.c:85:_dblib_handle_info_message(0x14e2e30, 0x1391b70, 0x7fff8b047e40) 
dbutil.c:86:msgno 18456: "Login failed for user 'xxx'." 
token.c:2674:tds_process_msg() returning TDS_SUCCEED 
token.c:337:looking for login token, got fd(DONE) 
token.c:122:tds_process_default_tokens() marker is fd(DONE) 
token.c:2339:tds_process_end: more_results = 0 
    was_cancelled = 0 
    error = 1 
    done_count_valid = 0 
token.c:2355:tds_process_end() state set to TDS_IDLE 
token.c:2370:    rows_affected = 0 
token.c:438:tds_process_login_tokens() returning TDS_FAIL 
login.c:466:login packet accepted 
util.c:156:Changed query state from IDLE to DEAD 
util.c:331:tdserror(0x14e2e30, 0x1391b70, 20002, 0) 
dblib.c:7929:dbperror(0x1383c70, 20002, 0) 
dblib.c:7981:20002: "Adaptive Server connection failed" 
dblib.c:8002:"Adaptive Server connection failed", client returns 2 (INT_CANCEL) 
util.c:361:tdserror: client library returned TDS_INT_CANCEL(2) 
util.c:384:tdserror: returning TDS_INT_CANCEL(2) 
dblib.c:1443:dbclose(0x1383c70) 
dblib.c:258:dblib_del_connection(0x7fa462faf540, 0x1391b70) 
mem.c:615:tds_free_all_results() 
dblib.c:305:dblib_release_tds_ctx(1) 
dblib.c:5882:dbfreebuf(0x1383c70) 
dblib.c:739:dbloginfree(0x1533a40) 

我完全失去了,爲什麼這是行不通的。任何幫助將非常感激。

+0

您可能想要使用servername而不是服務器。另外,我遇​​到了一些問題(至少在pyodbc中),其中連接字符串似乎沒有像傳遞kwargs一樣的行爲(可能是我的錯誤,儘管...)。你也可以嘗試使用pyodbc代替pymssql(這裏沒有pymssql的經驗)。 – 2012-05-18 14:26:32

+0

謝謝Derek,我已經採納了你的建議,從使用pymssql中解脫出來,並開始在微軟的Linux驅動程序中使用pyodbc。現在所有的工作都很好(雖然我知道這並不能回答我原來的問題)。 – chewynougat 2012-09-18 08:41:26

回答

6

「Adaptive Server連接失敗」似乎是一個相當通用的消息,但這裏有些事情要嘗試。

  1. 這個郵件列表線程(http://lists.ibiblio.org/pipermail/freetds/2010q3/026060.html)說,使用一個不正確的TDS協議結果「Adaptive Server連接失敗」的消息。在chewynougat的日誌中似乎並不是這樣,但也許它可以幫助其他人。

  2. 這個FAQ給出了很多步驟來嘗試: https://github.com/pymssql/pymssql/blob/87f4383ec153962b7ca7e63a05042d3f09005178/docs/faq.rst

之一是試圖通過測試TSQL -H,繞過從CONF讀取TDS連接,只有在通過讀取值。鑑於上面的conf同時包含端口和協議版本,可能需要檢查是否與tsql -C一起檢查是否需要進行調整。

  • 此外,在FAQ的底部,它指出

    real "Login incorrect" messages has code=18456 and severity=14

  • That is the error message being sent, so perhaps try Login Auditing ( http://msdn.microsoft.com/en-us/library/ms175850.aspx) to see if pymssql is passing your credentials in properly.

  • 即約不同的字符集相同部分會談弄亂了mssql.connect,所以也可以嘗試一個基本的密碼(即ASCII碼65-90),以確保翻譯中沒有任何東西丟失。看起來Aki和日本人一起工作,所以也許這也是一個原因。
  • 2

    我面臨同樣的問題。幸運的是,我發現了什麼是錯的。我的機器上有兩個不同版本的FreeTDS。我安裝了他們中的一個(V 0.91)由:

    sudo apt-get install freetds-dev 
    

    後來我發現這是不是最後的版本,我從freetds.org下載freetds的的tar文件。當我運行tsql -C。它顯示了正確的路徑,我正確地操作了freetds.conf。隨着變化的環境變量(http://www.freetds.org/userguide/envvar.htm),我可以連接到數據庫。但是,每次我嘗試通過Pymssql進行連接時,都出現錯誤。

    最後,我查看了日誌文件,我發現Python使用的是舊版本(v 0.91),但是我的最後一個是版本0.95。

    >>> import os 
    >>> os.environ['TDSDUMP'] = 'stdout' 
    >>> 
    >>> import pymssql 
    >>> conn = pymssql.connect(server="sqlserverhost") 
    

    所以,我刪除了版本0.91:

    sudo apt-get purge freetds-common 
    

    ,並連接到正確的版本與正確的配置pymssql。

    它也可以幫助你。

    相關問題