使用pyodbc连接Raspberry Pi 3与MSSQL Server时脚本卡住的问题
Hey there, let's tackle this MSSQL connection hang issue on your Raspberry Pi 3 running 2017 Raspbian Stretch Lite. I've dealt with similar ODBC connection quirks on Pi devices before, so here's a step-by-step breakdown of what to check and fix:
First off, let's confirm your driver setup is actually valid. Raspbian Stretch Lite is a 32-bit ARM system, so you need the correct ARM32 version of the MSSQL ODBC driver.
- Run
odbcinst -jin the terminal to get your system's ODBC configuration paths. - Open
/etc/odbcinst.iniand verify theDriverandSetuppaths point to existing files. A valid config should look something like this (adjust version numbers if needed):[ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib/arm/libmsodbcsql-17.6.so.1.1 Setup=/opt/microsoft/msodbcsql17/lib/arm/libmsodbcsql-17.6.so.1.1 UsageCount=1 - Use
ls /opt/microsoft/msodbcsql17/lib/arm/to confirm the driver file exists. If the path is wrong, your script will hang indefinitely waiting for a driver that isn't there.
Don't waste time debugging Python code until you confirm the system-level ODBC connection works. Install the unixODBC tools to test directly:
sudo apt-get install unixodbc unixodbc-dev
Then run this command to test connectivity (replace placeholders with your MSSQL details):
isql -v "ODBC Driver 17 for SQL Server" YOUR_SERVER_NAME YOUR_USERNAME YOUR_PASSWORD
If this command also hangs, the problem is not in Python—it's either network-related, driver-dependent, or your MSSQL server isn't configured for remote access. If it connects successfully, move on to debugging your Python script.
Script hangs usually happen because of missing connection parameters or no timeout set. Try adding explicit timeouts and verifying your connection string format:
import pyodbc try: conn_str = ( "DRIVER={ODBC Driver 17 for SQL Server};" "SERVER=YOUR_SERVER_IP,1433;" # Always specify the port (default 1433) "DATABASE=YOUR_DB_NAME;" "UID=YOUR_USERNAME;" "PWD=YOUR_PASSWORD;" "Connection Timeout=10;" # Prevents infinite hanging ) conn = pyodbc.connect(conn_str) print("Connection successful!") conn.close() except pyodbc.Error as e: print(f"ODBC Error: {e}") except Exception as e: print(f"Unexpected Error: {e}")
Also, verify your Pi can reach the MSSQL server:
- Use
ping YOUR_SERVER_IPto check basic network connectivity. - Use
telnet YOUR_SERVER_IP 1433to confirm the MSSQL port is open (if telnet fails, your server's firewall or MSSQL remote access settings are blocking you).
2017 Stretch Lite is pretty old, and newer MSSQL drivers might have dependency conflicts. Reinstall the driver using the correct Debian 9 (Stretch) source:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - curl https://packages.microsoft.com/config/debian/9/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list sudo apt-get update sudo ACCEPT_EULA=Y apt-get install msodbcsql17
This ensures you get a driver version that's compatible with your old Stretch system.
If you're still stuck, turn on ODBC tracing to see exactly where the connection is hanging. Edit /etc/odbcinst.ini to add:
[ODBC] Trace=Yes TraceFile=/tmp/odbc_connection.log
Run your Python script again, interrupt it, then check /tmp/odbc_connection.log—it will show every step of the connection attempt, from driver loading to network handshake, which can pinpoint exactly what's going wrong.
内容的提问来源于stack exchange,提问作者TomBombadil

