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

Ubuntu 16.04远程连接MS SQL Server遇pyodbc登录超时错误求助

Troubleshooting pyodbc Login Timeout on Ubuntu 16.04 Connecting to MS SQL Server

That login timeout error usually points to a network connectivity issue, misconfigured driver setup, or an incorrect connection string. Let’s walk through the most common fixes step by step:

  • First, verify network reachability
    Timeouts often happen when your Ubuntu machine can’t reach the SQL Server’s port. Test the connection using nc or telnet to rule out firewall/routing problems:

    nc -zv <your-server-ip> 51333
    # Or use telnet if nc isn't installed
    telnet <your-server-ip> 51333
    

    If this fails:

    • Check Ubuntu’s local firewall (ufw status) to ensure outgoing traffic on port 51333 is allowed.
    • Confirm the SQL Server’s firewall (on its host machine or cloud security group) permits incoming connections from your Ubuntu server’s IP on port 51333.
    • If you’re on a VPN or corporate network, make sure there are no routing rules blocking traffic to the SQL Server.
  • Validate ODBC Driver 17 installation
    Ubuntu 16.04 is an older release, so you need to ensure the driver was installed correctly. Re-run the official installation steps to confirm:

    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
    apt-get update
    ACCEPT_EULA=Y apt-get install msodbcsql17
    

    Then check if the driver is registered with unixODBC:

    odbcinst -q -d
    

    You should see ODBC Driver 17 for SQL Server listed. If not, the installation failed—check for errors during the install process.

  • Fix your connection string format
    Some versions of the ODBC Driver 17 don’t recognize the standalone PORT parameter. Instead, append the port directly to the server address with a comma:

    import pyodbc
    conn = pyodbc.connect(
        r'DRIVER=ODBC Driver 17 for SQL Server;'
        r'SERVER=xxx.xxx.xxx.xxx,51333;'  # Replace with your server IP + port
        r'DATABASE=TestDB;'
        r'UID=xxxx;'
        r'PWD=xxxx;'
    )
    

    This is the more reliable format for most SQL Server ODBC connections.

  • Check SQL Server remote access settings
    Ensure the SQL Server instance is configured to allow remote connections:

    1. On the SQL Server host, open SQL Server Configuration Manager.
    2. Navigate to SQL Server Network Configuration > Protocols for [Your Instance].
    3. Make sure TCP/IP is enabled.
    4. Double-click TCP/IP, go to the IP Addresses tab, and confirm port 51333 is set for the relevant IP (or that TCP Dynamic Ports is disabled if you’re using a static port).
    5. Also verify that your SQL Server login has permissions to access the TestDB database and hasn’t been locked out. Test the login directly via SSMS on the SQL Server host to rule out credential issues.
  • Test with unixODBC’s isql tool
    To isolate whether the issue is with pyodbc or the underlying ODBC setup, use isql to test the connection:

    isql -v "ODBC Driver 17 for SQL Server" <your-server-ip>,51333 <your-uid> <your-pwd>
    

    If isql times out, the problem is in the ODBC/driver/network layer. If it connects successfully, then the issue is specific to your pyodbc code (though this is less likely in your case).

内容的提问来源于stack exchange,提问作者Krishnang K Dalal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:49:03