Как настроить PHP на macOS для использования аутентификации Windows для SQL Server? - PullRequest
0 голосов
/ 30 октября 2018

Когда я пытаюсь подключиться из 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 ✔

Чего мне не хватает? Как я могу хотя бы узнать больше, что это за «неизвестное исключение»?

1 Ответ

0 голосов
/ 01 ноября 2018

После дополнительных исследований выясняется, что проблема связана со средой, в которой выполняется 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

заставляет вещи работать нормально.

...