Ubuntu 16.04远程连接MS SQL Server遇pyodbc登录超时错误求助
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 usingncortelnetto rule out firewall/routing problems:nc -zv <your-server-ip> 51333 # Or use telnet if nc isn't installed telnet <your-server-ip> 51333If 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.
- Check Ubuntu’s local firewall (
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 msodbcsql17Then check if the driver is registered with unixODBC:
odbcinst -q -dYou should see
ODBC Driver 17 for SQL Serverlisted. 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 standalonePORTparameter. 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:- On the SQL Server host, open SQL Server Configuration Manager.
- Navigate to SQL Server Network Configuration > Protocols for [Your Instance].
- Make sure TCP/IP is enabled.
- Double-click TCP/IP, go to the IP Addresses tab, and confirm port 51333 is set for the relevant IP (or that
TCP Dynamic Portsis disabled if you’re using a static port). - Also verify that your SQL Server login has permissions to access the
TestDBdatabase 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
isqltool
To isolate whether the issue is with pyodbc or the underlying ODBC setup, useisqlto test the connection:isql -v "ODBC Driver 17 for SQL Server" <your-server-ip>,51333 <your-uid> <your-pwd>If
isqltimes 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

