[python] 如何在 linux 環境下透過 python 存取 MSSQL

Visits: 2

如何在 linux 環境下透過 python 存取 MSSQL

寫了一支程式,在 Win10 運作無誤,但是在 linux 環境下跳出 pymssql.InterfaceError: Connection to the database failed for an unknown reason.

file

尋找解決方案

把錯誤訊息丟去 google 後,發現錯誤原因是需要 TDS 才能讓 linux 連線至 MSSQL。

解法

  1. 安裝 freetds client
sudo apt install freetds-bin
  1. 修改 freetds.conf 設定檔,把要連線的 MSSQL host 與 port 輸入
sudo vim /etc/freetds/freetds.conf

# [freetds.conf]
# A typical Microsoft server
[egServer70]
        #host = ntmachine.domain.com
        host = mssql_host
        port = mssql_port
        tds version = 7.0
  1. 在 shell 測試連線
tsql -S mssql_host -U mssql_user
Password: mssql_password

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

出現 1> 就代表連線成功囉

About the Author

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

You may also like these