簡體   English   中英

如何在 macOS 上配置 PHP 以對 SQL Server 使用 Windows 身份驗證?

[英]How to configure PHP on macOS to use Windows Authentication for SQL Server?

當我嘗試從 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 ✔

我錯過了什么? 我怎樣才能至少了解更多“未知異常”是什么?

經過更多調查,發現問題與執行 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

導致事情正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM