Debian9 Stretch下PHP7.2连接SQL Server遇PDO驱动问题求助
Let's walk through practical solutions for both of the connection problems you're facing:
1. sqlsrv Driver: "Can't open lib ... file not found" (but file exists)
This error almost always stems from permission gaps, missing system dependencies, or misconfigured ODBC settings. Here's how to resolve it step by step:
Verify file permissions
First, check if your web server user (usuallywww-data) has read access to the driver file:ls -l /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1If the permissions don’t allow read access for others, fix them with:
sudo chmod 755 /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1You can also add
www-datato the group owning the file if needed, usingsudo usermod -aG <group-name> www-data.Check for missing library dependencies
The driver might be missing required system libraries. Uselddto inspect what it needs:ldd /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1Look for lines marked
not found—common culprits are outdatedlibsslorlibcryptoversions. On Debian 9, you may need to install compatible packages or adjust symlinks to match the driver’s requirements.Validate ODBC driver configuration
Make sure the driver is properly registered in/etc/odbcinst.ini. Open the file and confirm you have an entry like this:[ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1 UsageCount=1If the entry is missing, import the official configuration with:
sudo odbcinst -i -d -f /opt/microsoft/msodbcsql17/etc/odbcinst.iniRestart PHP and web server
After making changes, reload PHP-FPM and your web server to apply them:sudo systemctl restart php7.2-fpm sudo systemctl restart apache2 # Replace with nginx if that's your setup
2. dblib Driver: "Adaptive Server is unavailable or does not exist"
The dblib driver relies on FreeTDS, so we’ll focus on configuring that correctly and verifying low-level connectivity:
Install and configure FreeTDS
First, install the required packages:sudo apt-get install freetds-bin freetds-dev php7.2-pdo-dblibThen edit
/etc/freetds/freetds.confto add your SQL Server details (adjust the TDS version to match your SQL Server release—7.4 works for 2016 and newer):[sql_server] host = xxx.xxx.xxx.xxx port = 1433 tds version = 7.4 client charset = UTF-8Test FreeTDS connectivity directly
Before troubleshooting PHP, confirm FreeTDS can connect to your SQL Server:tsql -S sql_server -U user -P passIf this fails, the issue is outside PHP:
- Check if the SQL Server’s firewall allows incoming traffic on port 1433.
- Ensure SQL Server is configured to allow remote connections (enable TCP/IP in SQL Server Configuration Manager).
- Confirm SQL Server Authentication is enabled (not just Windows Authentication).
Adjust your PHP connection string
Use the alias fromfreetds.confor explicitly include the port in your connection string for reliability:$conn = new PDO("dblib:host=sql_server;dbname=xxxx;", "user", "pass"); // Or with explicit port: $conn = new PDO("dblib:host=xxx.xxx.xxx.xxx:1433;dbname=xxxx;", "user", "pass");Restart PHP-FPM
Ensure the dblib extension loads properly by restarting PHP-FPM:sudo systemctl restart php7.2-fpm
内容的提问来源于stack exchange,提问作者miraoui.bob

