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

使用PHP和Docker连接SQL Server 19失败的问题求助

排查Docker环境下PHP连接SQL Server失败问题

我在XAMPP环境下的PHP代码可正常连接SQL Server,支持查询与插入操作,已在php.ini中添加以下扩展配置:
extension=php_sqlsrv_81_ts_x64.dll
extension=php_pdo_sqlsrv_81_ts_x64.dll

PHP连接代码

function DBConnect(){
  $serverName = "localhost\SQLEXPRESS"; //我可从其他电脑连接SQL Server,曾尝试用tcp:<computername>,<port>替代'localhost\SQLEXPRESS'
  $connectionInfo = [
    "Database"=>"", //此处已移除数据库名称
    "Uid"=>"",
    "PWD"=>""
  ];

  $conn = sqlsrv_connect( $serverName, $connectionInfo);
  if(!$conn){
    echo die(print_r(sqlsrv_errors(), true));
    return false;
  }
  else{
    return $conn;
  }
}

Dockerfile内容

FROM php:8.1.10-apache
ENV ACCEPT_EULA=Y
RUN apt-get update && apt-get install -y gnupg2
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 
RUN curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list 
RUN apt-get update 
RUN ACCEPT_EULA=Y apt-get -y --no-install-recommends install msodbcsql18 unixodbc-dev 
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN docker-php-ext-enable sqlsrv pdo_sqlsrv

COPY src/ /var/www/html/

运行Docker容器时的错误信息

Array
(
    [0] => Array
        (
            [0] => HYT00
            [SQLSTATE] => HYT00
            [1] => 0
            [code] => 0
            [2] => [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired
            [message] => [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired
        )

    [1] => Array
        (
            [0] => 08001
            [SQLSTATE] => 08001
            [1] => 11001
            [code] => 11001
            [2] => [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2AF9
            [message] => [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2AF9
        )

    [2] => Array
        (
            [0] => 08001
            [SQLSTATE] => 08001
            [1] => 11001
            [code] => 11001
            [2] => [Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
            [message] => [Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
        )

)

排查步骤

  • 修正容器内的localhost指向:Docker容器中的localhost是容器自身,不是宿主机。如果SQL Server在宿主机上:
    • Windows环境:将serverName改为tcp:host.docker.internal,1433
    • Linux环境:运行容器时添加--network host参数,或者改用宿主机的实际IP,格式为tcp:<宿主机IP>,1433
  • 调整连接字符串格式:Linux环境下的sqlsrv扩展不支持localhost\SQLEXPRESS这种命名实例格式,必须使用TCP连接格式tcp:<host>,<port>
  • 检查SQL Server配置:确保SQL Server已启用TCP/IP协议,开放默认端口1433,并且允许远程连接
  • 测试网络连通性:进入容器内部,执行nc -zv <目标IP> 1433或telnet <目标IP> 1433,确认容器能连通SQL Server端口
  • 尝试降级ODBC驱动:ODBC Driver 18可能与旧版本SQL Server存在兼容性问题,可修改Dockerfile,将msodbcsql18替换为msodbcsql17后重新构建镜像
  • 验证账号权限:确认SQL Server的登录账号允许从容器所在IP段连接,且账号具备目标数据库的访问权限

内容的提问来源于stack exchange,提问作者ks10

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 03:10:16