PHP PDO_mssql SQLSTATE[01002] Adaptive Server 连接失败(严重性 9)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24895605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:31:57  来源:igfitidea点击:

PHP PDO_mssql SQLSTATE[01002] Adaptive Server connection failed (severity 9)

phpsql-serverdatabasepdofreetds

提问by Jakub Riedl

I'm connecting to external MSSQL database for exports from PHP55/osx and I have wierd issue.

我正在连接到外部 MSSQL 数据库以从 PHP55/osx 导出,但我遇到了奇怪的问题。

code:

代码:

new \PDO("dblib:host={$hostname};dbname={$dbname}", $user, $pass);

throws:

抛出:

SQLSTATE[01002] Adaptive Server connection failed (severity 9)

but connection from CLI works correcty

但来自 CLI 的连接工作正常

tsql -S hostname -U user -P pass -L dbname:

tsql -S 主机名 -U 用户 -P 传递 -L 数据库名:

locale is "cs_CZ.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> 

freetds.conf:

freetds.conf:

[hostname]
    host = ipaddress
    port = 1433
    tds version = 8.0

tsql -C:

tsql -C:

                        Version: freetds v0.91
         freetds.conf directory: /usr/local/Cellar/freetds/0.91/etc
 MS db-lib source compatibility: no
    Sybase binary compatibility: no
                  Thread safety: yes
                  iconv library: yes
                    TDS version: 7.1
                          iODBC: no
                       unixodbc: no
          SSPI "trusted" logins: no
                       Kerberos: no

Any ideas? I have tried alomost anything, connect to hostname, ip, with and without port, to instance name, another user, TDS versions 7.0,7.1,7.2,8.0, reinstall php and freetds

有任何想法吗?我几乎尝试过任何东西,连接到主机名、IP、带和不带端口、实例名称、另一个用户、TDS 版本 7.0、7.1、7.2、8.0,重新安装 php 和 freetds

回答by Jorj

Check these:

检查这些:

  1. locate freetds.conf on your disk. It is possible it exists in several places and tsql uses one while PHP used another one. Best is to symlink them into one common file and test on that. Note that a common place for that file is ~/.freetds.conf beside /etc/ or /usr/local/etc/

  2. there should be a [global] section on your freetds.conf file. Put there these lines :

    tds version = 8.0

    text size = 20971520

    client charset = UTF-8

  1. 在磁盘上找到 freetds.conf。它可能存在于多个地方,tsql 使用一个,而 PHP 使用另一个。最好的方法是将它们符号链接到一个公共文件中并对其进行测试。请注意,该文件的常见位置是 /etc/ 或 /usr/local/etc/ 旁边的 ~/.freetds.conf

  2. 您的 freetds.conf 文件中应该有一个 [global] 部分。把这些行放在那里:

    tds 版本 = 8.0

    文字大小 = 20971520

    客户端字符集 = UTF-8

Of course, I expect that you already checked which driver is loaded by PHP with phpinfo(): mssql, sqlsrv or dblib

当然,我希望您已经使用 phpinfo() 检查过 PHP 加载了哪个驱动程序:mssql、sqlsrv 或 dblib

回答by bbaassssiiee

This is the generic failure message. You cannot connect to the database for whatever reason. Given you use OSX and brew most likely the problem lies in linking/loading the shared library. FreeTDS is based on Sybase 8 (as is MS SQL Server). It is a bit arcane to compile with.

这是一般的失败消息。无论出于何种原因,您都无法连接到数据库。鉴于您使用 OSX 和 brew,问题很可能在于链接/加载共享库。FreeTDS 基于 Sybase 8(与 MS SQL Server 一样)。编译有点神秘。

Was your PHP linked against the libsybdb.dylib that brew installs? You should be able to find out using phpinfo() if this is included in your server:

您的 PHP 是否与 brew 安装的 libsybdb.dylib 相关联?如果这包含在您的服务器中,您应该能够使用 phpinfo() 找出:

Configure Command '--with-pdo-dblib=shared,/usr/local/Cellar/freetds/0.91/lib'

配置命令'--with-pdo-dblib=shared,/usr/local/Cellar/freetds/0.91/lib'

To get it to work you need to compile/build your PHP with the dblib option.

要使其工作,您需要使用 dblib 选项编译/构建 PHP。

回答by Joaquim d'Souza

My solution:

我的解决方案:

Get FreeTds

获取 FreeTds

  • brew install freetds
  • copy everything in the lib/ folder of your freetds installation into the lib/ folder of your PHP installation directory, replacing the old versions of: libct.4.dylib, libct.a, libct.dylib, libsybdb.5.dylib, libsybdb.a, libsybdb.dylib
  • brew install freetds
  • 将 freetds 安装的 lib/ 文件夹中的所有内容复制到 PHP 安装目录的 lib/ 文件夹中,替换旧版本:libct.4.dyliblibct.alibct.dyliblibsybdb.5.dyliblibsybdb。一libsybdb.dylib

Compile and install the dblib extension

编译安装dblib扩展

  • download the php source for whatever php version you have installed
  • navigate to the ext/pdo_dblibsubfolder of the source
  • run phpize, making sure it is the correct binary for your installation
  • run the configure command with e.g. parameters: ./configure --with-php-config=/usr/local/php5/bin/php-config --with-pdo-dblib=/usr/local/Cellar/freetds/0.95.19/
  • run make
  • copy modules/pdo_dblib.sointo your PHP installation directory, replacing the old version
  • 下载您安装的任何 php 版本的 php 源代码
  • 导航到ext/pdo_dblib源的子文件夹
  • 运行phpize,确保它是您安装的正确二进制文件
  • 使用例如参数运行配置命令: ./configure --with-php-config=/usr/local/php5/bin/php-config --with-pdo-dblib=/usr/local/Cellar/freetds/0.95.19/
  • make
  • 复制modules/pdo_dblib.so到你的PHP安装目录,替换旧版本