【问题标题】:How to configure PHP on macOS to use Windows Authentication for SQL Server?如何在 macOS 上配置 PHP 以对 SQL Server 使用 Windows 身份验证?
【发布时间】:2018-10-30 15:05:59
【问题描述】:

当我尝试从 PHP 连接时,我得到一个

PHP Fatal error:  sqlsrv_connect: Unknown exception caught. in /<edited>/testsql-plain.php on line 8

testsql-plain.php

<?php
$serverName = "<my DB server>";
$connectionOptions = array(
    "Database" => "<my DB>",
    "CharacterSet" => "UTF-8"
);

$conn = sqlsrv_connect($serverName, $connectionOptions); // This is line 8

我已设置 Kerberos 以对 SQL Server 进行身份验证,并且使用 sqlcmd 可以正常工作:

$ klist
Credentials cache: API:<edited>
        Principal: <edited>

  Issued                Expires               Principal
Oct 30 15:13:41 2018  Oct 31 01:13:41 2018  krbtgt/<edited>
Oct 30 15:13:54 2018  Oct 31 01:13:41 2018  MSSQLSvc/<edited>:1433@<edited>

$ sqlcmd -S <my DB server> -d <my DB> -E -q 'SELECT @@Version AS SQL_VERSION;'
SQL_VERSION
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2012 (SP4) (KB4018073) - 11.0.7001.0 (X64)
    Aug 15 2017 10:23:29
    Copyright (c) Microsoft Corporation
    Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

我有 PHP 7.2.11,安装了 PHP 驱动和 ODBC 驱动:

$ sudo pecl install sqlsrv
pecl/sqlsrv is already installed and is the same as the released version 5.3.0
install failed

$ sudo pecl install pdo_sqlsrv
pecl/pdo_sqlsrv is already installed and is the same as the released version 5.3.0
install failed 

$ grep sqlsrv.so /usr/local/etc/php/7.2/php.ini
extension="pdo_sqlsrv.so"
extension="sqlsrv.so"

$ brew info msodbcsql17 mssql-tools
microsoft/mssql-release/msodbcsql17: stable 17.2.0.1
ODBC Driver for Microsoft(R) SQL Server(R)
https://msdn.microsoft.com/en-us/library/mt654048(v=sql.1).aspx
/usr/local/Cellar/msodbcsql17/17.2.0.1 (9 files, 2.7MB) *
  Built from source on 2018-10-24 at 15:45:18
From: https://github.com/Microsoft/homebrew-mssql-release/blob/master/Formula/msodbcsql17.rb
==> Dependencies
Required: unixodbc ✔, openssl ✔
==> Options
--without-registration
    Don't register the driver in odbcinst.ini
==> Caveats
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 17 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 17 for SQL Server"

microsoft/mssql-release/mssql-tools: stable 17.2.0.1
Sqlcmd and Bcp for Microsoft(R) SQL Server(R)
https://msdn.microsoft.com/en-us/library/ms162773.aspx
/usr/local/Cellar/mssql-tools/17.2.0.1 (11 files, 1.2MB) *
  Built from source on 2018-10-24 at 15:45:39
From: https://github.com/Microsoft/homebrew-mssql-release/blob/master/Formula/mssql-tools.rb
==> Dependencies
Required: unixodbc ✔, openssl ✔, msodbcsql17 ✔

我错过了什么?我怎样才能至少找出更多“未知异常”是什么?

【问题讨论】:

  • try { $conn = sqlsrv_connect($serverName, $connectionOptions); } catch (Exception $e) { var_dump($e); exit;} 应该告诉你到底哪里出了问题
  • @delboy1978uk 这是PHP Fatal error,我听不懂(我试过了)。我注册了一个关闭函数来打印error_get_last(),但它给出的结果是一样的:“sqlsrv_connect:捕获的未知异常”
  • 你能用 XDebug 单步调试代码吗?

标签: php sql-server macos active-directory kerberos


【解决方案1】:

经过更多调查发现问题与 PHP 的执行环境有关。

当我在干净的环境中运行 PHP 处理器时

$ env -i /usr/local/Cellar/php/7.2.11/bin/php /<edited>/testsql-plain.php

我得到一个连接并且可以运行一个查询,例如。 "SELECT @@Version AS SQL_VERSION":

Microsoft SQL Server 2012 (SP4) (KB4018073) - 11.0.7001.0 (X64)
    Aug 15 2017 10:23:29
    Copyright (c) Microsoft Corporation
    Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

同样适用于启动 Apache。在我的常规终端会话中运行 sudo apachectl start 会导致数据库连接失败,但使用干净的环境启动它

env -i sudo /usr/local/bin/apachectl start

使事情正常工作。

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多