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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37205752/
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-26 01:06:44  来源:igfitidea点击:

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

phpsql-serverazureubuntupdo

提问by hawx

I have the following script to connect to my microsoft azure server.

我有以下脚本连接到我的 microsoft azure 服务器。

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";

The script above passes on my old server, but gives me the following error message when executed from new server.

上面的脚本在我的旧服务器上传递,但从新服务器执行时给我以下错误消息。

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

My new server PHP setup is as follows:

我的新服务器 PHP 设置如下:

sudo apt-get install -y php5.6-fpm php5.6-ldap php5.6-curl php5.6-cli   php5.6-mcrypt php5.6-intl php5.6-json php5.6-pdo-dblib php5.6-mysqlnd php5.6-memcached php5.6-mbstring php5.6-imap php5.6-xml php5.6-sybase

My checks so far:

到目前为止,我的支票:

1) Both have same public facing IP address.

1) 两者都具有相同的面向公众的 IP 地址。

2) Both have identical PHP PDO/ODBC setup.

2) 两者都具有相同的 PHP PDO/ODBC 设置。

$ php -i | grep PDO
DO
PDO support => enabled
PDO drivers => dblib, mysql, odbc
PDO Driver for FreeTDS/Sybase DB-lib => enabled
PDO Driver for MySQL => enabled
PDO_ODBC
PDO Driver for ODBC (unixODBC) => enabled

3) I am able to ping my server using telnet from both servers using:

3)我可以使用以下两种服务器的 telnet ping 我的服务器:

telnet secrets.database.windows.net 1433

Any suggestions would be appreciated.

任何建议,将不胜感激。

回答by hawx

After some further googling I came across this answer.

经过进一步的谷歌搜索后,我发现了这个答案。

Connect PHP to MSSQL via PDO ODBC

通过 PDO ODBC 将 PHP 连接到 MSSQL

Turned out I just needed to update my /etc/freetds/freetds.conf

原来我只需要更新我的 /etc/freetds/freetds.conf

My changes:

我的变化:

Uncommented TDS protocol version and updated.

取消注释 TDS 协议版本并更新。

tds version = 8.0

Added mssql below examples.

在示例下方添加了 mssql。

[mssql]
host =
Port = 1433
tds version = 8.0

回答by Kannika

I got the same issue here, but it is fixed by adding version of FreeTDS 8.0 to the connection directly in PHP code:

我在这里遇到了同样的问题,但通过直接在 PHP 代码中将 FreeTDS 8.0 版本添加到连接中来修复它:

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:version=8.0;charset=UTF-8;host={$hostname};dbname={$dbname}", $username, $pwd);
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";

回答by Robin Nemeth

In my case I had a typo in $dbname, correcting it solved the issue.

在我的情况下,我有一个错字$dbname,纠正它解决了这个问题。

回答by grizzb

In my case the user's password had been reset without my knowledge.

在我的情况下,用户的密码在我不知情的情况下被重置。

回答by Gourgandine

In my case it was because the whl package you install when you launch "pip install pymssql" onboards the freetds 1.00.97. Rebuilding the package from source with freetds 1.1.6 installed did the trick and permit me to connect to the server.

就我而言,这是因为您在启动“pip install pymssql”时安装的 whl 包安装在 freetds 1.00.97 上。在安装了 freetds 1.1.6 的情况下,从源代码重建包成功了,并允许我连接到服务器。