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

Debian9 Stretch下PHP7.2连接SQL Server遇PDO驱动问题求助

Fixing PDO SQL Server Connection Issues on Debian 9 (PHP 7.2.14)

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 (usually www-data) has read access to the driver file:

    ls -l /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1
    

    If the permissions don’t allow read access for others, fix them with:

    sudo chmod 755 /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1
    

    You can also add www-data to the group owning the file if needed, using sudo usermod -aG <group-name> www-data.

  • Check for missing library dependencies
    The driver might be missing required system libraries. Use ldd to inspect what it needs:

    ldd /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.2.so.0.1
    

    Look for lines marked not found—common culprits are outdated libssl or libcrypto versions. 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=1
    

    If the entry is missing, import the official configuration with:

    sudo odbcinst -i -d -f /opt/microsoft/msodbcsql17/etc/odbcinst.ini
    
  • Restart 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-dblib
    

    Then edit /etc/freetds/freetds.conf to 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-8
    
  • Test FreeTDS connectivity directly
    Before troubleshooting PHP, confirm FreeTDS can connect to your SQL Server:

    tsql -S sql_server -U user -P pass
    

    If 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 from freetds.conf or 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:16:40