如何解决Python通过pyodbc连接SQL Server时出现的08001连接错误
First, let's fix the most obvious issue in your connection string—you’ve got a typo! You wrote Trusted_Connected=yes; but the correct attribute name is Trusted_Connection=yes;. That’s exactly why the error mentions "Invalid connection string attribute"—the ODBC driver doesn’t recognize the misspelled key.
Next, let’s tackle the core connectivity problem (the "Named Pipes Provider: Could not open a connection to SQL Server [67]" error):
Verify your server/instance name: The value
HEPHAISTEONAE5E/FUHRPmight not resolve correctly in your VM. Try these alternatives:- Use
localhost\FUHRPif you’re connecting to an instance running locally on the VM. - Swap the machine name for the VM’s IP address (e.g.,
192.168.1.100\FUHRP)—name resolution can fail in virtual environments, so using an IP often bypasses that. - Double-check the instance name in SQL Server Configuration Manager under "SQL Server Services" (it should show as
SQL Server (FUHRP)to confirm the instance is running).
- Use
Enable required network protocols: Even if remote connections are allowed, the SQL Server instance might not have the right protocols turned on:
- Open SQL Server Configuration Manager.
- Navigate to SQL Server Network Configuration > Protocols for FUHRP.
- Ensure both TCP/IP and Named Pipes are set to "Enabled".
- Restart the SQL Server (FUHRP) service for changes to take effect.
Check Windows Firewall settings: The VM’s firewall could be blocking the connection to SQL Server:
- For named instances (like FUHRP), SQL Server uses a dynamic port by default. You can either:
- Add an exception for
sqlservr.exe(usually inC:\Program Files\Microsoft SQL Server\MSSQL15.FUHRP\MSSQL\Binn). - Set a fixed port for the instance in TCP/IP properties (under IP Addresses > IPAll) and add that port to your firewall exceptions.
- Add an exception for
- For quick testing, temporarily disable the firewall to confirm it’s the blocker.
- For named instances (like FUHRP), SQL Server uses a dynamic port by default. You can either:
Here’s the corrected connection string to start with (fixing the typo and using localhost for testing):
import pyodbc # Confirm drivers are available print(pyodbc.drivers()) # Corrected connection string conn = pyodbc.connect( "Driver={ODBC Driver 17 for SQL Server};" "Server=localhost\FUHRP;" "Database=Fuhrp;" "Trusted_Connection=yes;" )
If you still hit issues, try connecting with SQL Server Management Studio first. If SSMS can’t connect either, the problem lies with your SQL Server setup—not your Python code.
内容的提问来源于stack exchange,提问作者Heph

