You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派4通过FreeTDS+unixODBC连接MSSQL失败求助

解决树莓派4通过FreeTDS/unixODBC连接MSSQL的Error 20009问题

错误信息

Error 20009(严重级别9):无法连接:Adaptive Server不可用或不存在,系统错误111,“连接被拒绝”

当前配置

freetds.conf 片段

[fred]
host = 109.*.*.*    # Remote Sql Server's IP addr
port = 1433         # this is default
tds version = 7.4     # this is by the time i post this
client charset =UTF-8

odbc.ini 片段

[test]
Driver = FreeTDS
Description = MSSQL Server
Trace = No
Server = fred      # IP or host name of the Sql Server
Port = 1433           # This is default
TDS_Version = 7.4

odbcinst.ini 片段

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
UsageCount=1

分步解决方法

1. 排查网络连通性

  • 测试服务器IP可达性:
    ping 109.*.*.*
    
  • 测试1433端口是否开放(连接失败则说明端口被防火墙/安全组拦截):
    telnet 109.*.*.* 1433
    
    若telnet未安装,先执行sudo apt install telnet

2. 检查MSSQL服务器端配置

  • 启用TCP/IP协议:打开SQL Server配置管理器,展开「SQL Server网络配置」→ 对应实例的「协议」,确保TCP/IP状态为「已启用」
  • 确认TCP/IP监听端口:双击TCP/IP → 「IP地址」标签,找到IPAll段的「TCP端口」设为1433
  • 允许远程连接:在SSMS中右键服务器→「属性」→「连接」,勾选「允许远程连接到此服务器」
  • 配置防火墙/安全组:Windows防火墙添加入站规则允许1433端口;若服务器在云端,检查云服务商安全组是否放行树莓派IP的1433端口访问

3. 优化FreeTDS配置与测试

  • 修正freetds.conf的缩进问题:配置段内的键值对不要加缩进,比如把host = ...前面的空格删除
  • 直接用tsql命令跳过配置文件测试:
    tsql -H 109.*.*.* -p 1433 -U 你的用户名 -P 你的密码 -C 7.4
    
  • 验证FreeTDS驱动路径:
    find /usr -name libtdsodbc.so
    
    确保输出路径和odbcinst.ini中的Driver字段一致

4. 测试unixODBC连接

  • 使用isql命令测试ODBC数据源:
    isql -v test 你的用户名 你的密码
    
    若出现错误,检查配置文件权限:
    sudo chmod 644 /etc/odbc.ini /etc/odbcinst.ini
    

5. pyodbc连接测试代码

import pyodbc

# 替换为实际的数据库名、用户名、密码
conn_str = (
    "DRIVER={FreeTDS};"
    "SERVER=109.*.*.*;"
    "PORT=1433;"
    "DATABASE=你的数据库名称;"
    "UID=你的SQL用户名;"
    "PWD=你的SQL密码;"
    "TDS_Version=7.4;"
    "CHARSET=UTF-8;"
)

try:
    conn = pyodbc.connect(conn_str)
    print("数据库连接成功!")
    conn.close()
except Exception as e:
    print(f"连接失败:{str(e)}")

内容的提问来源于stack exchange,提问作者MyatKaung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:35:36