【问题标题】:SQLSTATE[01002] Adaptive Server connection failed on PHP only, I can connect through CLISQLSTATE[01002] 仅在 PHP 上的 Adaptive Server 连接失败,我可以通过 CLI 连接
【发布时间】:2019-11-20 08:06:08
【问题描述】:

我正在尝试通过 Laravel 连接到我的 MSSQL 服务器。但由于某种原因,我每次都会收到此错误。我正在使用 PHP 7.3

1 PDOException::("SQLSTATE[01002] Adaptive Server 连接失败 (myserver.com:1433) (严重性 9)")

不过,我可以使用以下命令进行连接:

TDSVER=7.3 tsql -H myserver.com -U 'user' -D 'database' -p 1433 -P 'password'

这让我相信这是我的 PHP 配置方式的问题。 我正在使用 FreeTDS,这是我的配置:

#   $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
;       tds version = 7.3

        tds version = 8.0

        client charset = UTF-8
        # Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

        # Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 20971520

        # If you experience TLS handshake errors and are using openssl,
        # try adjusting the cipher list (don't surround in double or single quotes)
        # openssl ciphers = HIGH:!SSLv2:!aNULL:-DH

# A typical Sybase server
[egServer50]
        host = symachine.domain.com
        port = 5000
        tds version = 7.3

# A typical Microsoft server
[egServer70]
        host = ntmachine.domain.com
        port = 1433
        tds version = 7.3

这是我的连接代码:

$conn = new \PDO("dblib:host = myserver.com,1433; Database = mydb", "user", 'password');

这里有一些类似的问题,但他们的解决方案对我不起作用:

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

【问题讨论】:

标签: php sql-server laravel ubuntu


【解决方案1】:

您必须修复连接字符串。将 dlib 更改为 sqlsrv 并将 host 更改为 Server。更多详情here

$conn = new \PDO("sqlsrv:Server=myserver.com,1433;Database=mydb", "user", 'password');

【讨论】:

    【解决方案2】:

    在 freetds.conf 中使用TDS version 7.3 并将连接代码更改为:

    new PDO ("dblib:host=myserver.com:1433;dbname=mydb","user","password");
    

    请注意,dblib 仅在 PHP 5.6 上受支持(其本身不再受支持),因此您最好使用 Microsoft's SQLSRV 驱动程序。

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2016-09-09
      • 2017-02-15
      • 2017-01-09
      • 2022-12-24
      • 2019-08-19
      • 2016-07-13
      • 2013-12-10
      • 2019-11-09
      相关资源
      最近更新 更多